xref: /illumos-gate/usr/src/uts/common/inet/sctp/sctp.c (revision f4b3ec61)
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
55dddb8baSkcpoon  * Common Development and Distribution License (the "License").
65dddb8baSkcpoon  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
215dddb8baSkcpoon 
227c478bd9Sstevel@tonic-gate /*
23*f4b3ec61Sdh  * Copyright 2007 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 #include <sys/strsubr.h>
327c478bd9Sstevel@tonic-gate #include <sys/stropts.h>
337c478bd9Sstevel@tonic-gate #include <sys/strsun.h>
347c478bd9Sstevel@tonic-gate #define	_SUN_TPI_VERSION 2
357c478bd9Sstevel@tonic-gate #include <sys/tihdr.h>
367c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
377c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
387c478bd9Sstevel@tonic-gate #include <sys/xti_inet.h>
397c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
407c478bd9Sstevel@tonic-gate #include <sys/debug.h>
417c478bd9Sstevel@tonic-gate #include <sys/vtrace.h>
427c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
437c478bd9Sstevel@tonic-gate #include <sys/cpuvar.h>
447c478bd9Sstevel@tonic-gate #include <sys/random.h>
4545916cd2Sjpk #include <sys/priv.h>
46*f4b3ec61Sdh #include <sys/sunldi.h>
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate #include <sys/errno.h>
497c478bd9Sstevel@tonic-gate #include <sys/signal.h>
507c478bd9Sstevel@tonic-gate #include <sys/socket.h>
517c478bd9Sstevel@tonic-gate #include <sys/isa_defs.h>
527c478bd9Sstevel@tonic-gate #include <netinet/in.h>
537c478bd9Sstevel@tonic-gate #include <netinet/tcp.h>
547c478bd9Sstevel@tonic-gate #include <netinet/ip6.h>
557c478bd9Sstevel@tonic-gate #include <netinet/icmp6.h>
567c478bd9Sstevel@tonic-gate #include <netinet/sctp.h>
577c478bd9Sstevel@tonic-gate #include <net/if.h>
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate #include <inet/common.h>
607c478bd9Sstevel@tonic-gate #include <inet/ip.h>
617c478bd9Sstevel@tonic-gate #include <inet/ip6.h>
627c478bd9Sstevel@tonic-gate #include <inet/mi.h>
637c478bd9Sstevel@tonic-gate #include <inet/mib2.h>
64*f4b3ec61Sdh #include <inet/kstatcom.h>
657c478bd9Sstevel@tonic-gate #include <inet/nd.h>
667c478bd9Sstevel@tonic-gate #include <inet/optcom.h>
677c478bd9Sstevel@tonic-gate #include <inet/ipclassifier.h>
687c478bd9Sstevel@tonic-gate #include <inet/ipsec_impl.h>
697c478bd9Sstevel@tonic-gate #include <inet/sctp_ip.h>
707c478bd9Sstevel@tonic-gate #include <inet/sctp_crc32.h>
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate #include "sctp_impl.h"
737c478bd9Sstevel@tonic-gate #include "sctp_addr.h"
741d8c4025Svi #include "sctp_asconf.h"
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate extern major_t SCTP6_MAJ;
777c478bd9Sstevel@tonic-gate extern major_t SCTP_MAJ;
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate int sctpdebug;
807c478bd9Sstevel@tonic-gate sin6_t	sctp_sin6_null;	/* Zero address for quick clears */
817c478bd9Sstevel@tonic-gate 
82*f4b3ec61Sdh /*
83*f4b3ec61Sdh  * Have to ensure that sctp_g_q_close is not done by an
84*f4b3ec61Sdh  * interrupt thread.
85*f4b3ec61Sdh  */
86*f4b3ec61Sdh static taskq_t *sctp_taskq;
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate static void	sctp_closei_local(sctp_t *sctp);
897c478bd9Sstevel@tonic-gate static int	sctp_init_values(sctp_t *, sctp_t *, int);
907c478bd9Sstevel@tonic-gate static void	sctp_icmp_error_ipv6(sctp_t *sctp, mblk_t *mp);
917c478bd9Sstevel@tonic-gate static void	sctp_process_recvq(void *);
92*f4b3ec61Sdh static void	sctp_rq_tq_init(sctp_stack_t *);
93*f4b3ec61Sdh static void	sctp_rq_tq_fini(sctp_stack_t *);
947c478bd9Sstevel@tonic-gate static void	sctp_conn_cache_init();
957c478bd9Sstevel@tonic-gate static void	sctp_conn_cache_fini();
967c478bd9Sstevel@tonic-gate static int	sctp_conn_cache_constructor();
977c478bd9Sstevel@tonic-gate static void	sctp_conn_cache_destructor();
98*f4b3ec61Sdh void		sctp_g_q_setup(sctp_stack_t *);
99*f4b3ec61Sdh void		sctp_g_q_create(sctp_stack_t *);
100*f4b3ec61Sdh void		sctp_g_q_destroy(sctp_stack_t *);
101*f4b3ec61Sdh 
102*f4b3ec61Sdh static void	*sctp_stack_init(netstackid_t stackid, netstack_t *ns);
103*f4b3ec61Sdh static void	sctp_stack_shutdown(netstackid_t stackid, void *arg);
104*f4b3ec61Sdh static void	sctp_stack_fini(netstackid_t stackid, void *arg);
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate /*
1077c478bd9Sstevel@tonic-gate  * SCTP receive queue taskq
1087c478bd9Sstevel@tonic-gate  *
1097c478bd9Sstevel@tonic-gate  * At SCTP initialization time, a default taskq is created for
1107c478bd9Sstevel@tonic-gate  * servicing packets received when the interrupt thread cannot
1117c478bd9Sstevel@tonic-gate  * get a hold on the sctp_t.  The number of taskq can be increased in
1127c478bd9Sstevel@tonic-gate  * sctp_find_next_tq() when an existing taskq cannot be dispatched.
1137c478bd9Sstevel@tonic-gate  * The taskqs are never removed.  But the max number of taskq which
1147c478bd9Sstevel@tonic-gate  * can be created is controlled by sctp_recvq_tq_list_max_sz.  Note
1157c478bd9Sstevel@tonic-gate  * that SCTP recvq taskq is not tied to any specific CPU or ill.
1167c478bd9Sstevel@tonic-gate  *
1177c478bd9Sstevel@tonic-gate  * Those taskqs are stored in an array recvq_tq_list.  And they are
1187c478bd9Sstevel@tonic-gate  * used in a round robin fashion.  The current taskq being used is
1197c478bd9Sstevel@tonic-gate  * determined by recvq_tq_list_cur.
1207c478bd9Sstevel@tonic-gate  */
1217c478bd9Sstevel@tonic-gate 
122*f4b3ec61Sdh /* /etc/system variables */
1237c478bd9Sstevel@tonic-gate /* The minimum number of threads for each taskq. */
1247c478bd9Sstevel@tonic-gate int		sctp_recvq_tq_thr_min = 4;
1257c478bd9Sstevel@tonic-gate /* The maximum number of threads for each taskq. */
1267c478bd9Sstevel@tonic-gate int		sctp_recvq_tq_thr_max = 16;
1277c478bd9Sstevel@tonic-gate /* The minimum number of tasks for each taskq. */
1287c478bd9Sstevel@tonic-gate int		sctp_recvq_tq_task_min = 5;
1297c478bd9Sstevel@tonic-gate /* The maxiimum number of tasks for each taskq. */
1307c478bd9Sstevel@tonic-gate int		sctp_recvq_tq_task_max = 50;
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate /*  sctp_t/conn_t kmem cache */
1337c478bd9Sstevel@tonic-gate struct kmem_cache	*sctp_conn_cache;
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate #define	SCTP_CONDEMNED(sctp)				\
1367c478bd9Sstevel@tonic-gate 	mutex_enter(&(sctp)->sctp_reflock);		\
1377c478bd9Sstevel@tonic-gate 	((sctp)->sctp_condemned = B_TRUE);		\
1385dddb8baSkcpoon 	mutex_exit(&(sctp)->sctp_reflock);
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate /* Link/unlink a sctp_t to/from the global list. */
141*f4b3ec61Sdh #define	SCTP_LINK(sctp, sctps)				\
142*f4b3ec61Sdh 	mutex_enter(&(sctps)->sctps_g_lock);		\
143*f4b3ec61Sdh 	list_insert_tail(&sctps->sctps_g_list, (sctp));	\
144*f4b3ec61Sdh 	mutex_exit(&(sctps)->sctps_g_lock);
1457c478bd9Sstevel@tonic-gate 
146*f4b3ec61Sdh #define	SCTP_UNLINK(sctp, sctps)				\
147*f4b3ec61Sdh 	mutex_enter(&(sctps)->sctps_g_lock);		\
1487c478bd9Sstevel@tonic-gate 	ASSERT((sctp)->sctp_condemned);			\
149*f4b3ec61Sdh 	list_remove(&(sctps)->sctps_g_list, (sctp));	\
150*f4b3ec61Sdh 	mutex_exit(&(sctps)->sctps_g_lock);
1517c478bd9Sstevel@tonic-gate 
1521d8c4025Svi /*
1531d8c4025Svi  * Hooks for Sun Cluster. On non-clustered nodes these will remain NULL.
1541d8c4025Svi  * PSARC/2005/602.
1551d8c4025Svi  */
1561d8c4025Svi void (*cl_sctp_listen)(sa_family_t, uchar_t *, uint_t, in_port_t) = NULL;
1571d8c4025Svi void (*cl_sctp_unlisten)(sa_family_t, uchar_t *, uint_t, in_port_t) = NULL;
1581d8c4025Svi void (*cl_sctp_connect)(sa_family_t, uchar_t *, uint_t, in_port_t,
1591d8c4025Svi     uchar_t *, uint_t, in_port_t, boolean_t, cl_sctp_handle_t) = NULL;
1601d8c4025Svi void (*cl_sctp_disconnect)(sa_family_t, cl_sctp_handle_t) = NULL;
1611d8c4025Svi void (*cl_sctp_assoc_change)(sa_family_t, uchar_t *, size_t, uint_t,
1621d8c4025Svi     uchar_t *, size_t, uint_t, int, cl_sctp_handle_t) = NULL;
1631d8c4025Svi void (*cl_sctp_check_addrs)(sa_family_t, in_port_t, uchar_t **, size_t,
1641d8c4025Svi     uint_t *, boolean_t) = NULL;
1657c478bd9Sstevel@tonic-gate /*
1667c478bd9Sstevel@tonic-gate  * Return the version number of the SCTP kernel interface.
1677c478bd9Sstevel@tonic-gate  */
1687c478bd9Sstevel@tonic-gate int
1697c478bd9Sstevel@tonic-gate sctp_itf_ver(int cl_ver)
1707c478bd9Sstevel@tonic-gate {
1717c478bd9Sstevel@tonic-gate 	if (cl_ver != SCTP_ITF_VER)
1727c478bd9Sstevel@tonic-gate 		return (-1);
1737c478bd9Sstevel@tonic-gate 	return (SCTP_ITF_VER);
1747c478bd9Sstevel@tonic-gate }
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate /*
1777c478bd9Sstevel@tonic-gate  * Called when we need a new sctp instantiation but don't really have a
1787c478bd9Sstevel@tonic-gate  * new q to hang it off of. Copy the priv flag from the passed in structure.
1797c478bd9Sstevel@tonic-gate  */
1807c478bd9Sstevel@tonic-gate sctp_t *
1817c478bd9Sstevel@tonic-gate sctp_create_eager(sctp_t *psctp)
1827c478bd9Sstevel@tonic-gate {
1837c478bd9Sstevel@tonic-gate 	sctp_t	*sctp;
1847c478bd9Sstevel@tonic-gate 	mblk_t	*ack_mp, *hb_mp;
1857c478bd9Sstevel@tonic-gate 	conn_t	*connp, *pconnp;
18645916cd2Sjpk 	cred_t *credp;
187*f4b3ec61Sdh 	sctp_stack_t	*sctps = psctp->sctp_sctps;
1887c478bd9Sstevel@tonic-gate 
189*f4b3ec61Sdh 	if ((connp = ipcl_conn_create(IPCL_SCTPCONN, KM_NOSLEEP,
190*f4b3ec61Sdh 		    sctps->sctps_netstack)) == NULL) {
1917c478bd9Sstevel@tonic-gate 		return (NULL);
192*f4b3ec61Sdh 	}
19345916cd2Sjpk 
19445916cd2Sjpk 	connp->conn_ulp_labeled = is_system_labeled();
19545916cd2Sjpk 
1967c478bd9Sstevel@tonic-gate 	sctp = CONN2SCTP(connp);
197*f4b3ec61Sdh 	sctp->sctp_sctps = sctps;
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate 	if ((ack_mp = sctp_timer_alloc(sctp, sctp_ack_timer)) == NULL ||
2007c478bd9Sstevel@tonic-gate 	    (hb_mp = sctp_timer_alloc(sctp, sctp_heartbeat_timer)) == NULL) {
2017c478bd9Sstevel@tonic-gate 		if (ack_mp != NULL)
2027c478bd9Sstevel@tonic-gate 			freeb(ack_mp);
203*f4b3ec61Sdh 		netstack_rele(sctps->sctps_netstack);
204*f4b3ec61Sdh 		connp->conn_netstack = NULL;
205*f4b3ec61Sdh 		sctp->sctp_sctps = NULL;
206*f4b3ec61Sdh 		SCTP_G_Q_REFRELE(sctps);
2077c478bd9Sstevel@tonic-gate 		kmem_cache_free(sctp_conn_cache, connp);
2087c478bd9Sstevel@tonic-gate 		return (NULL);
2097c478bd9Sstevel@tonic-gate 	}
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate 	sctp->sctp_ack_mp = ack_mp;
2127c478bd9Sstevel@tonic-gate 	sctp->sctp_heartbeat_mp = hb_mp;
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate 	/* Inherit information from the "parent" */
2157c478bd9Sstevel@tonic-gate 	sctp->sctp_ipversion = psctp->sctp_ipversion;
2167c478bd9Sstevel@tonic-gate 	sctp->sctp_family = psctp->sctp_family;
2177c478bd9Sstevel@tonic-gate 	pconnp = psctp->sctp_connp;
2187c478bd9Sstevel@tonic-gate 	connp->conn_af_isv6 = pconnp->conn_af_isv6;
2197c478bd9Sstevel@tonic-gate 	connp->conn_pkt_isv6 = pconnp->conn_pkt_isv6;
2207c478bd9Sstevel@tonic-gate 	connp->conn_ipv6_v6only = pconnp->conn_ipv6_v6only;
2217c478bd9Sstevel@tonic-gate 	if (sctp_init_values(sctp, psctp, KM_NOSLEEP) != 0) {
2227c478bd9Sstevel@tonic-gate 		freeb(ack_mp);
2237c478bd9Sstevel@tonic-gate 		freeb(hb_mp);
224*f4b3ec61Sdh 		netstack_rele(sctps->sctps_netstack);
225*f4b3ec61Sdh 		connp->conn_netstack = NULL;
226*f4b3ec61Sdh 		sctp->sctp_sctps = NULL;
227*f4b3ec61Sdh 		SCTP_G_Q_REFRELE(sctps);
2287c478bd9Sstevel@tonic-gate 		kmem_cache_free(sctp_conn_cache, connp);
2297c478bd9Sstevel@tonic-gate 		return (NULL);
2307c478bd9Sstevel@tonic-gate 	}
23145916cd2Sjpk 
23245916cd2Sjpk 	/*
23345916cd2Sjpk 	 * If the parent is multilevel, then we'll fix up the remote cred
23445916cd2Sjpk 	 * when we do sctp_accept_comm.
23545916cd2Sjpk 	 */
23645916cd2Sjpk 	if ((credp = pconnp->conn_cred) != NULL) {
23745916cd2Sjpk 		connp->conn_cred = credp;
23845916cd2Sjpk 		crhold(credp);
23945916cd2Sjpk 		/*
24045916cd2Sjpk 		 * If the caller has the process-wide flag set, then default to
24145916cd2Sjpk 		 * MAC exempt mode.  This allows read-down to unlabeled hosts.
24245916cd2Sjpk 		 */
24345916cd2Sjpk 		if (getpflags(NET_MAC_AWARE, credp) != 0)
24445916cd2Sjpk 			connp->conn_mac_exempt = B_TRUE;
2457c478bd9Sstevel@tonic-gate 	}
2465d0bc3edSsommerfe 
2475d0bc3edSsommerfe 	connp->conn_allzones = pconnp->conn_allzones;
2485d0bc3edSsommerfe 	connp->conn_zoneid = pconnp->conn_zoneid;
2495d0bc3edSsommerfe 
2507c478bd9Sstevel@tonic-gate 	sctp->sctp_mss = psctp->sctp_mss;
2517c478bd9Sstevel@tonic-gate 	sctp->sctp_detached = B_TRUE;
2527c478bd9Sstevel@tonic-gate 	/*
2537c478bd9Sstevel@tonic-gate 	 * Link to the global as soon as possible so that this sctp_t
2547c478bd9Sstevel@tonic-gate 	 * can be found.
2557c478bd9Sstevel@tonic-gate 	 */
256*f4b3ec61Sdh 	SCTP_LINK(sctp, sctps);
2577c478bd9Sstevel@tonic-gate 
2587c478bd9Sstevel@tonic-gate 	return (sctp);
2597c478bd9Sstevel@tonic-gate }
2607c478bd9Sstevel@tonic-gate 
2617c478bd9Sstevel@tonic-gate /*
2627c478bd9Sstevel@tonic-gate  * We are dying for some reason.  Try to do it gracefully.
2637c478bd9Sstevel@tonic-gate  */
2647c478bd9Sstevel@tonic-gate void
2657c478bd9Sstevel@tonic-gate sctp_clean_death(sctp_t *sctp, int err)
2667c478bd9Sstevel@tonic-gate {
2677c478bd9Sstevel@tonic-gate 	ASSERT(sctp != NULL);
2687c478bd9Sstevel@tonic-gate 	ASSERT((sctp->sctp_family == AF_INET &&
2697c478bd9Sstevel@tonic-gate 	    sctp->sctp_ipversion == IPV4_VERSION) ||
2707c478bd9Sstevel@tonic-gate 	    (sctp->sctp_family == AF_INET6 &&
2717c478bd9Sstevel@tonic-gate 	    (sctp->sctp_ipversion == IPV4_VERSION ||
2727c478bd9Sstevel@tonic-gate 	    sctp->sctp_ipversion == IPV6_VERSION)));
2737c478bd9Sstevel@tonic-gate 
27445916cd2Sjpk 	dprint(3, ("sctp_clean_death %p, state %d\n", (void *)sctp,
27545916cd2Sjpk 	    sctp->sctp_state));
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate 	sctp->sctp_client_errno = err;
2787c478bd9Sstevel@tonic-gate 	/*
2797c478bd9Sstevel@tonic-gate 	 * Check to see if we need to notify upper layer.
2807c478bd9Sstevel@tonic-gate 	 */
2817c478bd9Sstevel@tonic-gate 	if ((sctp->sctp_state >= SCTPS_COOKIE_WAIT) &&
2827c478bd9Sstevel@tonic-gate 	    !SCTP_IS_DETACHED(sctp)) {
2837c478bd9Sstevel@tonic-gate 		if (sctp->sctp_xmit_head || sctp->sctp_xmit_unsent) {
2847c478bd9Sstevel@tonic-gate 			sctp_regift_xmitlist(sctp);
2857c478bd9Sstevel@tonic-gate 		}
2867c478bd9Sstevel@tonic-gate 		if (sctp->sctp_ulp_disconnected(sctp->sctp_ulpd, err)) {
2877c478bd9Sstevel@tonic-gate 			/*
2887c478bd9Sstevel@tonic-gate 			 * Socket is gone, detach.
2897c478bd9Sstevel@tonic-gate 			 */
2907c478bd9Sstevel@tonic-gate 			sctp->sctp_detached = B_TRUE;
2917c478bd9Sstevel@tonic-gate 			sctp->sctp_ulpd = NULL;
2927c478bd9Sstevel@tonic-gate 			bzero(&sctp->sctp_upcalls, sizeof (sctp_upcalls_t));
2937c478bd9Sstevel@tonic-gate 		}
2947c478bd9Sstevel@tonic-gate 	}
2957c478bd9Sstevel@tonic-gate 
2967c478bd9Sstevel@tonic-gate 	/* Remove this sctp from all hashes. */
2977c478bd9Sstevel@tonic-gate 	sctp_closei_local(sctp);
2987c478bd9Sstevel@tonic-gate 
2997c478bd9Sstevel@tonic-gate 	/*
3007c478bd9Sstevel@tonic-gate 	 * If the sctp_t is detached, we need to finish freeing up
3017c478bd9Sstevel@tonic-gate 	 * the resources.  At this point, ip_fanout_sctp() should have
3027c478bd9Sstevel@tonic-gate 	 * a hold on this sctp_t.  Some thread doing snmp stuff can
3037c478bd9Sstevel@tonic-gate 	 * have a hold.  And a taskq can also have a hold waiting to
3047c478bd9Sstevel@tonic-gate 	 * work.  sctp_unlink() the sctp_t from the global list so
3057c478bd9Sstevel@tonic-gate 	 * that no new thread can find it.  Then do a SCTP_REFRELE().
3067c478bd9Sstevel@tonic-gate 	 * The sctp_t will be freed after all those threads are done.
3077c478bd9Sstevel@tonic-gate 	 */
3087c478bd9Sstevel@tonic-gate 	if (SCTP_IS_DETACHED(sctp)) {
3097c478bd9Sstevel@tonic-gate 		SCTP_CONDEMNED(sctp);
3107c478bd9Sstevel@tonic-gate 		SCTP_REFRELE(sctp);
3117c478bd9Sstevel@tonic-gate 	}
3127c478bd9Sstevel@tonic-gate }
3137c478bd9Sstevel@tonic-gate 
3147c478bd9Sstevel@tonic-gate /*
3157c478bd9Sstevel@tonic-gate  * Called by upper layer when it wants to close this association.
3167c478bd9Sstevel@tonic-gate  * Depending on the state of this assoication, we need to do
3177c478bd9Sstevel@tonic-gate  * different things.
3187c478bd9Sstevel@tonic-gate  *
3197c478bd9Sstevel@tonic-gate  * If the state is below COOKIE_ECHOED or it is COOKIE_ECHOED but with
3207c478bd9Sstevel@tonic-gate  * no sent data, just remove this sctp from all the hashes.  This
3217c478bd9Sstevel@tonic-gate  * makes sure that all packets from the other end will go to the default
3227c478bd9Sstevel@tonic-gate  * sctp handling.  The upper layer will then do a sctp_close() to clean
3237c478bd9Sstevel@tonic-gate  * up.
3247c478bd9Sstevel@tonic-gate  *
3257c478bd9Sstevel@tonic-gate  * Otherwise, check and see if SO_LINGER is set.  If it is set, check
3267c478bd9Sstevel@tonic-gate  * the value.  If the value is 0, consider this an abortive close.  Send
3277c478bd9Sstevel@tonic-gate  * an ABORT message and kill the associatiion.
3287c478bd9Sstevel@tonic-gate  *
3297c478bd9Sstevel@tonic-gate  */
3307c478bd9Sstevel@tonic-gate int
3317c478bd9Sstevel@tonic-gate sctp_disconnect(sctp_t *sctp)
3327c478bd9Sstevel@tonic-gate {
3337c478bd9Sstevel@tonic-gate 	int	error = 0;
3347c478bd9Sstevel@tonic-gate 
33545916cd2Sjpk 	dprint(3, ("sctp_disconnect %p, state %d\n", (void *)sctp,
33645916cd2Sjpk 	    sctp->sctp_state));
3377c478bd9Sstevel@tonic-gate 
3387c478bd9Sstevel@tonic-gate 	RUN_SCTP(sctp);
3397c478bd9Sstevel@tonic-gate 
3407c478bd9Sstevel@tonic-gate 	switch (sctp->sctp_state) {
3417c478bd9Sstevel@tonic-gate 	case SCTPS_IDLE:
3427c478bd9Sstevel@tonic-gate 	case SCTPS_BOUND:
3437c478bd9Sstevel@tonic-gate 	case SCTPS_LISTEN:
3447c478bd9Sstevel@tonic-gate 		break;
3457c478bd9Sstevel@tonic-gate 	case SCTPS_COOKIE_WAIT:
3467c478bd9Sstevel@tonic-gate 	case SCTPS_COOKIE_ECHOED:
3477c478bd9Sstevel@tonic-gate 		/*
3487c478bd9Sstevel@tonic-gate 		 * Close during the connect 3-way handshake
3497c478bd9Sstevel@tonic-gate 		 * but here there may or may not be pending data
3507c478bd9Sstevel@tonic-gate 		 * already on queue. Process almost same as in
3517c478bd9Sstevel@tonic-gate 		 * the ESTABLISHED state.
3527c478bd9Sstevel@tonic-gate 		 */
3537c478bd9Sstevel@tonic-gate 		if (sctp->sctp_xmit_head == NULL &&
3547c478bd9Sstevel@tonic-gate 		    sctp->sctp_xmit_unsent == NULL) {
3557c478bd9Sstevel@tonic-gate 			break;
3567c478bd9Sstevel@tonic-gate 		}
3577c478bd9Sstevel@tonic-gate 		/* FALLTHRU */
3587c478bd9Sstevel@tonic-gate 	default:
3597c478bd9Sstevel@tonic-gate 		/*
3607c478bd9Sstevel@tonic-gate 		 * If SO_LINGER has set a zero linger time, abort the
3617c478bd9Sstevel@tonic-gate 		 * connection with a reset.
3627c478bd9Sstevel@tonic-gate 		 */
3637c478bd9Sstevel@tonic-gate 		if (sctp->sctp_linger && sctp->sctp_lingertime == 0) {
3647c478bd9Sstevel@tonic-gate 			sctp_user_abort(sctp, NULL, B_FALSE);
3657c478bd9Sstevel@tonic-gate 			break;
3667c478bd9Sstevel@tonic-gate 		}
3677c478bd9Sstevel@tonic-gate 
3681d8c4025Svi 		/*
3691d8c4025Svi 		 * In there is unread data, send an ABORT
3701d8c4025Svi 		 */
3711d8c4025Svi 		if (sctp->sctp_rxqueued > 0 || sctp->sctp_irwnd >
3721d8c4025Svi 		    sctp->sctp_rwnd) {
3731d8c4025Svi 			sctp_user_abort(sctp, NULL, B_FALSE);
3741d8c4025Svi 			break;
3751d8c4025Svi 		}
3767c478bd9Sstevel@tonic-gate 		/*
3777c478bd9Sstevel@tonic-gate 		 * Transmit the shutdown before detaching the sctp_t.
3787c478bd9Sstevel@tonic-gate 		 * After sctp_detach returns this queue/perimeter
3797c478bd9Sstevel@tonic-gate 		 * no longer owns the sctp_t thus others can modify it.
3807c478bd9Sstevel@tonic-gate 		 */
3817c478bd9Sstevel@tonic-gate 		sctp_send_shutdown(sctp, 0);
3827c478bd9Sstevel@tonic-gate 
3837c478bd9Sstevel@tonic-gate 		/* Pass gathered wisdom to IP for keeping */
38477c67f2fSkcpoon 		sctp_update_ire(sctp);
3857c478bd9Sstevel@tonic-gate 
3867c478bd9Sstevel@tonic-gate 		/*
3877c478bd9Sstevel@tonic-gate 		 * If lingering on close then wait until the shutdown
3887c478bd9Sstevel@tonic-gate 		 * is complete, or the SO_LINGER time passes, or an
3897c478bd9Sstevel@tonic-gate 		 * ABORT is sent/received.  Note that sctp_disconnect()
3907c478bd9Sstevel@tonic-gate 		 * can be called more than once.  Make sure that only
3917c478bd9Sstevel@tonic-gate 		 * one thread waits.
3927c478bd9Sstevel@tonic-gate 		 */
3937c478bd9Sstevel@tonic-gate 		if (sctp->sctp_linger && sctp->sctp_lingertime > 0 &&
3947c478bd9Sstevel@tonic-gate 		    sctp->sctp_state >= SCTPS_ESTABLISHED &&
3957c478bd9Sstevel@tonic-gate 		    !sctp->sctp_lingering) {
3967c478bd9Sstevel@tonic-gate 			clock_t stoptime;	/* in ticks */
3977c478bd9Sstevel@tonic-gate 			clock_t ret;
3987c478bd9Sstevel@tonic-gate 
3997c478bd9Sstevel@tonic-gate 			/*
4007c478bd9Sstevel@tonic-gate 			 * Process the sendq to send the SHUTDOWN out
4017c478bd9Sstevel@tonic-gate 			 * before waiting.
4027c478bd9Sstevel@tonic-gate 			 */
4037c478bd9Sstevel@tonic-gate 			sctp_process_sendq(sctp);
4047c478bd9Sstevel@tonic-gate 
4057c478bd9Sstevel@tonic-gate 			sctp->sctp_lingering = 1;
4067c478bd9Sstevel@tonic-gate 			sctp->sctp_client_errno = 0;
4077c478bd9Sstevel@tonic-gate 			stoptime = lbolt + sctp->sctp_lingertime;
4087c478bd9Sstevel@tonic-gate 
4097c478bd9Sstevel@tonic-gate 			mutex_enter(&sctp->sctp_lock);
4107c478bd9Sstevel@tonic-gate 			sctp->sctp_running = B_FALSE;
4117c478bd9Sstevel@tonic-gate 			while (sctp->sctp_state >= SCTPS_ESTABLISHED &&
4127c478bd9Sstevel@tonic-gate 			    sctp->sctp_client_errno == 0) {
4137c478bd9Sstevel@tonic-gate 				cv_broadcast(&sctp->sctp_cv);
4147c478bd9Sstevel@tonic-gate 				ret = cv_timedwait_sig(&sctp->sctp_cv,
4157c478bd9Sstevel@tonic-gate 				    &sctp->sctp_lock, stoptime);
4167c478bd9Sstevel@tonic-gate 				if (ret < 0) {
4177c478bd9Sstevel@tonic-gate 					/* Stoptime has reached. */
4187c478bd9Sstevel@tonic-gate 					sctp->sctp_client_errno = EWOULDBLOCK;
4197c478bd9Sstevel@tonic-gate 					break;
4207c478bd9Sstevel@tonic-gate 				} else if (ret == 0) {
4217c478bd9Sstevel@tonic-gate 					/* Got a signal. */
4227c478bd9Sstevel@tonic-gate 					break;
4237c478bd9Sstevel@tonic-gate 				}
4247c478bd9Sstevel@tonic-gate 			}
4257c478bd9Sstevel@tonic-gate 			error = sctp->sctp_client_errno;
4267c478bd9Sstevel@tonic-gate 			sctp->sctp_client_errno = 0;
4277c478bd9Sstevel@tonic-gate 			mutex_exit(&sctp->sctp_lock);
4287c478bd9Sstevel@tonic-gate 		}
4297c478bd9Sstevel@tonic-gate 
4307c478bd9Sstevel@tonic-gate 		WAKE_SCTP(sctp);
4317c478bd9Sstevel@tonic-gate 		sctp_process_sendq(sctp);
4327c478bd9Sstevel@tonic-gate 		return (error);
4337c478bd9Sstevel@tonic-gate 	}
4347c478bd9Sstevel@tonic-gate 
4357c478bd9Sstevel@tonic-gate 
4367c478bd9Sstevel@tonic-gate 	/* Remove this sctp from all hashes so nobody can find it. */
4377c478bd9Sstevel@tonic-gate 	sctp_closei_local(sctp);
4387c478bd9Sstevel@tonic-gate 	WAKE_SCTP(sctp);
4397c478bd9Sstevel@tonic-gate 	return (error);
4407c478bd9Sstevel@tonic-gate }
4417c478bd9Sstevel@tonic-gate 
4427c478bd9Sstevel@tonic-gate void
4437c478bd9Sstevel@tonic-gate sctp_close(sctp_t *sctp)
4447c478bd9Sstevel@tonic-gate {
44545916cd2Sjpk 	dprint(3, ("sctp_close %p, state %d\n", (void *)sctp,
44645916cd2Sjpk 	    sctp->sctp_state));
4477c478bd9Sstevel@tonic-gate 
4487c478bd9Sstevel@tonic-gate 	RUN_SCTP(sctp);
4497c478bd9Sstevel@tonic-gate 	sctp->sctp_detached = 1;
4507c478bd9Sstevel@tonic-gate 	sctp->sctp_ulpd = NULL;
4517c478bd9Sstevel@tonic-gate 	bzero(&sctp->sctp_upcalls, sizeof (sctp_upcalls_t));
4527c478bd9Sstevel@tonic-gate 	bzero(&sctp->sctp_events, sizeof (sctp->sctp_events));
4537c478bd9Sstevel@tonic-gate 
4547c478bd9Sstevel@tonic-gate 	/* If the graceful shutdown has not been completed, just return. */
4557c478bd9Sstevel@tonic-gate 	if (sctp->sctp_state != SCTPS_IDLE) {
4567c478bd9Sstevel@tonic-gate 		WAKE_SCTP(sctp);
4577c478bd9Sstevel@tonic-gate 		return;
4587c478bd9Sstevel@tonic-gate 	}
4597c478bd9Sstevel@tonic-gate 
4607c478bd9Sstevel@tonic-gate 	/*
4617c478bd9Sstevel@tonic-gate 	 * Since sctp_t is in SCTPS_IDLE state, so the only thread which
4627c478bd9Sstevel@tonic-gate 	 * can have a hold on the sctp_t is doing snmp stuff.  Just do
4637c478bd9Sstevel@tonic-gate 	 * a SCTP_REFRELE() here after the SCTP_UNLINK().  It will
4647c478bd9Sstevel@tonic-gate 	 * be freed when the other thread is done.
4657c478bd9Sstevel@tonic-gate 	 */
4667c478bd9Sstevel@tonic-gate 	SCTP_CONDEMNED(sctp);
4677c478bd9Sstevel@tonic-gate 	WAKE_SCTP(sctp);
4687c478bd9Sstevel@tonic-gate 	SCTP_REFRELE(sctp);
4697c478bd9Sstevel@tonic-gate }
4707c478bd9Sstevel@tonic-gate 
4717c478bd9Sstevel@tonic-gate /*
4727c478bd9Sstevel@tonic-gate  * Unlink from global list and do the eager close.
4737c478bd9Sstevel@tonic-gate  * Remove the refhold implicit in being on the global list.
4747c478bd9Sstevel@tonic-gate  */
4757c478bd9Sstevel@tonic-gate void
4767c478bd9Sstevel@tonic-gate sctp_close_eager(sctp_t *sctp)
4777c478bd9Sstevel@tonic-gate {
4787c478bd9Sstevel@tonic-gate 	SCTP_CONDEMNED(sctp);
4797c478bd9Sstevel@tonic-gate 	sctp_closei_local(sctp);
4807c478bd9Sstevel@tonic-gate 	SCTP_REFRELE(sctp);
4817c478bd9Sstevel@tonic-gate }
4827c478bd9Sstevel@tonic-gate 
4837c478bd9Sstevel@tonic-gate /*
4847c478bd9Sstevel@tonic-gate  * The sctp_t is going away. Remove it from all lists and set it
4857c478bd9Sstevel@tonic-gate  * to SCTPS_IDLE. The caller has to remove it from the
4867c478bd9Sstevel@tonic-gate  * global list. The freeing up of memory is deferred until
4877c478bd9Sstevel@tonic-gate  * sctp_free(). This is needed since a thread in sctp_input() might have
4887c478bd9Sstevel@tonic-gate  * done a SCTP_REFHOLD on this structure before it was removed from the
4897c478bd9Sstevel@tonic-gate  * hashes.
4907c478bd9Sstevel@tonic-gate  */
4917c478bd9Sstevel@tonic-gate static void
4927c478bd9Sstevel@tonic-gate sctp_closei_local(sctp_t *sctp)
4937c478bd9Sstevel@tonic-gate {
4947c478bd9Sstevel@tonic-gate 	mblk_t	*mp;
4957c478bd9Sstevel@tonic-gate 	ire_t	*ire = NULL;
4967c478bd9Sstevel@tonic-gate 	conn_t	*connp = sctp->sctp_connp;
4977c478bd9Sstevel@tonic-gate 
4987c478bd9Sstevel@tonic-gate 	/* Stop and free the timers */
4997c478bd9Sstevel@tonic-gate 	sctp_free_faddr_timers(sctp);
5007c478bd9Sstevel@tonic-gate 	if ((mp = sctp->sctp_heartbeat_mp) != NULL) {
5017c478bd9Sstevel@tonic-gate 		sctp_timer_free(mp);
5027c478bd9Sstevel@tonic-gate 		sctp->sctp_heartbeat_mp = NULL;
5037c478bd9Sstevel@tonic-gate 	}
5047c478bd9Sstevel@tonic-gate 	if ((mp = sctp->sctp_ack_mp) != NULL) {
5057c478bd9Sstevel@tonic-gate 		sctp_timer_free(mp);
5067c478bd9Sstevel@tonic-gate 		sctp->sctp_ack_mp = NULL;
5077c478bd9Sstevel@tonic-gate 	}
5087c478bd9Sstevel@tonic-gate 
5097c478bd9Sstevel@tonic-gate 	/* Set the CONN_CLOSING flag so that IP will not cache IRE again. */
5107c478bd9Sstevel@tonic-gate 	mutex_enter(&connp->conn_lock);
5117c478bd9Sstevel@tonic-gate 	connp->conn_state_flags |= CONN_CLOSING;
5127c478bd9Sstevel@tonic-gate 	ire = connp->conn_ire_cache;
5137c478bd9Sstevel@tonic-gate 	connp->conn_ire_cache = NULL;
5147c478bd9Sstevel@tonic-gate 	mutex_exit(&connp->conn_lock);
5157c478bd9Sstevel@tonic-gate 	if (ire != NULL)
5167c478bd9Sstevel@tonic-gate 		IRE_REFRELE_NOTR(ire);
5177c478bd9Sstevel@tonic-gate 
5187c478bd9Sstevel@tonic-gate 	/* Remove from all hashes. */
5197c478bd9Sstevel@tonic-gate 	sctp_bind_hash_remove(sctp);
5207c478bd9Sstevel@tonic-gate 	sctp_conn_hash_remove(sctp);
5217c478bd9Sstevel@tonic-gate 	sctp_listen_hash_remove(sctp);
5227c478bd9Sstevel@tonic-gate 	sctp->sctp_state = SCTPS_IDLE;
5237c478bd9Sstevel@tonic-gate 
5247c478bd9Sstevel@tonic-gate 	/*
5257c478bd9Sstevel@tonic-gate 	 * Clean up the recvq as much as possible.  All those packets
5267c478bd9Sstevel@tonic-gate 	 * will be silently dropped as this sctp_t is now in idle state.
5277c478bd9Sstevel@tonic-gate 	 */
5287c478bd9Sstevel@tonic-gate 	mutex_enter(&sctp->sctp_recvq_lock);
5297c478bd9Sstevel@tonic-gate 	while ((mp = sctp->sctp_recvq) != NULL) {
5307c478bd9Sstevel@tonic-gate 		mblk_t *ipsec_mp;
5317c478bd9Sstevel@tonic-gate 
5327c478bd9Sstevel@tonic-gate 		sctp->sctp_recvq = mp->b_next;
5337c478bd9Sstevel@tonic-gate 		mp->b_next = NULL;
5347c478bd9Sstevel@tonic-gate 		if ((ipsec_mp = mp->b_prev) != NULL) {
5357c478bd9Sstevel@tonic-gate 			freeb(ipsec_mp);
5367c478bd9Sstevel@tonic-gate 			mp->b_prev = NULL;
5377c478bd9Sstevel@tonic-gate 		}
5387c478bd9Sstevel@tonic-gate 		freemsg(mp);
5397c478bd9Sstevel@tonic-gate 	}
5407c478bd9Sstevel@tonic-gate 	mutex_exit(&sctp->sctp_recvq_lock);
5417c478bd9Sstevel@tonic-gate }
5427c478bd9Sstevel@tonic-gate 
5437c478bd9Sstevel@tonic-gate /*
5447c478bd9Sstevel@tonic-gate  * Free memory associated with the sctp/ip header template.
5457c478bd9Sstevel@tonic-gate  */
5467c478bd9Sstevel@tonic-gate static void
5477c478bd9Sstevel@tonic-gate sctp_headers_free(sctp_t *sctp)
5487c478bd9Sstevel@tonic-gate {
5497c478bd9Sstevel@tonic-gate 	if (sctp->sctp_iphc != NULL) {
5507c478bd9Sstevel@tonic-gate 		kmem_free(sctp->sctp_iphc, sctp->sctp_iphc_len);
5517c478bd9Sstevel@tonic-gate 		sctp->sctp_iphc = NULL;
5527c478bd9Sstevel@tonic-gate 		sctp->sctp_ipha = NULL;
5537c478bd9Sstevel@tonic-gate 		sctp->sctp_hdr_len = 0;
5547c478bd9Sstevel@tonic-gate 		sctp->sctp_ip_hdr_len = 0;
5557c478bd9Sstevel@tonic-gate 		sctp->sctp_iphc_len = 0;
5567c478bd9Sstevel@tonic-gate 		sctp->sctp_sctph = NULL;
5577c478bd9Sstevel@tonic-gate 		sctp->sctp_hdr_len = 0;
5587c478bd9Sstevel@tonic-gate 	}
5597c478bd9Sstevel@tonic-gate 	if (sctp->sctp_iphc6 != NULL) {
5607c478bd9Sstevel@tonic-gate 		kmem_free(sctp->sctp_iphc6, sctp->sctp_iphc6_len);
5617c478bd9Sstevel@tonic-gate 		sctp->sctp_iphc6 = NULL;
5627c478bd9Sstevel@tonic-gate 		sctp->sctp_ip6h = NULL;
5637c478bd9Sstevel@tonic-gate 		sctp->sctp_hdr6_len = 0;
5647c478bd9Sstevel@tonic-gate 		sctp->sctp_ip_hdr6_len = 0;
5657c478bd9Sstevel@tonic-gate 		sctp->sctp_iphc6_len = 0;
5667c478bd9Sstevel@tonic-gate 		sctp->sctp_sctph6 = NULL;
5677c478bd9Sstevel@tonic-gate 		sctp->sctp_hdr6_len = 0;
5687c478bd9Sstevel@tonic-gate 	}
5697c478bd9Sstevel@tonic-gate }
5707c478bd9Sstevel@tonic-gate 
5717c478bd9Sstevel@tonic-gate static void
5727c478bd9Sstevel@tonic-gate sctp_free_xmit_data(sctp_t *sctp)
5737c478bd9Sstevel@tonic-gate {
5747c478bd9Sstevel@tonic-gate 	mblk_t	*ump = NULL;
5757c478bd9Sstevel@tonic-gate 	mblk_t	*nump;
5767c478bd9Sstevel@tonic-gate 	mblk_t	*mp;
5777c478bd9Sstevel@tonic-gate 	mblk_t	*nmp;
5787c478bd9Sstevel@tonic-gate 
5797c478bd9Sstevel@tonic-gate 	sctp->sctp_xmit_unacked = NULL;
5807c478bd9Sstevel@tonic-gate 	ump = sctp->sctp_xmit_head;
5817c478bd9Sstevel@tonic-gate 	sctp->sctp_xmit_tail = sctp->sctp_xmit_head = NULL;
5827c478bd9Sstevel@tonic-gate free_unsent:
5837c478bd9Sstevel@tonic-gate 	for (; ump != NULL; ump = nump) {
5847c478bd9Sstevel@tonic-gate 		for (mp = ump->b_cont; mp != NULL; mp = nmp) {
5857c478bd9Sstevel@tonic-gate 			nmp = mp->b_next;
5867c478bd9Sstevel@tonic-gate 			mp->b_next = NULL;
5877c478bd9Sstevel@tonic-gate 			mp->b_prev = NULL;
5887c478bd9Sstevel@tonic-gate 			freemsg(mp);
5897c478bd9Sstevel@tonic-gate 		}
5907c478bd9Sstevel@tonic-gate 		ASSERT(DB_REF(ump) == 1);
5917c478bd9Sstevel@tonic-gate 		nump = ump->b_next;
5927c478bd9Sstevel@tonic-gate 		ump->b_next = NULL;
5937c478bd9Sstevel@tonic-gate 		ump->b_prev = NULL;
5947c478bd9Sstevel@tonic-gate 		ump->b_cont = NULL;
5957c478bd9Sstevel@tonic-gate 		freeb(ump);
5967c478bd9Sstevel@tonic-gate 	}
5977c478bd9Sstevel@tonic-gate 	if ((ump = sctp->sctp_xmit_unsent) == NULL) {
5987c478bd9Sstevel@tonic-gate 		ASSERT(sctp->sctp_xmit_unsent_tail == NULL);
5997c478bd9Sstevel@tonic-gate 		return;
6007c478bd9Sstevel@tonic-gate 	}
6017c478bd9Sstevel@tonic-gate 	sctp->sctp_xmit_unsent = sctp->sctp_xmit_unsent_tail = NULL;
6027c478bd9Sstevel@tonic-gate 	goto free_unsent;
6037c478bd9Sstevel@tonic-gate }
6047c478bd9Sstevel@tonic-gate 
6057c478bd9Sstevel@tonic-gate /*
6067c478bd9Sstevel@tonic-gate  * Cleanup all the messages in the stream queue and the reassembly lists.
6077c478bd9Sstevel@tonic-gate  * If 'free' is true, then delete the streams as well.
6087c478bd9Sstevel@tonic-gate  */
6097c478bd9Sstevel@tonic-gate void
6107c478bd9Sstevel@tonic-gate sctp_instream_cleanup(sctp_t *sctp, boolean_t free)
6117c478bd9Sstevel@tonic-gate {
6127c478bd9Sstevel@tonic-gate 	int	i;
6137c478bd9Sstevel@tonic-gate 	mblk_t	*mp;
6147c478bd9Sstevel@tonic-gate 	mblk_t	*mp1;
6157c478bd9Sstevel@tonic-gate 
6167c478bd9Sstevel@tonic-gate 	if (sctp->sctp_instr != NULL) {
6177c478bd9Sstevel@tonic-gate 		/* walk thru and flush out anything remaining in the Q */
6187c478bd9Sstevel@tonic-gate 		for (i = 0; i < sctp->sctp_num_istr; i++) {
6197c478bd9Sstevel@tonic-gate 			mp = sctp->sctp_instr[i].istr_msgs;
6207c478bd9Sstevel@tonic-gate 			while (mp != NULL) {
6217c478bd9Sstevel@tonic-gate 				mp1 = mp->b_next;
6227c478bd9Sstevel@tonic-gate 				mp->b_next = mp->b_prev = NULL;
6237c478bd9Sstevel@tonic-gate 				freemsg(mp);
6247c478bd9Sstevel@tonic-gate 				mp = mp1;
6257c478bd9Sstevel@tonic-gate 			}
6267c478bd9Sstevel@tonic-gate 			sctp->sctp_instr[i].istr_msgs = NULL;
6277c478bd9Sstevel@tonic-gate 			sctp_free_reass((sctp->sctp_instr) + i);
6287c478bd9Sstevel@tonic-gate 			sctp->sctp_instr[i].nextseq = 0;
6297c478bd9Sstevel@tonic-gate 		}
6307c478bd9Sstevel@tonic-gate 		if (free) {
6317c478bd9Sstevel@tonic-gate 			kmem_free(sctp->sctp_instr,
6327c478bd9Sstevel@tonic-gate 			    sizeof (*sctp->sctp_instr) * sctp->sctp_num_istr);
6337c478bd9Sstevel@tonic-gate 			sctp->sctp_instr = NULL;
6347c478bd9Sstevel@tonic-gate 			sctp->sctp_num_istr = 0;
6357c478bd9Sstevel@tonic-gate 		}
6367c478bd9Sstevel@tonic-gate 	}
6377c478bd9Sstevel@tonic-gate 	/* un-ordered fragments */
6387c478bd9Sstevel@tonic-gate 	if (sctp->sctp_uo_frags != NULL) {
6397c478bd9Sstevel@tonic-gate 		for (mp = sctp->sctp_uo_frags; mp != NULL; mp = mp1) {
6407c478bd9Sstevel@tonic-gate 			mp1 = mp->b_next;
6417c478bd9Sstevel@tonic-gate 			mp->b_next = mp->b_prev = NULL;
6427c478bd9Sstevel@tonic-gate 			freemsg(mp);
6437c478bd9Sstevel@tonic-gate 		}
6447c478bd9Sstevel@tonic-gate 	}
6457c478bd9Sstevel@tonic-gate }
6467c478bd9Sstevel@tonic-gate 
6477c478bd9Sstevel@tonic-gate /*
6487c478bd9Sstevel@tonic-gate  * Last reference to the sctp_t is gone. Free all memory associated with it.
6497c478bd9Sstevel@tonic-gate  * Called from SCTP_REFRELE. Called inline in sctp_close()
6507c478bd9Sstevel@tonic-gate  */
6517c478bd9Sstevel@tonic-gate void
6527c478bd9Sstevel@tonic-gate sctp_free(conn_t *connp)
6537c478bd9Sstevel@tonic-gate {
6547c478bd9Sstevel@tonic-gate 	sctp_t *sctp = CONN2SCTP(connp);
6557c478bd9Sstevel@tonic-gate 	int		cnt;
656*f4b3ec61Sdh 	sctp_stack_t	*sctps = sctp->sctp_sctps;
657*f4b3ec61Sdh 	netstack_t	*ns;
6587c478bd9Sstevel@tonic-gate 
659*f4b3ec61Sdh 	ASSERT(sctps != NULL);
6607c478bd9Sstevel@tonic-gate 	/* Unlink it from the global list */
661*f4b3ec61Sdh 	SCTP_UNLINK(sctp, sctps);
6627c478bd9Sstevel@tonic-gate 
6637c478bd9Sstevel@tonic-gate 	ASSERT(connp->conn_ref == 0);
6647c478bd9Sstevel@tonic-gate 	ASSERT(connp->conn_ulp == IPPROTO_SCTP);
6657c478bd9Sstevel@tonic-gate 	ASSERT(!MUTEX_HELD(&sctp->sctp_reflock));
6667c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_refcnt == 0);
6677c478bd9Sstevel@tonic-gate 
6687c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_ptpbhn == NULL && sctp->sctp_bind_hash == NULL);
6697c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_conn_hash_next == NULL &&
6707c478bd9Sstevel@tonic-gate 	    sctp->sctp_conn_hash_prev == NULL);
6717c478bd9Sstevel@tonic-gate 
6727c478bd9Sstevel@tonic-gate 
6737c478bd9Sstevel@tonic-gate 	/* Free up all the resources. */
6747c478bd9Sstevel@tonic-gate 
6757c478bd9Sstevel@tonic-gate 	/* blow away sctp stream management */
6767c478bd9Sstevel@tonic-gate 	if (sctp->sctp_ostrcntrs != NULL) {
6777c478bd9Sstevel@tonic-gate 		kmem_free(sctp->sctp_ostrcntrs,
6787c478bd9Sstevel@tonic-gate 		    sizeof (uint16_t) * sctp->sctp_num_ostr);
6797c478bd9Sstevel@tonic-gate 		sctp->sctp_ostrcntrs = NULL;
6807c478bd9Sstevel@tonic-gate 	}
6817c478bd9Sstevel@tonic-gate 	sctp_instream_cleanup(sctp, B_TRUE);
6827c478bd9Sstevel@tonic-gate 
6837c478bd9Sstevel@tonic-gate 	/* Remove all data transfer resources. */
6847c478bd9Sstevel@tonic-gate 	sctp->sctp_istr_nmsgs = 0;
6857c478bd9Sstevel@tonic-gate 	sctp->sctp_rxqueued = 0;
6867c478bd9Sstevel@tonic-gate 	sctp_free_xmit_data(sctp);
6877c478bd9Sstevel@tonic-gate 	sctp->sctp_unacked = 0;
6887c478bd9Sstevel@tonic-gate 	sctp->sctp_unsent = 0;
6891d8c4025Svi 	if (sctp->sctp_cxmit_list != NULL)
6901d8c4025Svi 		sctp_asconf_free_cxmit(sctp, NULL);
6911d8c4025Svi 
6927c478bd9Sstevel@tonic-gate 	sctp->sctp_lastdata = NULL;
6937c478bd9Sstevel@tonic-gate 
6947c478bd9Sstevel@tonic-gate 	/* Clear out default xmit settings */
6957c478bd9Sstevel@tonic-gate 	sctp->sctp_def_stream = 0;
6967c478bd9Sstevel@tonic-gate 	sctp->sctp_def_flags = 0;
6977c478bd9Sstevel@tonic-gate 	sctp->sctp_def_ppid = 0;
6987c478bd9Sstevel@tonic-gate 	sctp->sctp_def_context = 0;
6997c478bd9Sstevel@tonic-gate 	sctp->sctp_def_timetolive = 0;
7007c478bd9Sstevel@tonic-gate 
7017c478bd9Sstevel@tonic-gate 	if (sctp->sctp_sack_info != NULL) {
7027c478bd9Sstevel@tonic-gate 		sctp_free_set(sctp->sctp_sack_info);
7037c478bd9Sstevel@tonic-gate 		sctp->sctp_sack_info = NULL;
7047c478bd9Sstevel@tonic-gate 	}
7057c478bd9Sstevel@tonic-gate 	sctp->sctp_sack_gaps = 0;
7067c478bd9Sstevel@tonic-gate 
7077c478bd9Sstevel@tonic-gate 	if (sctp->sctp_cookie_mp != NULL) {
7087c478bd9Sstevel@tonic-gate 		freemsg(sctp->sctp_cookie_mp);
7097c478bd9Sstevel@tonic-gate 		sctp->sctp_cookie_mp = NULL;
7107c478bd9Sstevel@tonic-gate 	}
7117c478bd9Sstevel@tonic-gate 
7127c478bd9Sstevel@tonic-gate 	/* Remove all the address resources. */
7137c478bd9Sstevel@tonic-gate 	sctp_zap_addrs(sctp);
7147c478bd9Sstevel@tonic-gate 	for (cnt = 0; cnt < SCTP_IPIF_HASH; cnt++) {
7157c478bd9Sstevel@tonic-gate 		ASSERT(sctp->sctp_saddrs[cnt].ipif_count == 0);
7167c478bd9Sstevel@tonic-gate 		list_destroy(&sctp->sctp_saddrs[cnt].sctp_ipif_list);
7177c478bd9Sstevel@tonic-gate 	}
7187c478bd9Sstevel@tonic-gate 
71945916cd2Sjpk 	ip6_pkt_free(&sctp->sctp_sticky_ipp);
7207c478bd9Sstevel@tonic-gate 
7217c478bd9Sstevel@tonic-gate 	if (sctp->sctp_hopopts != NULL) {
7227c478bd9Sstevel@tonic-gate 		mi_free(sctp->sctp_hopopts);
7237c478bd9Sstevel@tonic-gate 		sctp->sctp_hopopts = NULL;
7247c478bd9Sstevel@tonic-gate 		sctp->sctp_hopoptslen = 0;
7257c478bd9Sstevel@tonic-gate 	}
7267c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_hopoptslen == 0);
7277c478bd9Sstevel@tonic-gate 	if (sctp->sctp_dstopts != NULL) {
7287c478bd9Sstevel@tonic-gate 		mi_free(sctp->sctp_dstopts);
7297c478bd9Sstevel@tonic-gate 		sctp->sctp_dstopts = NULL;
7307c478bd9Sstevel@tonic-gate 		sctp->sctp_dstoptslen = 0;
7317c478bd9Sstevel@tonic-gate 	}
7327c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_dstoptslen == 0);
7337c478bd9Sstevel@tonic-gate 	if (sctp->sctp_rtdstopts != NULL) {
7347c478bd9Sstevel@tonic-gate 		mi_free(sctp->sctp_rtdstopts);
7357c478bd9Sstevel@tonic-gate 		sctp->sctp_rtdstopts = NULL;
7367c478bd9Sstevel@tonic-gate 		sctp->sctp_rtdstoptslen = 0;
7377c478bd9Sstevel@tonic-gate 	}
7387c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_rtdstoptslen == 0);
7397c478bd9Sstevel@tonic-gate 	if (sctp->sctp_rthdr != NULL) {
7407c478bd9Sstevel@tonic-gate 		mi_free(sctp->sctp_rthdr);
7417c478bd9Sstevel@tonic-gate 		sctp->sctp_rthdr = NULL;
7427c478bd9Sstevel@tonic-gate 		sctp->sctp_rthdrlen = 0;
7437c478bd9Sstevel@tonic-gate 	}
7447c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_rthdrlen == 0);
7457c478bd9Sstevel@tonic-gate 	sctp_headers_free(sctp);
7467c478bd9Sstevel@tonic-gate 
7477c478bd9Sstevel@tonic-gate 	sctp->sctp_shutdown_faddr = NULL;
7487c478bd9Sstevel@tonic-gate 
7497c478bd9Sstevel@tonic-gate 	/* Clear all the bitfields. */
7507c478bd9Sstevel@tonic-gate 	bzero(&sctp->sctp_bits, sizeof (sctp->sctp_bits));
7517c478bd9Sstevel@tonic-gate 
7527c478bd9Sstevel@tonic-gate 	/* It is time to update the global statistics. */
753*f4b3ec61Sdh 	UPDATE_MIB(&sctps->sctps_mib, sctpOutSCTPPkts, sctp->sctp_opkts);
754*f4b3ec61Sdh 	UPDATE_MIB(&sctps->sctps_mib, sctpOutCtrlChunks, sctp->sctp_obchunks);
755*f4b3ec61Sdh 	UPDATE_MIB(&sctps->sctps_mib, sctpOutOrderChunks, sctp->sctp_odchunks);
756*f4b3ec61Sdh 	UPDATE_MIB(&sctps->sctps_mib,
757*f4b3ec61Sdh 	    sctpOutUnorderChunks, sctp->sctp_oudchunks);
758*f4b3ec61Sdh 	UPDATE_MIB(&sctps->sctps_mib, sctpRetransChunks, sctp->sctp_rxtchunks);
759*f4b3ec61Sdh 	UPDATE_MIB(&sctps->sctps_mib, sctpInSCTPPkts, sctp->sctp_ipkts);
760*f4b3ec61Sdh 	UPDATE_MIB(&sctps->sctps_mib, sctpInCtrlChunks, sctp->sctp_ibchunks);
761*f4b3ec61Sdh 	UPDATE_MIB(&sctps->sctps_mib, sctpInOrderChunks, sctp->sctp_idchunks);
762*f4b3ec61Sdh 	UPDATE_MIB(&sctps->sctps_mib,
763*f4b3ec61Sdh 	    sctpInUnorderChunks, sctp->sctp_iudchunks);
764*f4b3ec61Sdh 	UPDATE_MIB(&sctps->sctps_mib, sctpFragUsrMsgs, sctp->sctp_fragdmsgs);
765*f4b3ec61Sdh 	UPDATE_MIB(&sctps->sctps_mib, sctpReasmUsrMsgs, sctp->sctp_reassmsgs);
7667c478bd9Sstevel@tonic-gate 	sctp->sctp_opkts = 0;
7677c478bd9Sstevel@tonic-gate 	sctp->sctp_obchunks = 0;
7687c478bd9Sstevel@tonic-gate 	sctp->sctp_odchunks = 0;
7697c478bd9Sstevel@tonic-gate 	sctp->sctp_oudchunks = 0;
7707c478bd9Sstevel@tonic-gate 	sctp->sctp_rxtchunks = 0;
7717c478bd9Sstevel@tonic-gate 	sctp->sctp_ipkts = 0;
7727c478bd9Sstevel@tonic-gate 	sctp->sctp_ibchunks = 0;
7737c478bd9Sstevel@tonic-gate 	sctp->sctp_idchunks = 0;
7747c478bd9Sstevel@tonic-gate 	sctp->sctp_iudchunks = 0;
7757c478bd9Sstevel@tonic-gate 	sctp->sctp_fragdmsgs = 0;
7767c478bd9Sstevel@tonic-gate 	sctp->sctp_reassmsgs = 0;
7777c478bd9Sstevel@tonic-gate 
7787c478bd9Sstevel@tonic-gate 	sctp->sctp_autoclose = 0;
7797c478bd9Sstevel@tonic-gate 	sctp->sctp_tx_adaption_code = 0;
7807c478bd9Sstevel@tonic-gate 
7817f99e6fcSkp 	sctp->sctp_v6label_len = 0;
7827f99e6fcSkp 	sctp->sctp_v4label_len = 0;
7837f99e6fcSkp 
7847c478bd9Sstevel@tonic-gate 	/* Clean up conn_t stuff */
7857c478bd9Sstevel@tonic-gate 	connp->conn_policy_cached = B_FALSE;
7867c478bd9Sstevel@tonic-gate 	if (connp->conn_latch != NULL) {
787*f4b3ec61Sdh 		IPLATCH_REFRELE(connp->conn_latch, connp->conn_netstack);
7887c478bd9Sstevel@tonic-gate 		connp->conn_latch = NULL;
7897c478bd9Sstevel@tonic-gate 	}
7907c478bd9Sstevel@tonic-gate 	if (connp->conn_policy != NULL) {
791*f4b3ec61Sdh 		IPPH_REFRELE(connp->conn_policy, connp->conn_netstack);
7927c478bd9Sstevel@tonic-gate 		connp->conn_policy = NULL;
7937c478bd9Sstevel@tonic-gate 	}
7947c478bd9Sstevel@tonic-gate 	if (connp->conn_ipsec_opt_mp != NULL) {
7957c478bd9Sstevel@tonic-gate 		freemsg(connp->conn_ipsec_opt_mp);
7967c478bd9Sstevel@tonic-gate 		connp->conn_ipsec_opt_mp = NULL;
7977c478bd9Sstevel@tonic-gate 	}
7987c478bd9Sstevel@tonic-gate 	if (connp->conn_cred != NULL) {
7997c478bd9Sstevel@tonic-gate 		crfree(connp->conn_cred);
8007c478bd9Sstevel@tonic-gate 		connp->conn_cred = NULL;
8017c478bd9Sstevel@tonic-gate 	}
8027c478bd9Sstevel@tonic-gate 
803*f4b3ec61Sdh 	/* Every sctp_t holds one reference on the default queue */
804*f4b3ec61Sdh 	sctp->sctp_sctps = NULL;
805*f4b3ec61Sdh 	SCTP_G_Q_REFRELE(sctps);
806*f4b3ec61Sdh 
807*f4b3ec61Sdh 	ns = connp->conn_netstack;
808*f4b3ec61Sdh 	connp->conn_netstack = NULL;
809*f4b3ec61Sdh 	netstack_rele(ns);
8107c478bd9Sstevel@tonic-gate 	kmem_cache_free(sctp_conn_cache, connp);
8117c478bd9Sstevel@tonic-gate }
8127c478bd9Sstevel@tonic-gate 
8137c478bd9Sstevel@tonic-gate /* Diagnostic routine used to return a string associated with the sctp state. */
8147c478bd9Sstevel@tonic-gate char *
8157c478bd9Sstevel@tonic-gate sctp_display(sctp_t *sctp, char *sup_buf)
8167c478bd9Sstevel@tonic-gate {
8177c478bd9Sstevel@tonic-gate 	char	*buf;
8187c478bd9Sstevel@tonic-gate 	char	buf1[30];
8197c478bd9Sstevel@tonic-gate 	static char	priv_buf[INET6_ADDRSTRLEN * 2 + 80];
8207c478bd9Sstevel@tonic-gate 	char	*cp;
8217c478bd9Sstevel@tonic-gate 
8227c478bd9Sstevel@tonic-gate 	if (sctp == NULL)
8237c478bd9Sstevel@tonic-gate 		return ("NULL_SCTP");
8247c478bd9Sstevel@tonic-gate 
8257c478bd9Sstevel@tonic-gate 	buf = (sup_buf != NULL) ? sup_buf : priv_buf;
8267c478bd9Sstevel@tonic-gate 
8277c478bd9Sstevel@tonic-gate 	switch (sctp->sctp_state) {
8287c478bd9Sstevel@tonic-gate 	case SCTPS_IDLE:
8297c478bd9Sstevel@tonic-gate 		cp = "SCTP_IDLE";
8307c478bd9Sstevel@tonic-gate 		break;
8317c478bd9Sstevel@tonic-gate 	case SCTPS_BOUND:
8327c478bd9Sstevel@tonic-gate 		cp = "SCTP_BOUND";
8337c478bd9Sstevel@tonic-gate 		break;
8347c478bd9Sstevel@tonic-gate 	case SCTPS_LISTEN:
8357c478bd9Sstevel@tonic-gate 		cp = "SCTP_LISTEN";
8367c478bd9Sstevel@tonic-gate 		break;
8377c478bd9Sstevel@tonic-gate 	case SCTPS_COOKIE_WAIT:
8387c478bd9Sstevel@tonic-gate 		cp = "SCTP_COOKIE_WAIT";
8397c478bd9Sstevel@tonic-gate 		break;
8407c478bd9Sstevel@tonic-gate 	case SCTPS_COOKIE_ECHOED:
8417c478bd9Sstevel@tonic-gate 		cp = "SCTP_COOKIE_ECHOED";
8427c478bd9Sstevel@tonic-gate 		break;
8437c478bd9Sstevel@tonic-gate 	case SCTPS_ESTABLISHED:
8447c478bd9Sstevel@tonic-gate 		cp = "SCTP_ESTABLISHED";
8457c478bd9Sstevel@tonic-gate 		break;
8467c478bd9Sstevel@tonic-gate 	case SCTPS_SHUTDOWN_PENDING:
8477c478bd9Sstevel@tonic-gate 		cp = "SCTP_SHUTDOWN_PENDING";
8487c478bd9Sstevel@tonic-gate 		break;
8497c478bd9Sstevel@tonic-gate 	case SCTPS_SHUTDOWN_SENT:
8507c478bd9Sstevel@tonic-gate 		cp = "SCTPS_SHUTDOWN_SENT";
8517c478bd9Sstevel@tonic-gate 		break;
8527c478bd9Sstevel@tonic-gate 	case SCTPS_SHUTDOWN_RECEIVED:
8537c478bd9Sstevel@tonic-gate 		cp = "SCTPS_SHUTDOWN_RECEIVED";
8547c478bd9Sstevel@tonic-gate 		break;
8557c478bd9Sstevel@tonic-gate 	case SCTPS_SHUTDOWN_ACK_SENT:
8567c478bd9Sstevel@tonic-gate 		cp = "SCTPS_SHUTDOWN_ACK_SENT";
8577c478bd9Sstevel@tonic-gate 		break;
8587c478bd9Sstevel@tonic-gate 	default:
8597c478bd9Sstevel@tonic-gate 		(void) mi_sprintf(buf1, "SCTPUnkState(%d)", sctp->sctp_state);
8607c478bd9Sstevel@tonic-gate 		cp = buf1;
8617c478bd9Sstevel@tonic-gate 		break;
8627c478bd9Sstevel@tonic-gate 	}
8637c478bd9Sstevel@tonic-gate 	(void) mi_sprintf(buf, "[%u, %u] %s",
8647c478bd9Sstevel@tonic-gate 	    ntohs(sctp->sctp_lport), ntohs(sctp->sctp_fport), cp);
8657c478bd9Sstevel@tonic-gate 
8667c478bd9Sstevel@tonic-gate 	return (buf);
8677c478bd9Sstevel@tonic-gate }
8687c478bd9Sstevel@tonic-gate 
8697c478bd9Sstevel@tonic-gate /*
8707c478bd9Sstevel@tonic-gate  * Initialize protocol control block. If a parent exists, inherit
8717c478bd9Sstevel@tonic-gate  * all values set through setsockopt().
8727c478bd9Sstevel@tonic-gate  */
8737c478bd9Sstevel@tonic-gate static int
8747c478bd9Sstevel@tonic-gate sctp_init_values(sctp_t *sctp, sctp_t *psctp, int sleep)
8757c478bd9Sstevel@tonic-gate {
8767c478bd9Sstevel@tonic-gate 	int	err;
8777c478bd9Sstevel@tonic-gate 	int	cnt;
878*f4b3ec61Sdh 	sctp_stack_t	*sctps = sctp->sctp_sctps;
8795d0bc3edSsommerfe 	conn_t 	*connp, *pconnp;
8807c478bd9Sstevel@tonic-gate 
8817c478bd9Sstevel@tonic-gate 	ASSERT((sctp->sctp_family == AF_INET &&
8827c478bd9Sstevel@tonic-gate 	    sctp->sctp_ipversion == IPV4_VERSION) ||
8837c478bd9Sstevel@tonic-gate 	    (sctp->sctp_family == AF_INET6 &&
8847c478bd9Sstevel@tonic-gate 	    (sctp->sctp_ipversion == IPV4_VERSION ||
8857c478bd9Sstevel@tonic-gate 	    sctp->sctp_ipversion == IPV6_VERSION)));
8867c478bd9Sstevel@tonic-gate 
8877c478bd9Sstevel@tonic-gate 	sctp->sctp_nsaddrs = 0;
8887c478bd9Sstevel@tonic-gate 	for (cnt = 0; cnt < SCTP_IPIF_HASH; cnt++) {
8897c478bd9Sstevel@tonic-gate 		sctp->sctp_saddrs[cnt].ipif_count = 0;
8907c478bd9Sstevel@tonic-gate 		list_create(&sctp->sctp_saddrs[cnt].sctp_ipif_list,
8917c478bd9Sstevel@tonic-gate 		    sizeof (sctp_saddr_ipif_t), offsetof(sctp_saddr_ipif_t,
8927c478bd9Sstevel@tonic-gate 		    saddr_ipif));
8937c478bd9Sstevel@tonic-gate 	}
8947c478bd9Sstevel@tonic-gate 	sctp->sctp_ports = 0;
8957c478bd9Sstevel@tonic-gate 	sctp->sctp_running = B_FALSE;
8967c478bd9Sstevel@tonic-gate 	sctp->sctp_state = SCTPS_IDLE;
8977c478bd9Sstevel@tonic-gate 
8987c478bd9Sstevel@tonic-gate 	sctp->sctp_refcnt = 1;
8997c478bd9Sstevel@tonic-gate 
9007c478bd9Sstevel@tonic-gate 	sctp->sctp_strikes = 0;
9017c478bd9Sstevel@tonic-gate 
9027c478bd9Sstevel@tonic-gate 	sctp->sctp_last_mtu_probe = lbolt64;
903*f4b3ec61Sdh 	sctp->sctp_mtu_probe_intvl = sctps->sctps_mtu_probe_interval;
9047c478bd9Sstevel@tonic-gate 
9057c478bd9Sstevel@tonic-gate 	sctp->sctp_sack_gaps = 0;
9067c478bd9Sstevel@tonic-gate 	sctp->sctp_sack_toggle = 2;
9077c478bd9Sstevel@tonic-gate 
9087c478bd9Sstevel@tonic-gate 	if (psctp != NULL) {
9097c478bd9Sstevel@tonic-gate 		/*
9107c478bd9Sstevel@tonic-gate 		 * Inherit from parent
9117c478bd9Sstevel@tonic-gate 		 */
9127c478bd9Sstevel@tonic-gate 		sctp->sctp_iphc = kmem_zalloc(psctp->sctp_iphc_len,
9137c478bd9Sstevel@tonic-gate 		    KM_NOSLEEP);
9147c478bd9Sstevel@tonic-gate 		if (sctp->sctp_iphc == NULL)
9157c478bd9Sstevel@tonic-gate 			return (ENOMEM);
9167c478bd9Sstevel@tonic-gate 		sctp->sctp_iphc_len = psctp->sctp_iphc_len;
9177c478bd9Sstevel@tonic-gate 		sctp->sctp_hdr_len = psctp->sctp_hdr_len;
9187c478bd9Sstevel@tonic-gate 
9197c478bd9Sstevel@tonic-gate 		sctp->sctp_iphc6 = kmem_zalloc(psctp->sctp_iphc6_len,
9207c478bd9Sstevel@tonic-gate 		    KM_NOSLEEP);
9217c478bd9Sstevel@tonic-gate 		if (sctp->sctp_iphc6 == NULL) {
9227c478bd9Sstevel@tonic-gate 			sctp->sctp_iphc6_len = 0;
9237c478bd9Sstevel@tonic-gate 			return (ENOMEM);
9247c478bd9Sstevel@tonic-gate 		}
9257c478bd9Sstevel@tonic-gate 		sctp->sctp_iphc6_len = psctp->sctp_iphc6_len;
9267c478bd9Sstevel@tonic-gate 		sctp->sctp_hdr6_len = psctp->sctp_hdr6_len;
9277c478bd9Sstevel@tonic-gate 
9287c478bd9Sstevel@tonic-gate 		sctp->sctp_ip_hdr_len = psctp->sctp_ip_hdr_len;
9297c478bd9Sstevel@tonic-gate 		sctp->sctp_ip_hdr6_len = psctp->sctp_ip_hdr6_len;
9307c478bd9Sstevel@tonic-gate 
9317c478bd9Sstevel@tonic-gate 		/*
9327c478bd9Sstevel@tonic-gate 		 * Copy the IP+SCTP header templates from listener
9337c478bd9Sstevel@tonic-gate 		 */
9347c478bd9Sstevel@tonic-gate 		bcopy(psctp->sctp_iphc, sctp->sctp_iphc,
9357c478bd9Sstevel@tonic-gate 		    psctp->sctp_hdr_len);
9367c478bd9Sstevel@tonic-gate 		sctp->sctp_ipha = (ipha_t *)sctp->sctp_iphc;
9377c478bd9Sstevel@tonic-gate 		sctp->sctp_sctph = (sctp_hdr_t *)(sctp->sctp_iphc +
9387c478bd9Sstevel@tonic-gate 		    sctp->sctp_ip_hdr_len);
9397c478bd9Sstevel@tonic-gate 
9407c478bd9Sstevel@tonic-gate 		bcopy(psctp->sctp_iphc6, sctp->sctp_iphc6,
9417c478bd9Sstevel@tonic-gate 		    psctp->sctp_hdr6_len);
9427c478bd9Sstevel@tonic-gate 		if (((ip6i_t *)(sctp->sctp_iphc6))->ip6i_nxt == IPPROTO_RAW) {
9437c478bd9Sstevel@tonic-gate 			sctp->sctp_ip6h = (ip6_t *)(sctp->sctp_iphc6 +
9447c478bd9Sstevel@tonic-gate 			    sizeof (ip6i_t));
9457c478bd9Sstevel@tonic-gate 		} else {
9467c478bd9Sstevel@tonic-gate 			sctp->sctp_ip6h = (ip6_t *)sctp->sctp_iphc6;
9477c478bd9Sstevel@tonic-gate 		}
9487c478bd9Sstevel@tonic-gate 		sctp->sctp_sctph6 = (sctp_hdr_t *)(sctp->sctp_iphc6 +
9497c478bd9Sstevel@tonic-gate 		    sctp->sctp_ip_hdr6_len);
9507c478bd9Sstevel@tonic-gate 
9517c478bd9Sstevel@tonic-gate 		sctp->sctp_cookie_lifetime = psctp->sctp_cookie_lifetime;
9527c478bd9Sstevel@tonic-gate 		sctp->sctp_xmit_lowater = psctp->sctp_xmit_lowater;
9537c478bd9Sstevel@tonic-gate 		sctp->sctp_xmit_hiwater = psctp->sctp_xmit_hiwater;
9547c478bd9Sstevel@tonic-gate 		sctp->sctp_cwnd_max = psctp->sctp_cwnd_max;
9557c478bd9Sstevel@tonic-gate 		sctp->sctp_rwnd = psctp->sctp_rwnd;
9561d8c4025Svi 		sctp->sctp_irwnd = psctp->sctp_rwnd;
9577c478bd9Sstevel@tonic-gate 
9587c478bd9Sstevel@tonic-gate 		sctp->sctp_rto_max = psctp->sctp_rto_max;
9597c478bd9Sstevel@tonic-gate 		sctp->sctp_init_rto_max = psctp->sctp_init_rto_max;
9607c478bd9Sstevel@tonic-gate 		sctp->sctp_rto_min = psctp->sctp_rto_min;
9617c478bd9Sstevel@tonic-gate 		sctp->sctp_rto_initial = psctp->sctp_rto_initial;
9627c478bd9Sstevel@tonic-gate 		sctp->sctp_pa_max_rxt = psctp->sctp_pa_max_rxt;
9637c478bd9Sstevel@tonic-gate 		sctp->sctp_pp_max_rxt = psctp->sctp_pp_max_rxt;
9647c478bd9Sstevel@tonic-gate 		sctp->sctp_max_init_rxt = psctp->sctp_max_init_rxt;
9657c478bd9Sstevel@tonic-gate 
9667c478bd9Sstevel@tonic-gate 		sctp->sctp_def_stream = psctp->sctp_def_stream;
9677c478bd9Sstevel@tonic-gate 		sctp->sctp_def_flags = psctp->sctp_def_flags;
9687c478bd9Sstevel@tonic-gate 		sctp->sctp_def_ppid = psctp->sctp_def_ppid;
9697c478bd9Sstevel@tonic-gate 		sctp->sctp_def_context = psctp->sctp_def_context;
9707c478bd9Sstevel@tonic-gate 		sctp->sctp_def_timetolive = psctp->sctp_def_timetolive;
9717c478bd9Sstevel@tonic-gate 
9727c478bd9Sstevel@tonic-gate 		sctp->sctp_num_istr = psctp->sctp_num_istr;
9737c478bd9Sstevel@tonic-gate 		sctp->sctp_num_ostr = psctp->sctp_num_ostr;
9747c478bd9Sstevel@tonic-gate 
9757c478bd9Sstevel@tonic-gate 		sctp->sctp_hb_interval = psctp->sctp_hb_interval;
9767c478bd9Sstevel@tonic-gate 		sctp->sctp_autoclose = psctp->sctp_autoclose;
9777c478bd9Sstevel@tonic-gate 		sctp->sctp_tx_adaption_code = psctp->sctp_tx_adaption_code;
9787c478bd9Sstevel@tonic-gate 
9797c478bd9Sstevel@tonic-gate 		/* xxx should be a better way to copy these flags xxx */
9807c478bd9Sstevel@tonic-gate 		sctp->sctp_debug = psctp->sctp_debug;
9817c478bd9Sstevel@tonic-gate 		sctp->sctp_bound_to_all = psctp->sctp_bound_to_all;
9827c478bd9Sstevel@tonic-gate 		sctp->sctp_cansleep = psctp->sctp_cansleep;
9837c478bd9Sstevel@tonic-gate 		sctp->sctp_send_adaption = psctp->sctp_send_adaption;
9847c478bd9Sstevel@tonic-gate 		sctp->sctp_ndelay = psctp->sctp_ndelay;
9857c478bd9Sstevel@tonic-gate 		sctp->sctp_events = psctp->sctp_events;
9867c478bd9Sstevel@tonic-gate 		sctp->sctp_ipv6_recvancillary = psctp->sctp_ipv6_recvancillary;
9875d0bc3edSsommerfe 
9885d0bc3edSsommerfe 		/* Copy IP-layer options */
9895d0bc3edSsommerfe 		connp = sctp->sctp_connp;
9905d0bc3edSsommerfe 		pconnp = psctp->sctp_connp;
9915d0bc3edSsommerfe 
9925d0bc3edSsommerfe 		connp->conn_broadcast = pconnp->conn_broadcast;
9935d0bc3edSsommerfe 		connp->conn_loopback = pconnp->conn_loopback;
9945d0bc3edSsommerfe 		connp->conn_dontroute = pconnp->conn_dontroute;
9955d0bc3edSsommerfe 		connp->conn_reuseaddr = pconnp->conn_reuseaddr;
9965d0bc3edSsommerfe 
9977c478bd9Sstevel@tonic-gate 	} else {
9987c478bd9Sstevel@tonic-gate 		/*
9997c478bd9Sstevel@tonic-gate 		 * Initialize the header template
10007c478bd9Sstevel@tonic-gate 		 */
10017c478bd9Sstevel@tonic-gate 		if ((err = sctp_header_init_ipv4(sctp, sleep)) != 0) {
10027c478bd9Sstevel@tonic-gate 			return (err);
10037c478bd9Sstevel@tonic-gate 		}
10047c478bd9Sstevel@tonic-gate 		if ((err = sctp_header_init_ipv6(sctp, sleep)) != 0) {
10057c478bd9Sstevel@tonic-gate 			return (err);
10067c478bd9Sstevel@tonic-gate 		}
10077c478bd9Sstevel@tonic-gate 
10087c478bd9Sstevel@tonic-gate 		/*
10097c478bd9Sstevel@tonic-gate 		 * Set to system defaults
10107c478bd9Sstevel@tonic-gate 		 */
1011*f4b3ec61Sdh 		sctp->sctp_cookie_lifetime =
1012*f4b3ec61Sdh 		    MSEC_TO_TICK(sctps->sctps_cookie_life);
1013*f4b3ec61Sdh 		sctp->sctp_xmit_lowater = sctps->sctps_xmit_lowat;
1014*f4b3ec61Sdh 		sctp->sctp_xmit_hiwater = sctps->sctps_xmit_hiwat;
1015*f4b3ec61Sdh 		sctp->sctp_cwnd_max = sctps->sctps_cwnd_max_;
1016*f4b3ec61Sdh 		sctp->sctp_rwnd = sctps->sctps_recv_hiwat;
10171d8c4025Svi 		sctp->sctp_irwnd = sctp->sctp_rwnd;
1018*f4b3ec61Sdh 		sctp->sctp_rto_max = MSEC_TO_TICK(sctps->sctps_rto_maxg);
10197c478bd9Sstevel@tonic-gate 		sctp->sctp_init_rto_max = sctp->sctp_rto_max;
1020*f4b3ec61Sdh 		sctp->sctp_rto_min = MSEC_TO_TICK(sctps->sctps_rto_ming);
1021*f4b3ec61Sdh 		sctp->sctp_rto_initial = MSEC_TO_TICK(
1022*f4b3ec61Sdh 		    sctps->sctps_rto_initialg);
1023*f4b3ec61Sdh 		sctp->sctp_pa_max_rxt = sctps->sctps_pa_max_retr;
1024*f4b3ec61Sdh 		sctp->sctp_pp_max_rxt = sctps->sctps_pp_max_retr;
1025*f4b3ec61Sdh 		sctp->sctp_max_init_rxt = sctps->sctps_max_init_retr;
1026*f4b3ec61Sdh 
1027*f4b3ec61Sdh 		sctp->sctp_num_istr = sctps->sctps_max_in_streams;
1028*f4b3ec61Sdh 		sctp->sctp_num_ostr = sctps->sctps_initial_out_streams;
1029*f4b3ec61Sdh 
1030*f4b3ec61Sdh 		sctp->sctp_hb_interval =
1031*f4b3ec61Sdh 		    MSEC_TO_TICK(sctps->sctps_heartbeat_interval);
10327c478bd9Sstevel@tonic-gate 	}
10337c478bd9Sstevel@tonic-gate 	sctp->sctp_understands_asconf = B_TRUE;
10347c478bd9Sstevel@tonic-gate 	sctp->sctp_understands_addip = B_TRUE;
10357c478bd9Sstevel@tonic-gate 	sctp->sctp_prsctp_aware = B_FALSE;
10367c478bd9Sstevel@tonic-gate 
10377c478bd9Sstevel@tonic-gate 	sctp->sctp_connp->conn_ref = 1;
10387c478bd9Sstevel@tonic-gate 	sctp->sctp_connp->conn_fully_bound = B_FALSE;
10397c478bd9Sstevel@tonic-gate 
10407c478bd9Sstevel@tonic-gate 	sctp->sctp_prsctpdrop = 0;
10417c478bd9Sstevel@tonic-gate 	sctp->sctp_msgcount = 0;
10427c478bd9Sstevel@tonic-gate 
10437c478bd9Sstevel@tonic-gate 	return (0);
10447c478bd9Sstevel@tonic-gate }
10457c478bd9Sstevel@tonic-gate 
10467c478bd9Sstevel@tonic-gate /*
10477c478bd9Sstevel@tonic-gate  * Extracts the init tag from an INIT chunk and checks if it matches
10487c478bd9Sstevel@tonic-gate  * the sctp's verification tag. Returns 0 if it doesn't match, 1 if
10497c478bd9Sstevel@tonic-gate  * it does.
10507c478bd9Sstevel@tonic-gate  */
10517c478bd9Sstevel@tonic-gate static boolean_t
10527c478bd9Sstevel@tonic-gate sctp_icmp_verf(sctp_t *sctp, sctp_hdr_t *sh, mblk_t *mp)
10537c478bd9Sstevel@tonic-gate {
10547c478bd9Sstevel@tonic-gate 	sctp_chunk_hdr_t *sch;
10557c478bd9Sstevel@tonic-gate 	uint32_t verf, *vp;
10567c478bd9Sstevel@tonic-gate 
10577c478bd9Sstevel@tonic-gate 	sch = (sctp_chunk_hdr_t *)(sh + 1);
10587c478bd9Sstevel@tonic-gate 	vp = (uint32_t *)(sch + 1);
10597c478bd9Sstevel@tonic-gate 
10607c478bd9Sstevel@tonic-gate 	/* Need at least the data chunk hdr and the first 4 bytes of INIT */
10617c478bd9Sstevel@tonic-gate 	if ((unsigned char *)(vp + 1) > mp->b_wptr) {
10627c478bd9Sstevel@tonic-gate 		return (B_FALSE);
10637c478bd9Sstevel@tonic-gate 	}
10647c478bd9Sstevel@tonic-gate 
10657c478bd9Sstevel@tonic-gate 	bcopy(vp, &verf, sizeof (verf));
10667c478bd9Sstevel@tonic-gate 
10677c478bd9Sstevel@tonic-gate 	if (verf == sctp->sctp_lvtag) {
10687c478bd9Sstevel@tonic-gate 		return (B_TRUE);
10697c478bd9Sstevel@tonic-gate 	}
10707c478bd9Sstevel@tonic-gate 	return (B_FALSE);
10717c478bd9Sstevel@tonic-gate }
10727c478bd9Sstevel@tonic-gate 
10737c478bd9Sstevel@tonic-gate /*
10747c478bd9Sstevel@tonic-gate  * sctp_icmp_error is called by sctp_input() to process ICMP error messages
10757c478bd9Sstevel@tonic-gate  * passed up by IP.  The queue is the default queue.  We need to find a sctp_t
10767c478bd9Sstevel@tonic-gate  * that corresponds to the returned datagram.  Passes the message back in on
10777c478bd9Sstevel@tonic-gate  * the correct queue once it has located the connection.
10787c478bd9Sstevel@tonic-gate  * Assumes that IP has pulled up everything up to and including
10797c478bd9Sstevel@tonic-gate  * the ICMP header.
10807c478bd9Sstevel@tonic-gate  */
10817c478bd9Sstevel@tonic-gate void
10827c478bd9Sstevel@tonic-gate sctp_icmp_error(sctp_t *sctp, mblk_t *mp)
10837c478bd9Sstevel@tonic-gate {
10847c478bd9Sstevel@tonic-gate 	icmph_t *icmph;
10857c478bd9Sstevel@tonic-gate 	ipha_t	*ipha;
10867c478bd9Sstevel@tonic-gate 	int	iph_hdr_length;
10877c478bd9Sstevel@tonic-gate 	sctp_hdr_t *sctph;
10887c478bd9Sstevel@tonic-gate 	mblk_t *first_mp;
10897c478bd9Sstevel@tonic-gate 	uint32_t new_mtu;
10907c478bd9Sstevel@tonic-gate 	in6_addr_t dst;
10917c478bd9Sstevel@tonic-gate 	sctp_faddr_t *fp;
1092*f4b3ec61Sdh 	sctp_stack_t	*sctps = sctp->sctp_sctps;
10937c478bd9Sstevel@tonic-gate 
109445916cd2Sjpk 	dprint(1, ("sctp_icmp_error: sctp=%p, mp=%p\n", (void *)sctp,
109545916cd2Sjpk 	    (void *)mp));
10967c478bd9Sstevel@tonic-gate 
10977c478bd9Sstevel@tonic-gate 	first_mp = mp;
10987c478bd9Sstevel@tonic-gate 
10997c478bd9Sstevel@tonic-gate 	ipha = (ipha_t *)mp->b_rptr;
11007c478bd9Sstevel@tonic-gate 	if (IPH_HDR_VERSION(ipha) != IPV4_VERSION) {
11017c478bd9Sstevel@tonic-gate 		ASSERT(IPH_HDR_VERSION(ipha) == IPV6_VERSION);
11027c478bd9Sstevel@tonic-gate 		sctp_icmp_error_ipv6(sctp, first_mp);
11037c478bd9Sstevel@tonic-gate 		return;
11047c478bd9Sstevel@tonic-gate 	}
11057c478bd9Sstevel@tonic-gate 
11067c478bd9Sstevel@tonic-gate 	iph_hdr_length = IPH_HDR_LENGTH(ipha);
11077c478bd9Sstevel@tonic-gate 	icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length];
11087c478bd9Sstevel@tonic-gate 	ipha = (ipha_t *)&icmph[1];
11097c478bd9Sstevel@tonic-gate 	iph_hdr_length = IPH_HDR_LENGTH(ipha);
11107c478bd9Sstevel@tonic-gate 	sctph = (sctp_hdr_t *)((char *)ipha + iph_hdr_length);
11117c478bd9Sstevel@tonic-gate 	if ((uchar_t *)(sctph + 1) >= mp->b_wptr) {
11127c478bd9Sstevel@tonic-gate 		/* not enough data for SCTP header */
11137c478bd9Sstevel@tonic-gate 		freemsg(first_mp);
11147c478bd9Sstevel@tonic-gate 		return;
11157c478bd9Sstevel@tonic-gate 	}
11167c478bd9Sstevel@tonic-gate 
11177c478bd9Sstevel@tonic-gate 	switch (icmph->icmph_type) {
11187c478bd9Sstevel@tonic-gate 	case ICMP_DEST_UNREACHABLE:
11197c478bd9Sstevel@tonic-gate 		switch (icmph->icmph_code) {
11207c478bd9Sstevel@tonic-gate 		case ICMP_FRAGMENTATION_NEEDED:
11217c478bd9Sstevel@tonic-gate 			/*
11227c478bd9Sstevel@tonic-gate 			 * Reduce the MSS based on the new MTU.  This will
11237c478bd9Sstevel@tonic-gate 			 * eliminate any fragmentation locally.
11247c478bd9Sstevel@tonic-gate 			 * N.B.  There may well be some funny side-effects on
11257c478bd9Sstevel@tonic-gate 			 * the local send policy and the remote receive policy.
11267c478bd9Sstevel@tonic-gate 			 * Pending further research, we provide
11277c478bd9Sstevel@tonic-gate 			 * sctp_ignore_path_mtu just in case this proves
11287c478bd9Sstevel@tonic-gate 			 * disastrous somewhere.
11297c478bd9Sstevel@tonic-gate 			 *
11307c478bd9Sstevel@tonic-gate 			 * After updating the MSS, retransmit part of the
11317c478bd9Sstevel@tonic-gate 			 * dropped segment using the new mss by calling
11327c478bd9Sstevel@tonic-gate 			 * sctp_wput_slow().  Need to adjust all those
11337c478bd9Sstevel@tonic-gate 			 * params to make sure sctp_wput_slow() work properly.
11347c478bd9Sstevel@tonic-gate 			 */
1135*f4b3ec61Sdh 			if (sctps->sctps_ignore_path_mtu)
11367c478bd9Sstevel@tonic-gate 				break;
11377c478bd9Sstevel@tonic-gate 
11387c478bd9Sstevel@tonic-gate 			/* find the offending faddr */
11397c478bd9Sstevel@tonic-gate 			IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, &dst);
11407c478bd9Sstevel@tonic-gate 			fp = sctp_lookup_faddr(sctp, &dst);
11417c478bd9Sstevel@tonic-gate 			if (fp == NULL) {
11427c478bd9Sstevel@tonic-gate 				break;
11437c478bd9Sstevel@tonic-gate 			}
11447c478bd9Sstevel@tonic-gate 
11457c478bd9Sstevel@tonic-gate 			new_mtu = ntohs(icmph->icmph_du_mtu);
11467c478bd9Sstevel@tonic-gate 
11477c478bd9Sstevel@tonic-gate 			if (new_mtu - sctp->sctp_hdr_len >= fp->sfa_pmss)
11487c478bd9Sstevel@tonic-gate 				break;
11497c478bd9Sstevel@tonic-gate 
11507c478bd9Sstevel@tonic-gate 			/*
11517c478bd9Sstevel@tonic-gate 			 * Make sure that sfa_pmss is a multiple of
11527c478bd9Sstevel@tonic-gate 			 * SCTP_ALIGN.
11537c478bd9Sstevel@tonic-gate 			 */
11547c478bd9Sstevel@tonic-gate 			fp->sfa_pmss = (new_mtu - sctp->sctp_hdr_len) &
11557c478bd9Sstevel@tonic-gate 				~(SCTP_ALIGN - 1);
11567c478bd9Sstevel@tonic-gate 			fp->pmtu_discovered = 1;
11577c478bd9Sstevel@tonic-gate 
11587c478bd9Sstevel@tonic-gate 			break;
11597c478bd9Sstevel@tonic-gate 		case ICMP_PORT_UNREACHABLE:
11607c478bd9Sstevel@tonic-gate 		case ICMP_PROTOCOL_UNREACHABLE:
11617c478bd9Sstevel@tonic-gate 			switch (sctp->sctp_state) {
11627c478bd9Sstevel@tonic-gate 			case SCTPS_COOKIE_WAIT:
11637c478bd9Sstevel@tonic-gate 			case SCTPS_COOKIE_ECHOED:
11647c478bd9Sstevel@tonic-gate 				/* make sure the verification tag matches */
11657c478bd9Sstevel@tonic-gate 				if (!sctp_icmp_verf(sctp, sctph, mp)) {
11667c478bd9Sstevel@tonic-gate 					break;
11677c478bd9Sstevel@tonic-gate 				}
1168*f4b3ec61Sdh 				BUMP_MIB(&sctps->sctps_mib, sctpAborted);
1169c9da23f8Skcpoon 				sctp_assoc_event(sctp, SCTP_CANT_STR_ASSOC, 0,
1170c9da23f8Skcpoon 				    NULL);
11717c478bd9Sstevel@tonic-gate 				sctp_clean_death(sctp, ECONNREFUSED);
11727c478bd9Sstevel@tonic-gate 				break;
11737c478bd9Sstevel@tonic-gate 			}
11747c478bd9Sstevel@tonic-gate 			break;
11757c478bd9Sstevel@tonic-gate 		case ICMP_HOST_UNREACHABLE:
11767c478bd9Sstevel@tonic-gate 		case ICMP_NET_UNREACHABLE:
11777c478bd9Sstevel@tonic-gate 			/* Record the error in case we finally time out. */
11787c478bd9Sstevel@tonic-gate 			sctp->sctp_client_errno = (icmph->icmph_code ==
11797c478bd9Sstevel@tonic-gate 			    ICMP_HOST_UNREACHABLE) ? EHOSTUNREACH : ENETUNREACH;
11807c478bd9Sstevel@tonic-gate 			break;
11817c478bd9Sstevel@tonic-gate 		default:
11827c478bd9Sstevel@tonic-gate 			break;
11837c478bd9Sstevel@tonic-gate 		}
11847c478bd9Sstevel@tonic-gate 		break;
11857c478bd9Sstevel@tonic-gate 	case ICMP_SOURCE_QUENCH: {
11867c478bd9Sstevel@tonic-gate 		/* Reduce the sending rate as if we got a retransmit timeout */
11877c478bd9Sstevel@tonic-gate 		break;
11887c478bd9Sstevel@tonic-gate 	}
11897c478bd9Sstevel@tonic-gate 	}
11907c478bd9Sstevel@tonic-gate 	freemsg(first_mp);
11917c478bd9Sstevel@tonic-gate }
11927c478bd9Sstevel@tonic-gate 
11937c478bd9Sstevel@tonic-gate /*
11947c478bd9Sstevel@tonic-gate  * sctp_icmp_error_ipv6() is called by sctp_icmp_error() to process ICMPv6
11957c478bd9Sstevel@tonic-gate  * error messages passed up by IP.
11967c478bd9Sstevel@tonic-gate  * Assumes that IP has pulled up all the extension headers as well
11977c478bd9Sstevel@tonic-gate  * as the ICMPv6 header.
11987c478bd9Sstevel@tonic-gate  */
11997c478bd9Sstevel@tonic-gate static void
12007c478bd9Sstevel@tonic-gate sctp_icmp_error_ipv6(sctp_t *sctp, mblk_t *mp)
12017c478bd9Sstevel@tonic-gate {
12027c478bd9Sstevel@tonic-gate 	icmp6_t *icmp6;
12037c478bd9Sstevel@tonic-gate 	ip6_t	*ip6h;
12047c478bd9Sstevel@tonic-gate 	uint16_t	iph_hdr_length;
12057c478bd9Sstevel@tonic-gate 	sctp_hdr_t *sctpha;
12067c478bd9Sstevel@tonic-gate 	uint8_t	*nexthdrp;
12077c478bd9Sstevel@tonic-gate 	uint32_t new_mtu;
12087c478bd9Sstevel@tonic-gate 	sctp_faddr_t *fp;
1209*f4b3ec61Sdh 	sctp_stack_t	*sctps = sctp->sctp_sctps;
12107c478bd9Sstevel@tonic-gate 
12117c478bd9Sstevel@tonic-gate 	ip6h = (ip6_t *)mp->b_rptr;
12127c478bd9Sstevel@tonic-gate 	iph_hdr_length = (ip6h->ip6_nxt != IPPROTO_SCTP) ?
12137c478bd9Sstevel@tonic-gate 	    ip_hdr_length_v6(mp, ip6h) : IPV6_HDR_LEN;
12147c478bd9Sstevel@tonic-gate 
12157c478bd9Sstevel@tonic-gate 	icmp6 = (icmp6_t *)&mp->b_rptr[iph_hdr_length];
12167c478bd9Sstevel@tonic-gate 	ip6h = (ip6_t *)&icmp6[1];
12177c478bd9Sstevel@tonic-gate 	if (!ip_hdr_length_nexthdr_v6(mp, ip6h, &iph_hdr_length, &nexthdrp)) {
12187c478bd9Sstevel@tonic-gate 		freemsg(mp);
12197c478bd9Sstevel@tonic-gate 		return;
12207c478bd9Sstevel@tonic-gate 	}
12217c478bd9Sstevel@tonic-gate 	ASSERT(*nexthdrp == IPPROTO_SCTP);
12227c478bd9Sstevel@tonic-gate 
12237c478bd9Sstevel@tonic-gate 	/* XXX need ifindex to find connection */
12247c478bd9Sstevel@tonic-gate 	sctpha = (sctp_hdr_t *)((char *)ip6h + iph_hdr_length);
12257c478bd9Sstevel@tonic-gate 	if ((uchar_t *)sctpha >= mp->b_wptr) {
12267c478bd9Sstevel@tonic-gate 		/* not enough data for SCTP header */
12277c478bd9Sstevel@tonic-gate 		freemsg(mp);
12287c478bd9Sstevel@tonic-gate 		return;
12297c478bd9Sstevel@tonic-gate 	}
12307c478bd9Sstevel@tonic-gate 	switch (icmp6->icmp6_type) {
12317c478bd9Sstevel@tonic-gate 	case ICMP6_PACKET_TOO_BIG:
12327c478bd9Sstevel@tonic-gate 		/*
12337c478bd9Sstevel@tonic-gate 		 * Reduce the MSS based on the new MTU.  This will
12347c478bd9Sstevel@tonic-gate 		 * eliminate any fragmentation locally.
12357c478bd9Sstevel@tonic-gate 		 * N.B.  There may well be some funny side-effects on
12367c478bd9Sstevel@tonic-gate 		 * the local send policy and the remote receive policy.
12377c478bd9Sstevel@tonic-gate 		 * Pending further research, we provide
12387c478bd9Sstevel@tonic-gate 		 * sctp_ignore_path_mtu just in case this proves
12397c478bd9Sstevel@tonic-gate 		 * disastrous somewhere.
12407c478bd9Sstevel@tonic-gate 		 *
12417c478bd9Sstevel@tonic-gate 		 * After updating the MSS, retransmit part of the
12427c478bd9Sstevel@tonic-gate 		 * dropped segment using the new mss by calling
12437c478bd9Sstevel@tonic-gate 		 * sctp_wput_slow().  Need to adjust all those
12447c478bd9Sstevel@tonic-gate 		 * params to make sure sctp_wput_slow() work properly.
12457c478bd9Sstevel@tonic-gate 		 */
1246*f4b3ec61Sdh 		if (sctps->sctps_ignore_path_mtu)
12477c478bd9Sstevel@tonic-gate 			break;
12487c478bd9Sstevel@tonic-gate 
12497c478bd9Sstevel@tonic-gate 		/* find the offending faddr */
12507c478bd9Sstevel@tonic-gate 		fp = sctp_lookup_faddr(sctp, &ip6h->ip6_dst);
12517c478bd9Sstevel@tonic-gate 		if (fp == NULL) {
12527c478bd9Sstevel@tonic-gate 			break;
12537c478bd9Sstevel@tonic-gate 		}
12547c478bd9Sstevel@tonic-gate 
12557c478bd9Sstevel@tonic-gate 		new_mtu = ntohs(icmp6->icmp6_mtu);
12567c478bd9Sstevel@tonic-gate 
12577c478bd9Sstevel@tonic-gate 		if (new_mtu - sctp->sctp_hdr6_len >= fp->sfa_pmss)
12587c478bd9Sstevel@tonic-gate 			break;
12597c478bd9Sstevel@tonic-gate 
12607c478bd9Sstevel@tonic-gate 		/* Make sure that sfa_pmss is a multiple of SCTP_ALIGN. */
12617c478bd9Sstevel@tonic-gate 		fp->sfa_pmss = (new_mtu - sctp->sctp_hdr6_len) &
12627c478bd9Sstevel@tonic-gate 			~(SCTP_ALIGN - 1);
12637c478bd9Sstevel@tonic-gate 		fp->pmtu_discovered = 1;
12647c478bd9Sstevel@tonic-gate 
12657c478bd9Sstevel@tonic-gate 		break;
12667c478bd9Sstevel@tonic-gate 
12677c478bd9Sstevel@tonic-gate 	case ICMP6_DST_UNREACH:
12687c478bd9Sstevel@tonic-gate 		switch (icmp6->icmp6_code) {
12697c478bd9Sstevel@tonic-gate 		case ICMP6_DST_UNREACH_NOPORT:
12707c478bd9Sstevel@tonic-gate 			/* make sure the verification tag matches */
12717c478bd9Sstevel@tonic-gate 			if (!sctp_icmp_verf(sctp, sctpha, mp)) {
12727c478bd9Sstevel@tonic-gate 				break;
12737c478bd9Sstevel@tonic-gate 			}
12747c478bd9Sstevel@tonic-gate 			if (sctp->sctp_state == SCTPS_COOKIE_WAIT ||
12757c478bd9Sstevel@tonic-gate 			    sctp->sctp_state == SCTPS_COOKIE_ECHOED) {
1276*f4b3ec61Sdh 				BUMP_MIB(&sctps->sctps_mib, sctpAborted);
1277c9da23f8Skcpoon 				sctp_assoc_event(sctp, SCTP_CANT_STR_ASSOC, 0,
1278c9da23f8Skcpoon 				    NULL);
12797c478bd9Sstevel@tonic-gate 				sctp_clean_death(sctp, ECONNREFUSED);
12807c478bd9Sstevel@tonic-gate 			}
12817c478bd9Sstevel@tonic-gate 			break;
12827c478bd9Sstevel@tonic-gate 
12837c478bd9Sstevel@tonic-gate 		case ICMP6_DST_UNREACH_ADMIN:
12847c478bd9Sstevel@tonic-gate 		case ICMP6_DST_UNREACH_NOROUTE:
12857c478bd9Sstevel@tonic-gate 		case ICMP6_DST_UNREACH_NOTNEIGHBOR:
12867c478bd9Sstevel@tonic-gate 		case ICMP6_DST_UNREACH_ADDR:
12877c478bd9Sstevel@tonic-gate 			/* Record the error in case we finally time out. */
12887c478bd9Sstevel@tonic-gate 			sctp->sctp_client_errno = EHOSTUNREACH;
12897c478bd9Sstevel@tonic-gate 			break;
12907c478bd9Sstevel@tonic-gate 		default:
12917c478bd9Sstevel@tonic-gate 			break;
12927c478bd9Sstevel@tonic-gate 		}
12937c478bd9Sstevel@tonic-gate 		break;
12947c478bd9Sstevel@tonic-gate 
12957c478bd9Sstevel@tonic-gate 	case ICMP6_PARAM_PROB:
12967c478bd9Sstevel@tonic-gate 		/* If this corresponds to an ICMP_PROTOCOL_UNREACHABLE */
12977c478bd9Sstevel@tonic-gate 		if (icmp6->icmp6_code == ICMP6_PARAMPROB_NEXTHEADER &&
12987c478bd9Sstevel@tonic-gate 		    (uchar_t *)ip6h + icmp6->icmp6_pptr ==
12997c478bd9Sstevel@tonic-gate 		    (uchar_t *)nexthdrp) {
13007c478bd9Sstevel@tonic-gate 			/* make sure the verification tag matches */
13017c478bd9Sstevel@tonic-gate 			if (!sctp_icmp_verf(sctp, sctpha, mp)) {
13027c478bd9Sstevel@tonic-gate 				break;
13037c478bd9Sstevel@tonic-gate 			}
13047c478bd9Sstevel@tonic-gate 			if (sctp->sctp_state == SCTPS_COOKIE_WAIT) {
1305*f4b3ec61Sdh 				BUMP_MIB(&sctps->sctps_mib, sctpAborted);
1306c9da23f8Skcpoon 				sctp_assoc_event(sctp, SCTP_CANT_STR_ASSOC, 0,
1307c9da23f8Skcpoon 				    NULL);
13087c478bd9Sstevel@tonic-gate 				sctp_clean_death(sctp, ECONNREFUSED);
13097c478bd9Sstevel@tonic-gate 			}
13107c478bd9Sstevel@tonic-gate 			break;
13117c478bd9Sstevel@tonic-gate 		}
13127c478bd9Sstevel@tonic-gate 		break;
13137c478bd9Sstevel@tonic-gate 
13147c478bd9Sstevel@tonic-gate 	case ICMP6_TIME_EXCEEDED:
13157c478bd9Sstevel@tonic-gate 	default:
13167c478bd9Sstevel@tonic-gate 		break;
13177c478bd9Sstevel@tonic-gate 	}
13187c478bd9Sstevel@tonic-gate 	freemsg(mp);
13197c478bd9Sstevel@tonic-gate }
13207c478bd9Sstevel@tonic-gate 
13217c478bd9Sstevel@tonic-gate /*
13227c478bd9Sstevel@tonic-gate  * Called by sockfs to create a new sctp instance.
13237c478bd9Sstevel@tonic-gate  *
13247c478bd9Sstevel@tonic-gate  * If parent pointer is passed in, inherit settings from it.
13257c478bd9Sstevel@tonic-gate  */
13267c478bd9Sstevel@tonic-gate sctp_t *
13277c478bd9Sstevel@tonic-gate sctp_create(void *sctp_ulpd, sctp_t *parent, int family, int flags,
13287c478bd9Sstevel@tonic-gate     const sctp_upcalls_t *sctp_upcalls, sctp_sockbuf_limits_t *sbl,
13297c478bd9Sstevel@tonic-gate     cred_t *credp)
13307c478bd9Sstevel@tonic-gate {
13317c478bd9Sstevel@tonic-gate 	sctp_t		*sctp, *psctp;
13327c478bd9Sstevel@tonic-gate 	conn_t		*sctp_connp;
13337c478bd9Sstevel@tonic-gate 	mblk_t		*ack_mp, *hb_mp;
13347c478bd9Sstevel@tonic-gate 	int		sleep = flags & SCTP_CAN_BLOCK ? KM_SLEEP : KM_NOSLEEP;
1335*f4b3ec61Sdh 	zoneid_t	zoneid;
1336*f4b3ec61Sdh 	sctp_stack_t	*sctps;
13377c478bd9Sstevel@tonic-gate 
13387c478bd9Sstevel@tonic-gate 	/* User must supply a credential. */
13397c478bd9Sstevel@tonic-gate 	if (credp == NULL)
13407c478bd9Sstevel@tonic-gate 		return (NULL);
13417c478bd9Sstevel@tonic-gate 
13427c478bd9Sstevel@tonic-gate 	psctp = (sctp_t *)parent;
1343*f4b3ec61Sdh 	if (psctp != NULL) {
1344*f4b3ec61Sdh 		sctps = psctp->sctp_sctps;
1345*f4b3ec61Sdh 		/* Increase here to have common decrease at end */
1346*f4b3ec61Sdh 		netstack_hold(sctps->sctps_netstack);
1347*f4b3ec61Sdh 	} else {
1348*f4b3ec61Sdh 		netstack_t *ns;
1349*f4b3ec61Sdh 
1350*f4b3ec61Sdh 		ns = netstack_find_by_cred(credp);
1351*f4b3ec61Sdh 		ASSERT(ns != NULL);
1352*f4b3ec61Sdh 		sctps = ns->netstack_sctp;
1353*f4b3ec61Sdh 		ASSERT(sctps != NULL);
13547c478bd9Sstevel@tonic-gate 
1355*f4b3ec61Sdh 		/*
1356*f4b3ec61Sdh 		 * For exclusive stacks we set the zoneid to zero
1357*f4b3ec61Sdh 		 * to make SCTP operate as if in the global zone.
1358*f4b3ec61Sdh 		 */
1359*f4b3ec61Sdh 		if (sctps->sctps_netstack->netstack_stackid !=
1360*f4b3ec61Sdh 		    GLOBAL_NETSTACKID)
1361*f4b3ec61Sdh 			zoneid = GLOBAL_ZONEID;
1362*f4b3ec61Sdh 		else
1363*f4b3ec61Sdh 			zoneid = crgetzoneid(credp);
1364*f4b3ec61Sdh 
1365*f4b3ec61Sdh 		/*
1366*f4b3ec61Sdh 		 * For stackid zero this is done from strplumb.c, but
1367*f4b3ec61Sdh 		 * non-zero stackids are handled here.
1368*f4b3ec61Sdh 		 */
1369*f4b3ec61Sdh 		if (sctps->sctps_g_q == NULL &&
1370*f4b3ec61Sdh 		    sctps->sctps_netstack->netstack_stackid !=
1371*f4b3ec61Sdh 		    GLOBAL_NETSTACKID) {
1372*f4b3ec61Sdh 			sctp_g_q_setup(sctps);
1373*f4b3ec61Sdh 		}
1374*f4b3ec61Sdh 	}
1375*f4b3ec61Sdh 	if ((sctp_connp = ipcl_conn_create(IPCL_SCTPCONN, sleep,
1376*f4b3ec61Sdh 		    sctps->sctps_netstack)) == NULL) {
1377*f4b3ec61Sdh 		netstack_rele(sctps->sctps_netstack);
1378*f4b3ec61Sdh 		SCTP_KSTAT(sctps, sctp_conn_create);
1379*f4b3ec61Sdh 		return (NULL);
1380*f4b3ec61Sdh 	}
1381*f4b3ec61Sdh 	/*
1382*f4b3ec61Sdh 	 * ipcl_conn_create did a netstack_hold. Undo the hold that was
1383*f4b3ec61Sdh 	 * done at top of sctp_create.
1384*f4b3ec61Sdh 	 */
1385*f4b3ec61Sdh 	netstack_rele(sctps->sctps_netstack);
13867c478bd9Sstevel@tonic-gate 	sctp = CONN2SCTP(sctp_connp);
1387*f4b3ec61Sdh 	sctp->sctp_sctps = sctps;
13887c478bd9Sstevel@tonic-gate 
1389*f4b3ec61Sdh 	sctp_connp->conn_ulp_labeled = is_system_labeled();
13907c478bd9Sstevel@tonic-gate 	if ((ack_mp = sctp_timer_alloc(sctp, sctp_ack_timer)) == NULL ||
13917c478bd9Sstevel@tonic-gate 	    (hb_mp = sctp_timer_alloc(sctp, sctp_heartbeat_timer)) == NULL) {
13927c478bd9Sstevel@tonic-gate 		if (ack_mp != NULL)
13937c478bd9Sstevel@tonic-gate 			freeb(ack_mp);
1394*f4b3ec61Sdh 		netstack_rele(sctp_connp->conn_netstack);
1395*f4b3ec61Sdh 		sctp_connp->conn_netstack = NULL;
1396*f4b3ec61Sdh 		sctp->sctp_sctps = NULL;
1397*f4b3ec61Sdh 		SCTP_G_Q_REFRELE(sctps);
13987c478bd9Sstevel@tonic-gate 		kmem_cache_free(sctp_conn_cache, sctp_connp);
13997c478bd9Sstevel@tonic-gate 		return (NULL);
14007c478bd9Sstevel@tonic-gate 	}
14017c478bd9Sstevel@tonic-gate 
14027c478bd9Sstevel@tonic-gate 	sctp->sctp_ack_mp = ack_mp;
14037c478bd9Sstevel@tonic-gate 	sctp->sctp_heartbeat_mp = hb_mp;
14047c478bd9Sstevel@tonic-gate 
14057c478bd9Sstevel@tonic-gate 	switch (family) {
14067c478bd9Sstevel@tonic-gate 	case AF_INET6:
14077c478bd9Sstevel@tonic-gate 		sctp_connp->conn_af_isv6 = B_TRUE;
14087c478bd9Sstevel@tonic-gate 		sctp->sctp_ipversion = IPV6_VERSION;
14097c478bd9Sstevel@tonic-gate 		sctp->sctp_family = AF_INET6;
14107c478bd9Sstevel@tonic-gate 		break;
14117c478bd9Sstevel@tonic-gate 
14127c478bd9Sstevel@tonic-gate 	case AF_INET:
14137c478bd9Sstevel@tonic-gate 		sctp_connp->conn_af_isv6 = B_FALSE;
14147c478bd9Sstevel@tonic-gate 		sctp_connp->conn_pkt_isv6 = B_FALSE;
14157c478bd9Sstevel@tonic-gate 		sctp->sctp_ipversion = IPV4_VERSION;
14167c478bd9Sstevel@tonic-gate 		sctp->sctp_family = AF_INET;
14177c478bd9Sstevel@tonic-gate 		break;
14187c478bd9Sstevel@tonic-gate 	default:
14197c478bd9Sstevel@tonic-gate 		ASSERT(0);
14207c478bd9Sstevel@tonic-gate 		break;
14217c478bd9Sstevel@tonic-gate 	}
14227c478bd9Sstevel@tonic-gate 	if (sctp_init_values(sctp, psctp, sleep) != 0) {
14237c478bd9Sstevel@tonic-gate 		freeb(ack_mp);
14247c478bd9Sstevel@tonic-gate 		freeb(hb_mp);
1425*f4b3ec61Sdh 		netstack_rele(sctp_connp->conn_netstack);
1426*f4b3ec61Sdh 		sctp_connp->conn_netstack = NULL;
1427*f4b3ec61Sdh 		sctp->sctp_sctps = NULL;
1428*f4b3ec61Sdh 		SCTP_G_Q_REFRELE(sctps);
14297c478bd9Sstevel@tonic-gate 		kmem_cache_free(sctp_conn_cache, sctp_connp);
14307c478bd9Sstevel@tonic-gate 		return (NULL);
14317c478bd9Sstevel@tonic-gate 	}
14327c478bd9Sstevel@tonic-gate 	sctp->sctp_cansleep = ((flags & SCTP_CAN_BLOCK) == SCTP_CAN_BLOCK);
14337c478bd9Sstevel@tonic-gate 
1434*f4b3ec61Sdh 	sctp->sctp_mss = sctps->sctps_initial_mtu - ((family == AF_INET6) ?
14357c478bd9Sstevel@tonic-gate 		sctp->sctp_hdr6_len : sctp->sctp_hdr_len);
14367c478bd9Sstevel@tonic-gate 
14377c478bd9Sstevel@tonic-gate 	if (psctp != NULL) {
14387c478bd9Sstevel@tonic-gate 		RUN_SCTP(psctp);
14397c478bd9Sstevel@tonic-gate 		/*
14407c478bd9Sstevel@tonic-gate 		 * Inherit local address list, local port. Parent is either
14417c478bd9Sstevel@tonic-gate 		 * in SCTPS_BOUND, or SCTPS_LISTEN state.
14427c478bd9Sstevel@tonic-gate 		 */
14437c478bd9Sstevel@tonic-gate 		ASSERT((psctp->sctp_state == SCTPS_BOUND) ||
14447c478bd9Sstevel@tonic-gate 		    (psctp->sctp_state == SCTPS_LISTEN));
14457c478bd9Sstevel@tonic-gate 		if (sctp_dup_saddrs(psctp, sctp, sleep)) {
14467c478bd9Sstevel@tonic-gate 			WAKE_SCTP(psctp);
14477c478bd9Sstevel@tonic-gate 			freeb(ack_mp);
14487c478bd9Sstevel@tonic-gate 			freeb(hb_mp);
14497c478bd9Sstevel@tonic-gate 			sctp_headers_free(sctp);
1450*f4b3ec61Sdh 			netstack_rele(sctps->sctps_netstack);
1451*f4b3ec61Sdh 			sctp_connp->conn_netstack = NULL;
1452*f4b3ec61Sdh 			sctp->sctp_sctps = NULL;
1453*f4b3ec61Sdh 			SCTP_G_Q_REFRELE(sctps);
14547c478bd9Sstevel@tonic-gate 			kmem_cache_free(sctp_conn_cache, sctp_connp);
14557c478bd9Sstevel@tonic-gate 			return (NULL);
14567c478bd9Sstevel@tonic-gate 		}
14577c478bd9Sstevel@tonic-gate 
14587c478bd9Sstevel@tonic-gate 		/*
14597c478bd9Sstevel@tonic-gate 		 * If the parent is specified, it'll be immediatelly
14607c478bd9Sstevel@tonic-gate 		 * followed by sctp_connect(). So don't add this guy to
14617c478bd9Sstevel@tonic-gate 		 * bind hash.
14627c478bd9Sstevel@tonic-gate 		 */
14637c478bd9Sstevel@tonic-gate 		sctp->sctp_lport = psctp->sctp_lport;
14647c478bd9Sstevel@tonic-gate 		sctp->sctp_state = SCTPS_BOUND;
14655d0bc3edSsommerfe 		sctp->sctp_allzones = psctp->sctp_allzones;
14667c478bd9Sstevel@tonic-gate 		sctp->sctp_zoneid = psctp->sctp_zoneid;
14677c478bd9Sstevel@tonic-gate 		WAKE_SCTP(psctp);
14687c478bd9Sstevel@tonic-gate 	} else {
1469*f4b3ec61Sdh 		sctp->sctp_zoneid = zoneid;
14707c478bd9Sstevel@tonic-gate 	}
14717c478bd9Sstevel@tonic-gate 
14727c478bd9Sstevel@tonic-gate 	sctp_connp->conn_cred = credp;
14737c478bd9Sstevel@tonic-gate 	crhold(credp);
14747c478bd9Sstevel@tonic-gate 
147545916cd2Sjpk 	/*
147645916cd2Sjpk 	 * If the caller has the process-wide flag set, then default to MAC
147745916cd2Sjpk 	 * exempt mode.  This allows read-down to unlabeled hosts.
147845916cd2Sjpk 	 */
147945916cd2Sjpk 	if (getpflags(NET_MAC_AWARE, credp) != 0)
148045916cd2Sjpk 		sctp_connp->conn_mac_exempt = B_TRUE;
148145916cd2Sjpk 
14827c478bd9Sstevel@tonic-gate 	/* Initialize SCTP instance values,  our verf tag must never be 0 */
14837c478bd9Sstevel@tonic-gate 	(void) random_get_pseudo_bytes((uint8_t *)&sctp->sctp_lvtag,
14847c478bd9Sstevel@tonic-gate 	    sizeof (sctp->sctp_lvtag));
14857c478bd9Sstevel@tonic-gate 	if (sctp->sctp_lvtag == 0)
14867c478bd9Sstevel@tonic-gate 		sctp->sctp_lvtag = (uint32_t)gethrtime();
14877c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_lvtag != 0);
14887c478bd9Sstevel@tonic-gate 
14897c478bd9Sstevel@tonic-gate 	sctp->sctp_ltsn = sctp->sctp_lvtag + 1;
14907c478bd9Sstevel@tonic-gate 	sctp->sctp_lcsn = sctp->sctp_ltsn;
14917c478bd9Sstevel@tonic-gate 	sctp->sctp_recovery_tsn = sctp->sctp_lastack_rxd = sctp->sctp_ltsn - 1;
14927c478bd9Sstevel@tonic-gate 	sctp->sctp_adv_pap = sctp->sctp_lastack_rxd;
14937c478bd9Sstevel@tonic-gate 
14947c478bd9Sstevel@tonic-gate 	/* Information required by upper layer */
14957c478bd9Sstevel@tonic-gate 	if (sctp_ulpd != NULL) {
14967c478bd9Sstevel@tonic-gate 		sctp->sctp_ulpd = sctp_ulpd;
14977c478bd9Sstevel@tonic-gate 
14987c478bd9Sstevel@tonic-gate 		ASSERT(sctp_upcalls != NULL);
14997c478bd9Sstevel@tonic-gate 		bcopy(sctp_upcalls, &sctp->sctp_upcalls,
15007c478bd9Sstevel@tonic-gate 		    sizeof (sctp_upcalls_t));
15017c478bd9Sstevel@tonic-gate 		ASSERT(sbl != NULL);
15027c478bd9Sstevel@tonic-gate 		/* Fill in the socket buffer limits for sctpsockfs */
15037c478bd9Sstevel@tonic-gate 		sbl->sbl_txlowat = sctp->sctp_xmit_lowater;
15047c478bd9Sstevel@tonic-gate 		sbl->sbl_txbuf = sctp->sctp_xmit_hiwater;
15057c478bd9Sstevel@tonic-gate 		sbl->sbl_rxbuf = sctp->sctp_rwnd;
15067c478bd9Sstevel@tonic-gate 		sbl->sbl_rxlowat = SCTP_RECV_LOWATER;
15077c478bd9Sstevel@tonic-gate 	}
15087c478bd9Sstevel@tonic-gate 	/* If no sctp_ulpd, must be creating the default sctp */
1509*f4b3ec61Sdh 	ASSERT(sctp_ulpd != NULL || sctps->sctps_gsctp == NULL);
15107c478bd9Sstevel@tonic-gate 
15117c478bd9Sstevel@tonic-gate 	/* Insert this in the global list. */
1512*f4b3ec61Sdh 	SCTP_LINK(sctp, sctps);
15137c478bd9Sstevel@tonic-gate 
15147c478bd9Sstevel@tonic-gate 	return (sctp);
15157c478bd9Sstevel@tonic-gate }
15167c478bd9Sstevel@tonic-gate 
1517*f4b3ec61Sdh /*
1518*f4b3ec61Sdh  * Make sure we wait until the default queue is setup, yet allow
1519*f4b3ec61Sdh  * sctp_g_q_create() to open a SCTP stream.
1520*f4b3ec61Sdh  * We need to allow sctp_g_q_create() do do an open
1521*f4b3ec61Sdh  * of sctp, hence we compare curhread.
1522*f4b3ec61Sdh  * All others have to wait until the sctps_g_q has been
1523*f4b3ec61Sdh  * setup.
1524*f4b3ec61Sdh  */
15257c478bd9Sstevel@tonic-gate void
1526*f4b3ec61Sdh sctp_g_q_setup(sctp_stack_t *sctps)
15277c478bd9Sstevel@tonic-gate {
1528*f4b3ec61Sdh 	mutex_enter(&sctps->sctps_g_q_lock);
1529*f4b3ec61Sdh 	if (sctps->sctps_g_q != NULL) {
1530*f4b3ec61Sdh 		mutex_exit(&sctps->sctps_g_q_lock);
1531*f4b3ec61Sdh 		return;
1532*f4b3ec61Sdh 	}
1533*f4b3ec61Sdh 	if (sctps->sctps_g_q_creator == NULL) {
1534*f4b3ec61Sdh 		/* This thread will set it up */
1535*f4b3ec61Sdh 		sctps->sctps_g_q_creator = curthread;
1536*f4b3ec61Sdh 		mutex_exit(&sctps->sctps_g_q_lock);
1537*f4b3ec61Sdh 		sctp_g_q_create(sctps);
1538*f4b3ec61Sdh 		mutex_enter(&sctps->sctps_g_q_lock);
1539*f4b3ec61Sdh 		ASSERT(sctps->sctps_g_q_creator == curthread);
1540*f4b3ec61Sdh 		sctps->sctps_g_q_creator = NULL;
1541*f4b3ec61Sdh 		cv_signal(&sctps->sctps_g_q_cv);
1542*f4b3ec61Sdh 		ASSERT(sctps->sctps_g_q != NULL);
1543*f4b3ec61Sdh 		mutex_exit(&sctps->sctps_g_q_lock);
1544*f4b3ec61Sdh 		return;
1545*f4b3ec61Sdh 	}
1546*f4b3ec61Sdh 	/* Everybody but the creator has to wait */
1547*f4b3ec61Sdh 	if (sctps->sctps_g_q_creator != curthread) {
1548*f4b3ec61Sdh 		while (sctps->sctps_g_q == NULL)
1549*f4b3ec61Sdh 			cv_wait(&sctps->sctps_g_q_cv, &sctps->sctps_g_q_lock);
1550*f4b3ec61Sdh 	}
1551*f4b3ec61Sdh 	mutex_exit(&sctps->sctps_g_q_lock);
1552*f4b3ec61Sdh }
15537c478bd9Sstevel@tonic-gate 
1554*f4b3ec61Sdh major_t IP_MAJ;
1555*f4b3ec61Sdh #define	IP	"ip"
1556*f4b3ec61Sdh 
1557*f4b3ec61Sdh #define	SCTP6DEV		"/devices/pseudo/sctp6@0:sctp6"
1558*f4b3ec61Sdh 
1559*f4b3ec61Sdh /*
1560*f4b3ec61Sdh  * Create a default sctp queue here instead of in strplumb
1561*f4b3ec61Sdh  */
1562*f4b3ec61Sdh void
1563*f4b3ec61Sdh sctp_g_q_create(sctp_stack_t *sctps)
1564*f4b3ec61Sdh {
1565*f4b3ec61Sdh 	int error;
1566*f4b3ec61Sdh 	ldi_handle_t	lh = NULL;
1567*f4b3ec61Sdh 	ldi_ident_t	li = NULL;
1568*f4b3ec61Sdh 	int		rval;
1569*f4b3ec61Sdh 	cred_t		*cr;
1570*f4b3ec61Sdh 
1571*f4b3ec61Sdh #ifdef NS_DEBUG
1572*f4b3ec61Sdh 	(void) printf("sctp_g_q_create()for stack %d\n",
1573*f4b3ec61Sdh 	    sctps->sctps_netstack->netstack_stackid);
1574*f4b3ec61Sdh #endif
1575*f4b3ec61Sdh 
1576*f4b3ec61Sdh 	ASSERT(sctps->sctps_g_q_creator == curthread);
1577*f4b3ec61Sdh 
1578*f4b3ec61Sdh 	error = ldi_ident_from_major(IP_MAJ, &li);
1579*f4b3ec61Sdh 	if (error) {
1580*f4b3ec61Sdh #ifdef DEBUG
1581*f4b3ec61Sdh 		printf("sctp_g_q_create: lyr ident get failed error %d\n",
1582*f4b3ec61Sdh 		    error);
1583*f4b3ec61Sdh #endif
1584*f4b3ec61Sdh 		return;
1585*f4b3ec61Sdh 	}
1586*f4b3ec61Sdh 
1587*f4b3ec61Sdh 	cr = zone_get_kcred(netstackid_to_zoneid(
1588*f4b3ec61Sdh 				sctps->sctps_netstack->netstack_stackid));
1589*f4b3ec61Sdh 	ASSERT(cr != NULL);
1590*f4b3ec61Sdh 	/*
1591*f4b3ec61Sdh 	 * We set the sctp default queue to IPv6 because IPv4 falls
1592*f4b3ec61Sdh 	 * back to IPv6 when it can't find a client, but
1593*f4b3ec61Sdh 	 * IPv6 does not fall back to IPv4.
1594*f4b3ec61Sdh 	 */
1595*f4b3ec61Sdh 	error = ldi_open_by_name(SCTP6DEV, FREAD|FWRITE, cr, &lh, li);
1596*f4b3ec61Sdh 	if (error) {
1597*f4b3ec61Sdh #ifdef DEBUG
1598*f4b3ec61Sdh 		printf("sctp_g_q_create: open of SCTP6DEV failed error %d\n",
1599*f4b3ec61Sdh 		    error);
1600*f4b3ec61Sdh #endif
1601*f4b3ec61Sdh 		goto out;
1602*f4b3ec61Sdh 	}
1603*f4b3ec61Sdh 
1604*f4b3ec61Sdh 	/*
1605*f4b3ec61Sdh 	 * This ioctl causes the sctp framework to cache a pointer to
1606*f4b3ec61Sdh 	 * this stream, so we don't want to close the stream after
1607*f4b3ec61Sdh 	 * this operation.
1608*f4b3ec61Sdh 	 * Use the kernel credentials that are for the zone we're in.
1609*f4b3ec61Sdh 	 */
1610*f4b3ec61Sdh 	error = ldi_ioctl(lh, SCTP_IOC_DEFAULT_Q,
1611*f4b3ec61Sdh 	    (intptr_t)0, FKIOCTL, cr, &rval);
1612*f4b3ec61Sdh 	if (error) {
1613*f4b3ec61Sdh #ifdef DEBUG
1614*f4b3ec61Sdh 		printf("sctp_g_q_create: ioctl SCTP_IOC_DEFAULT_Q failed "
1615*f4b3ec61Sdh 		    "error %d\n", error);
1616*f4b3ec61Sdh #endif
1617*f4b3ec61Sdh 		goto out;
1618*f4b3ec61Sdh 	}
1619*f4b3ec61Sdh 	sctps->sctps_g_q_lh = lh;	/* For sctp_g_q_inactive */
1620*f4b3ec61Sdh 	lh = NULL;
1621*f4b3ec61Sdh out:
1622*f4b3ec61Sdh 	/* Close layered handles */
1623*f4b3ec61Sdh 	if (li)
1624*f4b3ec61Sdh 		ldi_ident_release(li);
1625*f4b3ec61Sdh 	/* Keep cred around until _inactive needs it */
1626*f4b3ec61Sdh 	sctps->sctps_g_q_cr = cr;
1627*f4b3ec61Sdh }
1628*f4b3ec61Sdh 
1629*f4b3ec61Sdh /*
1630*f4b3ec61Sdh  * Remove the sctp_default queue so that new connections will not find it.
1631*f4b3ec61Sdh  * SCTP uses sctp_g_q for all transmission, so all sctp'ts implicitly
1632*f4b3ec61Sdh  * refer to it. Hence have each one have a reference on sctp_g_q_ref!
1633*f4b3ec61Sdh  *
1634*f4b3ec61Sdh  * We decrement the refcnt added in sctp_g_q_create. Once all the
1635*f4b3ec61Sdh  * sctp_t's which use the default go away, sctp_g_q_close will be called
1636*f4b3ec61Sdh  * and close the sctp_g_q. Once sctp_g_q is closed, sctp_close() will drop the
1637*f4b3ec61Sdh  * last reference count on the stack by calling netstack_rele().
1638*f4b3ec61Sdh  */
1639*f4b3ec61Sdh void
1640*f4b3ec61Sdh sctp_g_q_destroy(sctp_stack_t *sctps)
1641*f4b3ec61Sdh {
1642*f4b3ec61Sdh 	if (sctps->sctps_g_q == NULL) {
1643*f4b3ec61Sdh 		return;	/* Nothing to cleanup */
1644*f4b3ec61Sdh 	}
1645*f4b3ec61Sdh 	/*
1646*f4b3ec61Sdh 	 * Keep sctps_g_q and sctps_gsctp until the last reference has
1647*f4b3ec61Sdh 	 * dropped, since the output is always done using those.
1648*f4b3ec61Sdh 	 * Need to decrement twice to take sctp_g_q_create and
1649*f4b3ec61Sdh 	 * the gsctp reference into account so that sctp_g_q_inactive is called
1650*f4b3ec61Sdh 	 * when all but the default queue remains.
1651*f4b3ec61Sdh 	 */
1652*f4b3ec61Sdh #ifdef NS_DEBUG
1653*f4b3ec61Sdh 	(void) printf("sctp_g_q_destroy: ref %d\n",
1654*f4b3ec61Sdh 	    sctps->sctps_g_q_ref);
1655*f4b3ec61Sdh #endif
1656*f4b3ec61Sdh 	SCTP_G_Q_REFRELE(sctps);
1657*f4b3ec61Sdh }
1658*f4b3ec61Sdh 
1659*f4b3ec61Sdh /*
1660*f4b3ec61Sdh  * Called when last user (could be sctp_g_q_destroy) drops reference count
1661*f4b3ec61Sdh  * using SCTP_G_Q_REFRELE.
1662*f4b3ec61Sdh  * Run by sctp_q_q_inactive using a taskq.
1663*f4b3ec61Sdh  */
1664*f4b3ec61Sdh static void
1665*f4b3ec61Sdh sctp_g_q_close(void *arg)
1666*f4b3ec61Sdh {
1667*f4b3ec61Sdh 	sctp_stack_t *sctps = arg;
1668*f4b3ec61Sdh 	int error;
1669*f4b3ec61Sdh 	ldi_handle_t	lh = NULL;
1670*f4b3ec61Sdh 	ldi_ident_t	li = NULL;
1671*f4b3ec61Sdh 	cred_t		*cr;
1672*f4b3ec61Sdh 
1673*f4b3ec61Sdh 	lh = sctps->sctps_g_q_lh;
1674*f4b3ec61Sdh 	if (lh == NULL)
1675*f4b3ec61Sdh 		return;	/* Nothing to cleanup */
1676*f4b3ec61Sdh 
1677*f4b3ec61Sdh 	error = ldi_ident_from_major(IP_MAJ, &li);
1678*f4b3ec61Sdh 	if (error) {
1679*f4b3ec61Sdh #ifdef NS_DEBUG
1680*f4b3ec61Sdh 		printf("sctp_g_q_inactive: lyr ident get failed error %d\n",
1681*f4b3ec61Sdh 		    error);
1682*f4b3ec61Sdh #endif
1683*f4b3ec61Sdh 		return;
1684*f4b3ec61Sdh 	}
1685*f4b3ec61Sdh 
1686*f4b3ec61Sdh 	cr = sctps->sctps_g_q_cr;
1687*f4b3ec61Sdh 	sctps->sctps_g_q_cr = NULL;
1688*f4b3ec61Sdh 	ASSERT(cr != NULL);
1689*f4b3ec61Sdh 
1690*f4b3ec61Sdh 	/*
1691*f4b3ec61Sdh 	 * Make sure we can break the recursion when sctp_close decrements
1692*f4b3ec61Sdh 	 * the reference count causing g_q_inactive to be called again.
1693*f4b3ec61Sdh 	 */
1694*f4b3ec61Sdh 	sctps->sctps_g_q_lh = NULL;
16957c478bd9Sstevel@tonic-gate 
1696*f4b3ec61Sdh 	/* close the default queue */
1697*f4b3ec61Sdh 	(void) ldi_close(lh, FREAD|FWRITE, cr);
1698*f4b3ec61Sdh 
1699*f4b3ec61Sdh 	/* Close layered handles */
1700*f4b3ec61Sdh 	ldi_ident_release(li);
1701*f4b3ec61Sdh 	crfree(cr);
1702*f4b3ec61Sdh 
1703*f4b3ec61Sdh 	ASSERT(sctps->sctps_g_q != NULL);
1704*f4b3ec61Sdh 	sctps->sctps_g_q = NULL;
1705*f4b3ec61Sdh 	/*
1706*f4b3ec61Sdh 	 * Now free sctps_gsctp.
1707*f4b3ec61Sdh 	 */
1708*f4b3ec61Sdh 	ASSERT(sctps->sctps_gsctp != NULL);
1709*f4b3ec61Sdh 	sctp_closei_local(sctps->sctps_gsctp);
1710*f4b3ec61Sdh 	SCTP_CONDEMNED(sctps->sctps_gsctp);
1711*f4b3ec61Sdh 	SCTP_REFRELE(sctps->sctps_gsctp);
1712*f4b3ec61Sdh 	sctps->sctps_gsctp = NULL;
1713*f4b3ec61Sdh }
17147c478bd9Sstevel@tonic-gate 
1715*f4b3ec61Sdh /*
1716*f4b3ec61Sdh  * Called when last sctp_t drops reference count using SCTP_G_Q_REFRELE.
1717*f4b3ec61Sdh  *
1718*f4b3ec61Sdh  * Have to ensure that the ldi routines are not used by an
1719*f4b3ec61Sdh  * interrupt thread by using a taskq.
1720*f4b3ec61Sdh  */
1721*f4b3ec61Sdh void
1722*f4b3ec61Sdh sctp_g_q_inactive(sctp_stack_t *sctps)
1723*f4b3ec61Sdh {
1724*f4b3ec61Sdh 	if (sctps->sctps_g_q_lh == NULL)
1725*f4b3ec61Sdh 		return;	/* Nothing to cleanup */
1726*f4b3ec61Sdh 
1727*f4b3ec61Sdh 	ASSERT(sctps->sctps_g_q_ref == 0);
1728*f4b3ec61Sdh 	SCTP_G_Q_REFHOLD(sctps); /* Compensate for what g_q_destroy did */
1729*f4b3ec61Sdh 
1730*f4b3ec61Sdh 	if (servicing_interrupt()) {
1731*f4b3ec61Sdh 		(void) taskq_dispatch(sctp_taskq, sctp_g_q_close,
1732*f4b3ec61Sdh 			    (void *) sctps, TQ_SLEEP);
1733*f4b3ec61Sdh 	} else {
1734*f4b3ec61Sdh 		sctp_g_q_close(sctps);
17357c478bd9Sstevel@tonic-gate 	}
1736*f4b3ec61Sdh }
1737*f4b3ec61Sdh 
1738*f4b3ec61Sdh /* Run at module load time */
1739*f4b3ec61Sdh void
1740*f4b3ec61Sdh sctp_ddi_g_init(void)
1741*f4b3ec61Sdh {
1742*f4b3ec61Sdh 	IP_MAJ = ddi_name_to_major(IP);
17437c478bd9Sstevel@tonic-gate 
17447c478bd9Sstevel@tonic-gate 	/* Create sctp_t/conn_t cache */
17457c478bd9Sstevel@tonic-gate 	sctp_conn_cache_init();
17467c478bd9Sstevel@tonic-gate 
17477c478bd9Sstevel@tonic-gate 	/* Create the faddr cache */
17487c478bd9Sstevel@tonic-gate 	sctp_faddr_init();
17497c478bd9Sstevel@tonic-gate 
17507c478bd9Sstevel@tonic-gate 	/* Create the sets cache */
17517c478bd9Sstevel@tonic-gate 	sctp_sets_init();
17527c478bd9Sstevel@tonic-gate 
17537c478bd9Sstevel@tonic-gate 	/* Create the PR-SCTP sets cache */
17547c478bd9Sstevel@tonic-gate 	sctp_ftsn_sets_init();
17557c478bd9Sstevel@tonic-gate 
1756*f4b3ec61Sdh 	/* Initialize tables used for CRC calculation */
1757*f4b3ec61Sdh 	sctp_crc32_init();
1758*f4b3ec61Sdh 
1759*f4b3ec61Sdh 	sctp_taskq = taskq_create("sctp_taskq", 1, minclsyspri, 1, 1,
1760*f4b3ec61Sdh 	    TASKQ_PREPOPULATE);
1761*f4b3ec61Sdh 
1762*f4b3ec61Sdh 	/*
1763*f4b3ec61Sdh 	 * We want to be informed each time a stack is created or
1764*f4b3ec61Sdh 	 * destroyed in the kernel, so we can maintain the
1765*f4b3ec61Sdh 	 * set of sctp_stack_t's.
1766*f4b3ec61Sdh 	 */
1767*f4b3ec61Sdh 	netstack_register(NS_SCTP, sctp_stack_init, sctp_stack_shutdown,
1768*f4b3ec61Sdh 	    sctp_stack_fini);
1769*f4b3ec61Sdh }
1770*f4b3ec61Sdh 
1771*f4b3ec61Sdh static void *
1772*f4b3ec61Sdh sctp_stack_init(netstackid_t stackid, netstack_t *ns)
1773*f4b3ec61Sdh {
1774*f4b3ec61Sdh 	sctp_stack_t	*sctps;
1775*f4b3ec61Sdh 
1776*f4b3ec61Sdh 	sctps = kmem_zalloc(sizeof (*sctps), KM_SLEEP);
1777*f4b3ec61Sdh 	sctps->sctps_netstack = ns;
1778*f4b3ec61Sdh 
1779*f4b3ec61Sdh 	/* Initialize locks */
1780*f4b3ec61Sdh 	mutex_init(&sctps->sctps_g_q_lock, NULL, MUTEX_DEFAULT, NULL);
1781*f4b3ec61Sdh 	cv_init(&sctps->sctps_g_q_cv, NULL, CV_DEFAULT, NULL);
1782*f4b3ec61Sdh 	mutex_init(&sctps->sctps_g_lock, NULL, MUTEX_DEFAULT, NULL);
1783*f4b3ec61Sdh 	mutex_init(&sctps->sctps_epriv_port_lock, NULL, MUTEX_DEFAULT, NULL);
1784*f4b3ec61Sdh 	sctps->sctps_g_num_epriv_ports = SCTP_NUM_EPRIV_PORTS;
1785*f4b3ec61Sdh 	sctps->sctps_g_epriv_ports[0] = 2049;
1786*f4b3ec61Sdh 	sctps->sctps_g_epriv_ports[1] = 4045;
1787*f4b3ec61Sdh 
1788*f4b3ec61Sdh 	/* Initialize SCTP hash arrays. */
1789*f4b3ec61Sdh 	sctp_hash_init(sctps);
1790*f4b3ec61Sdh 
1791*f4b3ec61Sdh 	sctps->sctps_pad_mp = allocb(SCTP_ALIGN, BPRI_MED);
1792*f4b3ec61Sdh 	bzero(sctps->sctps_pad_mp->b_rptr, SCTP_ALIGN);
1793*f4b3ec61Sdh 	ASSERT(sctps->sctps_pad_mp);
1794*f4b3ec61Sdh 
1795*f4b3ec61Sdh 	if (!sctp_nd_init(sctps)) {
1796*f4b3ec61Sdh 		sctp_nd_free(sctps);
1797*f4b3ec61Sdh 	}
1798*f4b3ec61Sdh 
17997c478bd9Sstevel@tonic-gate 	/* Initialize the recvq taskq. */
1800*f4b3ec61Sdh 	sctp_rq_tq_init(sctps);
18017c478bd9Sstevel@tonic-gate 
18027c478bd9Sstevel@tonic-gate 	/* saddr init */
1803*f4b3ec61Sdh 	sctp_saddr_init(sctps);
18047c478bd9Sstevel@tonic-gate 
18057c478bd9Sstevel@tonic-gate 	/* Global SCTP PCB list. */
1806*f4b3ec61Sdh 	list_create(&sctps->sctps_g_list, sizeof (sctp_t),
18077c478bd9Sstevel@tonic-gate 	    offsetof(sctp_t, sctp_list));
18087c478bd9Sstevel@tonic-gate 
18097c478bd9Sstevel@tonic-gate 	/* Initialize sctp kernel stats. */
1810*f4b3ec61Sdh 	sctps->sctps_mibkp = sctp_kstat_init(stackid);
1811*f4b3ec61Sdh 	sctps->sctps_kstat =
1812*f4b3ec61Sdh 	    sctp_kstat2_init(stackid, &sctps->sctps_statistics);
1813*f4b3ec61Sdh 
1814*f4b3ec61Sdh 	return (sctps);
18157c478bd9Sstevel@tonic-gate }
18167c478bd9Sstevel@tonic-gate 
1817*f4b3ec61Sdh /*
1818*f4b3ec61Sdh  * Called when the module is about to be unloaded.
1819*f4b3ec61Sdh  */
18207c478bd9Sstevel@tonic-gate void
1821*f4b3ec61Sdh sctp_ddi_g_destroy(void)
18227c478bd9Sstevel@tonic-gate {
18237c478bd9Sstevel@tonic-gate 	/* Destroy sctp_t/conn_t caches */
18247c478bd9Sstevel@tonic-gate 	sctp_conn_cache_fini();
18257c478bd9Sstevel@tonic-gate 
18267c478bd9Sstevel@tonic-gate 	/* Destroy the faddr cache */
18277c478bd9Sstevel@tonic-gate 	sctp_faddr_fini();
18287c478bd9Sstevel@tonic-gate 
18297c478bd9Sstevel@tonic-gate 	/* Destroy the sets cache */
18307c478bd9Sstevel@tonic-gate 	sctp_sets_fini();
18317c478bd9Sstevel@tonic-gate 
18327c478bd9Sstevel@tonic-gate 	/* Destroy the PR-SCTP sets cache */
18337c478bd9Sstevel@tonic-gate 	sctp_ftsn_sets_fini();
18347c478bd9Sstevel@tonic-gate 
1835*f4b3ec61Sdh 	netstack_unregister(NS_SCTP);
1836*f4b3ec61Sdh 	taskq_destroy(sctp_taskq);
1837*f4b3ec61Sdh }
1838*f4b3ec61Sdh 
1839*f4b3ec61Sdh /*
1840*f4b3ec61Sdh  * Shut down the SCTP stack instance.
1841*f4b3ec61Sdh  */
1842*f4b3ec61Sdh /* ARGSUSED */
1843*f4b3ec61Sdh static void
1844*f4b3ec61Sdh sctp_stack_shutdown(netstackid_t stackid, void *arg)
1845*f4b3ec61Sdh {
1846*f4b3ec61Sdh 	sctp_stack_t *sctps = (sctp_stack_t *)arg;
1847*f4b3ec61Sdh 
1848*f4b3ec61Sdh 	sctp_g_q_destroy(sctps);
1849*f4b3ec61Sdh }
1850*f4b3ec61Sdh 
1851*f4b3ec61Sdh /*
1852*f4b3ec61Sdh  * Free the SCTP stack instance.
1853*f4b3ec61Sdh  */
1854*f4b3ec61Sdh static void
1855*f4b3ec61Sdh sctp_stack_fini(netstackid_t stackid, void *arg)
1856*f4b3ec61Sdh {
1857*f4b3ec61Sdh 	sctp_stack_t *sctps = (sctp_stack_t *)arg;
1858*f4b3ec61Sdh 
1859*f4b3ec61Sdh 	sctp_nd_free(sctps);
1860*f4b3ec61Sdh 
18617c478bd9Sstevel@tonic-gate 	/* Destroy the recvq taskqs. */
1862*f4b3ec61Sdh 	sctp_rq_tq_fini(sctps);
18637c478bd9Sstevel@tonic-gate 
18647c478bd9Sstevel@tonic-gate 	/* Destroy saddr  */
1865*f4b3ec61Sdh 	sctp_saddr_fini(sctps);
18667c478bd9Sstevel@tonic-gate 
18677c478bd9Sstevel@tonic-gate 	/* Global SCTP PCB list. */
1868*f4b3ec61Sdh 	list_destroy(&sctps->sctps_g_list);
18697c478bd9Sstevel@tonic-gate 
18707c478bd9Sstevel@tonic-gate 	/* Destroy SCTP hash arrays. */
1871*f4b3ec61Sdh 	sctp_hash_destroy(sctps);
1872*f4b3ec61Sdh 
1873*f4b3ec61Sdh 	/* Destroy SCTP kernel stats. */
1874*f4b3ec61Sdh 	sctp_kstat2_fini(stackid, sctps->sctps_kstat);
1875*f4b3ec61Sdh 	sctps->sctps_kstat = NULL;
1876*f4b3ec61Sdh 	bzero(&sctps->sctps_statistics, sizeof (sctps->sctps_statistics));
1877*f4b3ec61Sdh 
1878*f4b3ec61Sdh 	sctp_kstat_fini(stackid, sctps->sctps_mibkp);
1879*f4b3ec61Sdh 	sctps->sctps_mibkp = NULL;
1880*f4b3ec61Sdh 
1881*f4b3ec61Sdh 	freeb(sctps->sctps_pad_mp);
1882*f4b3ec61Sdh 	sctps->sctps_pad_mp = NULL;
18837c478bd9Sstevel@tonic-gate 
1884*f4b3ec61Sdh 	mutex_destroy(&sctps->sctps_g_lock);
1885*f4b3ec61Sdh 	mutex_destroy(&sctps->sctps_epriv_port_lock);
1886*f4b3ec61Sdh 	mutex_destroy(&sctps->sctps_g_q_lock);
1887*f4b3ec61Sdh 	cv_destroy(&sctps->sctps_g_q_cv);
18887c478bd9Sstevel@tonic-gate 
1889*f4b3ec61Sdh 	kmem_free(sctps, sizeof (*sctps));
18907c478bd9Sstevel@tonic-gate }
18917c478bd9Sstevel@tonic-gate 
18927c478bd9Sstevel@tonic-gate void
1893*f4b3ec61Sdh sctp_display_all(sctp_stack_t *sctps)
18947c478bd9Sstevel@tonic-gate {
18957c478bd9Sstevel@tonic-gate 	sctp_t *sctp_walker;
18967c478bd9Sstevel@tonic-gate 
1897*f4b3ec61Sdh 	mutex_enter(&sctps->sctps_g_lock);
1898*f4b3ec61Sdh 	for (sctp_walker = sctps->sctps_gsctp; sctp_walker != NULL;
1899*f4b3ec61Sdh 	    sctp_walker = (sctp_t *)list_next(&sctps->sctps_g_list,
1900*f4b3ec61Sdh 	    sctp_walker)) {
19017c478bd9Sstevel@tonic-gate 		(void) sctp_display(sctp_walker, NULL);
19027c478bd9Sstevel@tonic-gate 	}
1903*f4b3ec61Sdh 	mutex_exit(&sctps->sctps_g_lock);
19047c478bd9Sstevel@tonic-gate }
19057c478bd9Sstevel@tonic-gate 
19067c478bd9Sstevel@tonic-gate static void
1907*f4b3ec61Sdh sctp_rq_tq_init(sctp_stack_t *sctps)
19087c478bd9Sstevel@tonic-gate {
1909*f4b3ec61Sdh 	sctps->sctps_recvq_tq_list_max_sz = 16;
1910*f4b3ec61Sdh 	sctps->sctps_recvq_tq_list_cur_sz = 1;
19117c478bd9Sstevel@tonic-gate 	/*
19127c478bd9Sstevel@tonic-gate 	 * Initialize the recvq_tq_list and create the first recvq taskq.
19137c478bd9Sstevel@tonic-gate 	 * What to do if it fails?
19147c478bd9Sstevel@tonic-gate 	 */
1915*f4b3ec61Sdh 	sctps->sctps_recvq_tq_list =
1916*f4b3ec61Sdh 	    kmem_zalloc(sctps->sctps_recvq_tq_list_max_sz * sizeof (taskq_t *),
1917*f4b3ec61Sdh 		KM_SLEEP);
1918*f4b3ec61Sdh 	sctps->sctps_recvq_tq_list[0] = taskq_create("sctp_def_recvq_taskq",
19197c478bd9Sstevel@tonic-gate 	    MIN(sctp_recvq_tq_thr_max, MAX(sctp_recvq_tq_thr_min, ncpus)),
19207c478bd9Sstevel@tonic-gate 	    minclsyspri, sctp_recvq_tq_task_min, sctp_recvq_tq_task_max,
19217c478bd9Sstevel@tonic-gate 	    TASKQ_PREPOPULATE);
1922*f4b3ec61Sdh 	mutex_init(&sctps->sctps_rq_tq_lock, NULL, MUTEX_DEFAULT, NULL);
19237c478bd9Sstevel@tonic-gate }
19247c478bd9Sstevel@tonic-gate 
19257c478bd9Sstevel@tonic-gate static void
1926*f4b3ec61Sdh sctp_rq_tq_fini(sctp_stack_t *sctps)
19277c478bd9Sstevel@tonic-gate {
19287c478bd9Sstevel@tonic-gate 	int i;
19297c478bd9Sstevel@tonic-gate 
1930*f4b3ec61Sdh 	for (i = 0; i < sctps->sctps_recvq_tq_list_cur_sz; i++) {
1931*f4b3ec61Sdh 		ASSERT(sctps->sctps_recvq_tq_list[i] != NULL);
1932*f4b3ec61Sdh 		taskq_destroy(sctps->sctps_recvq_tq_list[i]);
19337c478bd9Sstevel@tonic-gate 	}
1934*f4b3ec61Sdh 	kmem_free(sctps->sctps_recvq_tq_list,
1935*f4b3ec61Sdh 	    sctps->sctps_recvq_tq_list_max_sz * sizeof (taskq_t *));
1936*f4b3ec61Sdh 	sctps->sctps_recvq_tq_list = NULL;
19377c478bd9Sstevel@tonic-gate }
19387c478bd9Sstevel@tonic-gate 
19397c478bd9Sstevel@tonic-gate /* Add another taskq for a new ill. */
19407c478bd9Sstevel@tonic-gate void
1941*f4b3ec61Sdh sctp_inc_taskq(sctp_stack_t *sctps)
19427c478bd9Sstevel@tonic-gate {
19437c478bd9Sstevel@tonic-gate 	taskq_t *tq;
19447c478bd9Sstevel@tonic-gate 	char tq_name[TASKQ_NAMELEN];
19457c478bd9Sstevel@tonic-gate 
1946*f4b3ec61Sdh 	mutex_enter(&sctps->sctps_rq_tq_lock);
1947*f4b3ec61Sdh 	if (sctps->sctps_recvq_tq_list_cur_sz + 1 >
1948*f4b3ec61Sdh 	    sctps->sctps_recvq_tq_list_max_sz) {
1949*f4b3ec61Sdh 		mutex_exit(&sctps->sctps_rq_tq_lock);
19507c478bd9Sstevel@tonic-gate 		cmn_err(CE_NOTE, "Cannot create more SCTP recvq taskq");
19517c478bd9Sstevel@tonic-gate 		return;
19527c478bd9Sstevel@tonic-gate 	}
19537c478bd9Sstevel@tonic-gate 
19547c478bd9Sstevel@tonic-gate 	(void) snprintf(tq_name, sizeof (tq_name), "sctp_recvq_taskq_%u",
1955*f4b3ec61Sdh 	    sctps->sctps_recvq_tq_list_cur_sz);
19567c478bd9Sstevel@tonic-gate 	tq = taskq_create(tq_name,
19577c478bd9Sstevel@tonic-gate 	    MIN(sctp_recvq_tq_thr_max, MAX(sctp_recvq_tq_thr_min, ncpus)),
19587c478bd9Sstevel@tonic-gate 	    minclsyspri, sctp_recvq_tq_task_min, sctp_recvq_tq_task_max,
19597c478bd9Sstevel@tonic-gate 	    TASKQ_PREPOPULATE);
19607c478bd9Sstevel@tonic-gate 	if (tq == NULL) {
1961*f4b3ec61Sdh 		mutex_exit(&sctps->sctps_rq_tq_lock);
19627c478bd9Sstevel@tonic-gate 		cmn_err(CE_NOTE, "SCTP recvq taskq creation failed");
19637c478bd9Sstevel@tonic-gate 		return;
19647c478bd9Sstevel@tonic-gate 	}
1965*f4b3ec61Sdh 	ASSERT(sctps->sctps_recvq_tq_list[
1966*f4b3ec61Sdh 		    sctps->sctps_recvq_tq_list_cur_sz] == NULL);
1967*f4b3ec61Sdh 	sctps->sctps_recvq_tq_list[sctps->sctps_recvq_tq_list_cur_sz] = tq;
1968*f4b3ec61Sdh 	atomic_add_32(&sctps->sctps_recvq_tq_list_cur_sz, 1);
1969*f4b3ec61Sdh 	mutex_exit(&sctps->sctps_rq_tq_lock);
19707c478bd9Sstevel@tonic-gate }
19717c478bd9Sstevel@tonic-gate 
19727c478bd9Sstevel@tonic-gate #ifdef DEBUG
19737c478bd9Sstevel@tonic-gate uint32_t sendq_loop_cnt = 0;
19747c478bd9Sstevel@tonic-gate uint32_t sendq_collision = 0;
19757c478bd9Sstevel@tonic-gate uint32_t sendq_empty = 0;
19767c478bd9Sstevel@tonic-gate #endif
19777c478bd9Sstevel@tonic-gate 
19787c478bd9Sstevel@tonic-gate void
19797c478bd9Sstevel@tonic-gate sctp_add_sendq(sctp_t *sctp, mblk_t *mp)
19807c478bd9Sstevel@tonic-gate {
19817c478bd9Sstevel@tonic-gate 	mutex_enter(&sctp->sctp_sendq_lock);
19827c478bd9Sstevel@tonic-gate 	if (sctp->sctp_sendq == NULL) {
19837c478bd9Sstevel@tonic-gate 		sctp->sctp_sendq = mp;
19847c478bd9Sstevel@tonic-gate 		sctp->sctp_sendq_tail = mp;
19857c478bd9Sstevel@tonic-gate 	} else {
19867c478bd9Sstevel@tonic-gate 		sctp->sctp_sendq_tail->b_next = mp;
19877c478bd9Sstevel@tonic-gate 		sctp->sctp_sendq_tail = mp;
19887c478bd9Sstevel@tonic-gate 	}
19897c478bd9Sstevel@tonic-gate 	mutex_exit(&sctp->sctp_sendq_lock);
19907c478bd9Sstevel@tonic-gate }
19917c478bd9Sstevel@tonic-gate 
19927c478bd9Sstevel@tonic-gate void
19937c478bd9Sstevel@tonic-gate sctp_process_sendq(sctp_t *sctp)
19947c478bd9Sstevel@tonic-gate {
19957c478bd9Sstevel@tonic-gate 	mblk_t *mp;
19967c478bd9Sstevel@tonic-gate #ifdef DEBUG
19977c478bd9Sstevel@tonic-gate 	uint32_t loop_cnt = 0;
19987c478bd9Sstevel@tonic-gate #endif
19997c478bd9Sstevel@tonic-gate 
20007c478bd9Sstevel@tonic-gate 	mutex_enter(&sctp->sctp_sendq_lock);
20017c478bd9Sstevel@tonic-gate 	if (sctp->sctp_sendq == NULL || sctp->sctp_sendq_sending) {
20027c478bd9Sstevel@tonic-gate #ifdef DEBUG
20037c478bd9Sstevel@tonic-gate 		if (sctp->sctp_sendq == NULL)
20047c478bd9Sstevel@tonic-gate 			sendq_empty++;
20057c478bd9Sstevel@tonic-gate 		else
20067c478bd9Sstevel@tonic-gate 			sendq_collision++;
20077c478bd9Sstevel@tonic-gate #endif
20087c478bd9Sstevel@tonic-gate 		mutex_exit(&sctp->sctp_sendq_lock);
20097c478bd9Sstevel@tonic-gate 		return;
20107c478bd9Sstevel@tonic-gate 	}
20117c478bd9Sstevel@tonic-gate 	sctp->sctp_sendq_sending = B_TRUE;
20127c478bd9Sstevel@tonic-gate 
20137c478bd9Sstevel@tonic-gate 	/*
20147c478bd9Sstevel@tonic-gate 	 * Note that while we are in this loop, other thread can put
20157c478bd9Sstevel@tonic-gate 	 * new packets in the receive queue.  We may be looping for
20167c478bd9Sstevel@tonic-gate 	 * quite a while.  This is OK even for an interrupt thread.
20177c478bd9Sstevel@tonic-gate 	 * The reason is that SCTP should only able to send a limited
20187c478bd9Sstevel@tonic-gate 	 * number of packets out in a burst.  So the number of times
20197c478bd9Sstevel@tonic-gate 	 * we go through this loop should not be many.
20207c478bd9Sstevel@tonic-gate 	 */
20217c478bd9Sstevel@tonic-gate 	while ((mp = sctp->sctp_sendq) != NULL) {
20227c478bd9Sstevel@tonic-gate 		sctp->sctp_sendq = mp->b_next;
20237c478bd9Sstevel@tonic-gate 		ASSERT(sctp->sctp_connp->conn_ref > 0);
20247c478bd9Sstevel@tonic-gate 		mutex_exit(&sctp->sctp_sendq_lock);
20257c478bd9Sstevel@tonic-gate 		mp->b_next = NULL;
20267c478bd9Sstevel@tonic-gate 		CONN_INC_REF(sctp->sctp_connp);
20277c478bd9Sstevel@tonic-gate 		mp->b_flag |= MSGHASREF;
20287c478bd9Sstevel@tonic-gate 		/* If we don't have sctp_current, default to IPv4 */
20297c478bd9Sstevel@tonic-gate 		IP_PUT(mp, sctp->sctp_connp, sctp->sctp_current == NULL ?
20307c478bd9Sstevel@tonic-gate 		    B_TRUE : sctp->sctp_current->isv4);
20317c478bd9Sstevel@tonic-gate 		BUMP_LOCAL(sctp->sctp_opkts);
20327c478bd9Sstevel@tonic-gate #ifdef DEBUG
20337c478bd9Sstevel@tonic-gate 		loop_cnt++;
20347c478bd9Sstevel@tonic-gate #endif
20357c478bd9Sstevel@tonic-gate 		mutex_enter(&sctp->sctp_sendq_lock);
20367c478bd9Sstevel@tonic-gate 	}
20377c478bd9Sstevel@tonic-gate 
20387c478bd9Sstevel@tonic-gate 	sctp->sctp_sendq_tail = NULL;
20397c478bd9Sstevel@tonic-gate 	sctp->sctp_sendq_sending = B_FALSE;
20407c478bd9Sstevel@tonic-gate #ifdef DEBUG
20417c478bd9Sstevel@tonic-gate 	if (loop_cnt > sendq_loop_cnt)
20427c478bd9Sstevel@tonic-gate 		sendq_loop_cnt = loop_cnt;
20437c478bd9Sstevel@tonic-gate #endif
20447c478bd9Sstevel@tonic-gate 	mutex_exit(&sctp->sctp_sendq_lock);
20457c478bd9Sstevel@tonic-gate }
20467c478bd9Sstevel@tonic-gate 
20477c478bd9Sstevel@tonic-gate #ifdef DEBUG
20487c478bd9Sstevel@tonic-gate uint32_t recvq_loop_cnt = 0;
20497c478bd9Sstevel@tonic-gate uint32_t recvq_call = 0;
20507c478bd9Sstevel@tonic-gate #endif
20517c478bd9Sstevel@tonic-gate 
20527c478bd9Sstevel@tonic-gate /*
20537c478bd9Sstevel@tonic-gate  * Find the next recvq_tq to use.  This routine will go thru all the
20547c478bd9Sstevel@tonic-gate  * taskqs until it can dispatch a job for the sctp.  If this fails,
20557c478bd9Sstevel@tonic-gate  * it will create a new taskq and try it.
20567c478bd9Sstevel@tonic-gate  */
20577c478bd9Sstevel@tonic-gate static boolean_t
20587c478bd9Sstevel@tonic-gate sctp_find_next_tq(sctp_t *sctp)
20597c478bd9Sstevel@tonic-gate {
20607c478bd9Sstevel@tonic-gate 	int next_tq, try;
20617c478bd9Sstevel@tonic-gate 	taskq_t *tq;
2062*f4b3ec61Sdh 	sctp_stack_t	*sctps = sctp->sctp_sctps;
20637c478bd9Sstevel@tonic-gate 
20647c478bd9Sstevel@tonic-gate 	/*
20657c478bd9Sstevel@tonic-gate 	 * Note that since we don't hold a lock on sctp_rq_tq_lock for
20667c478bd9Sstevel@tonic-gate 	 * performance reason, recvq_ta_list_cur_sz can be changed during
20677c478bd9Sstevel@tonic-gate 	 * this loop.  The problem this will create is that the loop may
20687c478bd9Sstevel@tonic-gate 	 * not have tried all the recvq_tq.  This should be OK.
20697c478bd9Sstevel@tonic-gate 	 */
2070*f4b3ec61Sdh 	next_tq = atomic_add_32_nv(&sctps->sctps_recvq_tq_list_cur, 1) %
2071*f4b3ec61Sdh 	    sctps->sctps_recvq_tq_list_cur_sz;
2072*f4b3ec61Sdh 	for (try = 0; try < sctps->sctps_recvq_tq_list_cur_sz; try++) {
2073*f4b3ec61Sdh 		tq = sctps->sctps_recvq_tq_list[next_tq];
20747c478bd9Sstevel@tonic-gate 		if (taskq_dispatch(tq, sctp_process_recvq, sctp,
20757c478bd9Sstevel@tonic-gate 		    TQ_NOSLEEP) != NULL) {
20767c478bd9Sstevel@tonic-gate 			sctp->sctp_recvq_tq = tq;
20777c478bd9Sstevel@tonic-gate 			return (B_TRUE);
20787c478bd9Sstevel@tonic-gate 		}
2079*f4b3ec61Sdh 		next_tq = (next_tq + 1) % sctps->sctps_recvq_tq_list_cur_sz;
20807c478bd9Sstevel@tonic-gate 	}
20817c478bd9Sstevel@tonic-gate 
20827c478bd9Sstevel@tonic-gate 	/*
20837c478bd9Sstevel@tonic-gate 	 * Create one more taskq and try it.  Note that sctp_inc_taskq()
20847c478bd9Sstevel@tonic-gate 	 * may not have created another taskq if the number of recvq
20857c478bd9Sstevel@tonic-gate 	 * taskqs is at the maximum.  We are probably in a pretty bad
20867c478bd9Sstevel@tonic-gate 	 * shape if this actually happens...
20877c478bd9Sstevel@tonic-gate 	 */
2088*f4b3ec61Sdh 	sctp_inc_taskq(sctps);
2089*f4b3ec61Sdh 	tq = sctps->sctps_recvq_tq_list[sctps->sctps_recvq_tq_list_cur_sz - 1];
20907c478bd9Sstevel@tonic-gate 	if (taskq_dispatch(tq, sctp_process_recvq, sctp, TQ_NOSLEEP) != NULL) {
20917c478bd9Sstevel@tonic-gate 		sctp->sctp_recvq_tq = tq;
20927c478bd9Sstevel@tonic-gate 		return (B_TRUE);
20937c478bd9Sstevel@tonic-gate 	}
2094*f4b3ec61Sdh 	SCTP_KSTAT(sctps, sctp_find_next_tq);
20957c478bd9Sstevel@tonic-gate 	return (B_FALSE);
20967c478bd9Sstevel@tonic-gate }
20977c478bd9Sstevel@tonic-gate 
20987c478bd9Sstevel@tonic-gate /*
20997c478bd9Sstevel@tonic-gate  * To add a message to the recvq.  Note that the sctp_timer_fire()
21007c478bd9Sstevel@tonic-gate  * routine also uses this function to add the timer message to the
21017c478bd9Sstevel@tonic-gate  * receive queue for later processing.  And it should be the only
21027c478bd9Sstevel@tonic-gate  * caller of sctp_add_recvq() which sets the try_harder argument
21037c478bd9Sstevel@tonic-gate  * to B_TRUE.
21047c478bd9Sstevel@tonic-gate  *
21057c478bd9Sstevel@tonic-gate  * If the try_harder argument is B_TRUE, this routine sctp_find_next_tq()
21067c478bd9Sstevel@tonic-gate  * will try very hard to dispatch the task.  Refer to the comment
21077c478bd9Sstevel@tonic-gate  * for that routine on how it does that.
21087c478bd9Sstevel@tonic-gate  */
21097c478bd9Sstevel@tonic-gate boolean_t
21107c478bd9Sstevel@tonic-gate sctp_add_recvq(sctp_t *sctp, mblk_t *mp, boolean_t caller_hold_lock)
21117c478bd9Sstevel@tonic-gate {
21127c478bd9Sstevel@tonic-gate 	if (!caller_hold_lock)
21137c478bd9Sstevel@tonic-gate 		mutex_enter(&sctp->sctp_recvq_lock);
21147c478bd9Sstevel@tonic-gate 
21157c478bd9Sstevel@tonic-gate 	/* If the taskq dispatch has not been scheduled, do it now. */
21167c478bd9Sstevel@tonic-gate 	if (sctp->sctp_recvq_tq == NULL) {
21177c478bd9Sstevel@tonic-gate 		ASSERT(sctp->sctp_recvq == NULL);
21187c478bd9Sstevel@tonic-gate 		if (!sctp_find_next_tq(sctp)) {
21197c478bd9Sstevel@tonic-gate 			if (!caller_hold_lock)
21207c478bd9Sstevel@tonic-gate 				mutex_exit(&sctp->sctp_recvq_lock);
21217c478bd9Sstevel@tonic-gate 			return (B_FALSE);
21227c478bd9Sstevel@tonic-gate 		}
21237c478bd9Sstevel@tonic-gate 		/* Make sure the sctp_t will not go away. */
21247c478bd9Sstevel@tonic-gate 		SCTP_REFHOLD(sctp);
21257c478bd9Sstevel@tonic-gate 	}
21267c478bd9Sstevel@tonic-gate 
21277c478bd9Sstevel@tonic-gate 	if (sctp->sctp_recvq == NULL) {
21287c478bd9Sstevel@tonic-gate 		sctp->sctp_recvq = mp;
21297c478bd9Sstevel@tonic-gate 		sctp->sctp_recvq_tail = mp;
21307c478bd9Sstevel@tonic-gate 	} else {
21317c478bd9Sstevel@tonic-gate 		sctp->sctp_recvq_tail->b_next = mp;
21327c478bd9Sstevel@tonic-gate 		sctp->sctp_recvq_tail = mp;
21337c478bd9Sstevel@tonic-gate 	}
21347c478bd9Sstevel@tonic-gate 
21357c478bd9Sstevel@tonic-gate 	if (!caller_hold_lock)
21367c478bd9Sstevel@tonic-gate 		mutex_exit(&sctp->sctp_recvq_lock);
21377c478bd9Sstevel@tonic-gate 	return (B_TRUE);
21387c478bd9Sstevel@tonic-gate }
21397c478bd9Sstevel@tonic-gate 
21407c478bd9Sstevel@tonic-gate static void
21417c478bd9Sstevel@tonic-gate sctp_process_recvq(void *arg)
21427c478bd9Sstevel@tonic-gate {
21437c478bd9Sstevel@tonic-gate 	sctp_t		*sctp = (sctp_t *)arg;
21447c478bd9Sstevel@tonic-gate 	mblk_t		*mp;
21457c478bd9Sstevel@tonic-gate 	mblk_t		*ipsec_mp;
21467c478bd9Sstevel@tonic-gate #ifdef DEBUG
21477c478bd9Sstevel@tonic-gate 	uint32_t	loop_cnt = 0;
21487c478bd9Sstevel@tonic-gate #endif
21497c478bd9Sstevel@tonic-gate 
21507c478bd9Sstevel@tonic-gate #ifdef	_BIG_ENDIAN
21517c478bd9Sstevel@tonic-gate #define	IPVER(ip6h)	((((uint32_t *)ip6h)[0] >> 28) & 0x7)
21527c478bd9Sstevel@tonic-gate #else
21537c478bd9Sstevel@tonic-gate #define	IPVER(ip6h)	((((uint32_t *)ip6h)[0] >> 4) & 0x7)
21547c478bd9Sstevel@tonic-gate #endif
21557c478bd9Sstevel@tonic-gate 
21567c478bd9Sstevel@tonic-gate 	RUN_SCTP(sctp);
21577c478bd9Sstevel@tonic-gate 	mutex_enter(&sctp->sctp_recvq_lock);
21587c478bd9Sstevel@tonic-gate 
21597c478bd9Sstevel@tonic-gate #ifdef DEBUG
21607c478bd9Sstevel@tonic-gate 	recvq_call++;
21617c478bd9Sstevel@tonic-gate #endif
21627c478bd9Sstevel@tonic-gate 	/*
21637c478bd9Sstevel@tonic-gate 	 * Note that while we are in this loop, other thread can put
21647c478bd9Sstevel@tonic-gate 	 * new packets in the receive queue.  We may be looping for
21657c478bd9Sstevel@tonic-gate 	 * quite a while.
21667c478bd9Sstevel@tonic-gate 	 */
21677c478bd9Sstevel@tonic-gate 	while ((mp = sctp->sctp_recvq) != NULL) {
21687c478bd9Sstevel@tonic-gate 		sctp->sctp_recvq = mp->b_next;
21697c478bd9Sstevel@tonic-gate 		mutex_exit(&sctp->sctp_recvq_lock);
21707c478bd9Sstevel@tonic-gate 		mp->b_next = NULL;
21717c478bd9Sstevel@tonic-gate #ifdef DEBUG
21727c478bd9Sstevel@tonic-gate 		loop_cnt++;
21737c478bd9Sstevel@tonic-gate #endif
21747c478bd9Sstevel@tonic-gate 		ipsec_mp = mp->b_prev;
21757c478bd9Sstevel@tonic-gate 		mp->b_prev = NULL;
21767c478bd9Sstevel@tonic-gate 		sctp_input_data(sctp, mp, ipsec_mp);
21777c478bd9Sstevel@tonic-gate 
21787c478bd9Sstevel@tonic-gate 		mutex_enter(&sctp->sctp_recvq_lock);
21797c478bd9Sstevel@tonic-gate 	}
21807c478bd9Sstevel@tonic-gate 
21817c478bd9Sstevel@tonic-gate 	sctp->sctp_recvq_tail = NULL;
21827c478bd9Sstevel@tonic-gate 	sctp->sctp_recvq_tq = NULL;
21837c478bd9Sstevel@tonic-gate 
21847c478bd9Sstevel@tonic-gate 	mutex_exit(&sctp->sctp_recvq_lock);
21857c478bd9Sstevel@tonic-gate 
21867c478bd9Sstevel@tonic-gate 	WAKE_SCTP(sctp);
21877c478bd9Sstevel@tonic-gate 
21887c478bd9Sstevel@tonic-gate 	/* We may have sent something when processing the receive queue. */
21897c478bd9Sstevel@tonic-gate 	sctp_process_sendq(sctp);
21907c478bd9Sstevel@tonic-gate #ifdef DEBUG
21917c478bd9Sstevel@tonic-gate 	if (loop_cnt > recvq_loop_cnt)
21927c478bd9Sstevel@tonic-gate 		recvq_loop_cnt = loop_cnt;
21937c478bd9Sstevel@tonic-gate #endif
21947c478bd9Sstevel@tonic-gate 	/* Now it can go away. */
21957c478bd9Sstevel@tonic-gate 	SCTP_REFRELE(sctp);
21967c478bd9Sstevel@tonic-gate }
21977c478bd9Sstevel@tonic-gate 
21987c478bd9Sstevel@tonic-gate /* ARGSUSED */
21997c478bd9Sstevel@tonic-gate static int
22007c478bd9Sstevel@tonic-gate sctp_conn_cache_constructor(void *buf, void *cdrarg, int kmflags)
22017c478bd9Sstevel@tonic-gate {
22027c478bd9Sstevel@tonic-gate 	conn_t	*sctp_connp = (conn_t *)buf;
22037c478bd9Sstevel@tonic-gate 	sctp_t	*sctp = (sctp_t *)&sctp_connp[1];
22047c478bd9Sstevel@tonic-gate 
22057c478bd9Sstevel@tonic-gate 	bzero(buf, (char *)&sctp[1] - (char *)buf);
22067c478bd9Sstevel@tonic-gate 
22077c478bd9Sstevel@tonic-gate 	sctp->sctp_connp = sctp_connp;
22087c478bd9Sstevel@tonic-gate 	mutex_init(&sctp->sctp_reflock, NULL, MUTEX_DEFAULT, NULL);
22097c478bd9Sstevel@tonic-gate 	mutex_init(&sctp->sctp_lock, NULL, MUTEX_DEFAULT, NULL);
22107c478bd9Sstevel@tonic-gate 	mutex_init(&sctp->sctp_recvq_lock, NULL, MUTEX_DEFAULT, NULL);
22117c478bd9Sstevel@tonic-gate 	cv_init(&sctp->sctp_cv, NULL, CV_DEFAULT, NULL);
22127c478bd9Sstevel@tonic-gate 	mutex_init(&sctp->sctp_sendq_lock, NULL, MUTEX_DEFAULT, NULL);
22137c478bd9Sstevel@tonic-gate 
22147c478bd9Sstevel@tonic-gate 	sctp_connp->conn_rq = sctp_connp->conn_wq = NULL;
22157c478bd9Sstevel@tonic-gate 	sctp_connp->conn_multicast_loop = IP_DEFAULT_MULTICAST_LOOP;
22167c478bd9Sstevel@tonic-gate 	sctp_connp->conn_ulp = IPPROTO_SCTP;
22177c478bd9Sstevel@tonic-gate 	mutex_init(&sctp_connp->conn_lock, NULL, MUTEX_DEFAULT, NULL);
22187c478bd9Sstevel@tonic-gate 	cv_init(&sctp_connp->conn_cv, NULL, CV_DEFAULT, NULL);
22197c478bd9Sstevel@tonic-gate 
22207c478bd9Sstevel@tonic-gate 	return (0);
22217c478bd9Sstevel@tonic-gate }
22227c478bd9Sstevel@tonic-gate 
22237c478bd9Sstevel@tonic-gate /* ARGSUSED */
22247c478bd9Sstevel@tonic-gate static void
22257c478bd9Sstevel@tonic-gate sctp_conn_cache_destructor(void *buf, void *cdrarg)
22267c478bd9Sstevel@tonic-gate {
22277c478bd9Sstevel@tonic-gate 	conn_t	*sctp_connp = (conn_t *)buf;
22287c478bd9Sstevel@tonic-gate 	sctp_t	*sctp = (sctp_t *)&sctp_connp[1];
22297c478bd9Sstevel@tonic-gate 
22307c478bd9Sstevel@tonic-gate 	ASSERT(!MUTEX_HELD(&sctp->sctp_lock));
22317c478bd9Sstevel@tonic-gate 	ASSERT(!MUTEX_HELD(&sctp->sctp_reflock));
22327c478bd9Sstevel@tonic-gate 	ASSERT(!MUTEX_HELD(&sctp->sctp_recvq_lock));
22337c478bd9Sstevel@tonic-gate 	ASSERT(!MUTEX_HELD(&sctp->sctp_sendq_lock));
22347c478bd9Sstevel@tonic-gate 	ASSERT(!MUTEX_HELD(&sctp->sctp_connp->conn_lock));
22357c478bd9Sstevel@tonic-gate 
22367c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_conn_hash_next == NULL);
22377c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_conn_hash_prev == NULL);
22387c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_listen_hash_next == NULL);
22397c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_listen_hash_prev == NULL);
22407c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_listen_tfp == NULL);
22417c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_conn_tfp == NULL);
22427c478bd9Sstevel@tonic-gate 
22437c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_faddrs == NULL);
22447c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_nsaddrs == 0);
22457c478bd9Sstevel@tonic-gate 
22467c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_ulpd == NULL);
22477c478bd9Sstevel@tonic-gate 
22487c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_lastfaddr == NULL);
22497c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_primary == NULL);
22507c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_current == NULL);
22517c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_lastdata == NULL);
22527c478bd9Sstevel@tonic-gate 
22537c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_xmit_head == NULL);
22547c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_xmit_tail == NULL);
22557c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_xmit_unsent == NULL);
22567c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_xmit_unsent_tail == NULL);
22577c478bd9Sstevel@tonic-gate 
22587c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_ostrcntrs == NULL);
22597c478bd9Sstevel@tonic-gate 
22607c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_sack_info == NULL);
22617c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_ack_mp == NULL);
22627c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_instr == NULL);
22637c478bd9Sstevel@tonic-gate 
22647c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_iphc == NULL);
22657c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_iphc6 == NULL);
22667c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_ipha == NULL);
22677c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_ip6h == NULL);
22687c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_sctph == NULL);
22697c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_sctph6 == NULL);
22707c478bd9Sstevel@tonic-gate 
22717c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_cookie_mp == NULL);
22727c478bd9Sstevel@tonic-gate 
22737c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_refcnt == 0);
22747c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_timer_mp == NULL);
22757c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_connp->conn_ref == 0);
22767c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_heartbeat_mp == NULL);
22777c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_ptpbhn == NULL && sctp->sctp_bind_hash == NULL);
22787c478bd9Sstevel@tonic-gate 
22797c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_shutdown_faddr == NULL);
22807c478bd9Sstevel@tonic-gate 
22817c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_cxmit_list == NULL);
22827c478bd9Sstevel@tonic-gate 
22837c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_recvq == NULL);
22847c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_recvq_tail == NULL);
22857c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_recvq_tq == NULL);
22867c478bd9Sstevel@tonic-gate 
22877c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_sendq == NULL);
22887c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_sendq_tail == NULL);
22897c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_sendq_sending == B_FALSE);
22907c478bd9Sstevel@tonic-gate 
22917f99e6fcSkp 	ASSERT(sctp->sctp_ipp_hopopts == NULL);
22927f99e6fcSkp 	ASSERT(sctp->sctp_ipp_rtdstopts == NULL);
22937f99e6fcSkp 	ASSERT(sctp->sctp_ipp_rthdr == NULL);
22947f99e6fcSkp 	ASSERT(sctp->sctp_ipp_dstopts == NULL);
22957f99e6fcSkp 	ASSERT(sctp->sctp_ipp_pathmtu == NULL);
22967f99e6fcSkp 
22977c478bd9Sstevel@tonic-gate 	mutex_destroy(&sctp->sctp_reflock);
22987c478bd9Sstevel@tonic-gate 	mutex_destroy(&sctp->sctp_lock);
22997c478bd9Sstevel@tonic-gate 	mutex_destroy(&sctp->sctp_recvq_lock);
23007c478bd9Sstevel@tonic-gate 	cv_destroy(&sctp->sctp_cv);
23017c478bd9Sstevel@tonic-gate 	mutex_destroy(&sctp->sctp_sendq_lock);
23027c478bd9Sstevel@tonic-gate 
23037c478bd9Sstevel@tonic-gate 	mutex_destroy(&sctp_connp->conn_lock);
23047c478bd9Sstevel@tonic-gate 	cv_destroy(&sctp_connp->conn_cv);
23057c478bd9Sstevel@tonic-gate }
23067c478bd9Sstevel@tonic-gate 
23077c478bd9Sstevel@tonic-gate static void
23087c478bd9Sstevel@tonic-gate sctp_conn_cache_init()
23097c478bd9Sstevel@tonic-gate {
23107c478bd9Sstevel@tonic-gate 	sctp_conn_cache = kmem_cache_create("sctp_conn_cache",
23117c478bd9Sstevel@tonic-gate 	    sizeof (sctp_t) + sizeof (conn_t), 0, sctp_conn_cache_constructor,
23127c478bd9Sstevel@tonic-gate 	    sctp_conn_cache_destructor, NULL, NULL, NULL, 0);
23137c478bd9Sstevel@tonic-gate }
23147c478bd9Sstevel@tonic-gate 
23157c478bd9Sstevel@tonic-gate static void
23167c478bd9Sstevel@tonic-gate sctp_conn_cache_fini()
23177c478bd9Sstevel@tonic-gate {
23187c478bd9Sstevel@tonic-gate 	kmem_cache_destroy(sctp_conn_cache);
23197c478bd9Sstevel@tonic-gate }
2320