xref: /illumos-gate/usr/src/uts/common/inet/sctp/sctp.c (revision a0263d1c)
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 /*
23f4b3ec61Sdh  * 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>
46f4b3ec61Sdh #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>
64f4b3ec61Sdh #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 
82f4b3ec61Sdh /*
83f4b3ec61Sdh  * Have to ensure that sctp_g_q_close is not done by an
84f4b3ec61Sdh  * interrupt thread.
85f4b3ec61Sdh  */
86f4b3ec61Sdh 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 *);
92f4b3ec61Sdh static void	sctp_rq_tq_init(sctp_stack_t *);
93f4b3ec61Sdh 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();
98f4b3ec61Sdh void		sctp_g_q_setup(sctp_stack_t *);
99f4b3ec61Sdh void		sctp_g_q_create(sctp_stack_t *);
100f4b3ec61Sdh void		sctp_g_q_destroy(sctp_stack_t *);
101f4b3ec61Sdh 
102f4b3ec61Sdh static void	*sctp_stack_init(netstackid_t stackid, netstack_t *ns);
103f4b3ec61Sdh static void	sctp_stack_shutdown(netstackid_t stackid, void *arg);
104f4b3ec61Sdh 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 
122f4b3ec61Sdh /* /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. */
141f4b3ec61Sdh #define	SCTP_LINK(sctp, sctps)				\
142f4b3ec61Sdh 	mutex_enter(&(sctps)->sctps_g_lock);		\
143f4b3ec61Sdh 	list_insert_tail(&sctps->sctps_g_list, (sctp));	\
144f4b3ec61Sdh 	mutex_exit(&(sctps)->sctps_g_lock);
1457c478bd9Sstevel@tonic-gate 
146f4b3ec61Sdh #define	SCTP_UNLINK(sctp, sctps)				\
147f4b3ec61Sdh 	mutex_enter(&(sctps)->sctps_g_lock);		\
1487c478bd9Sstevel@tonic-gate 	ASSERT((sctp)->sctp_condemned);			\
149f4b3ec61Sdh 	list_remove(&(sctps)->sctps_g_list, (sctp));	\
150f4b3ec61Sdh 	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;
187f4b3ec61Sdh 	sctp_stack_t	*sctps = psctp->sctp_sctps;
1887c478bd9Sstevel@tonic-gate 
189f4b3ec61Sdh 	if ((connp = ipcl_conn_create(IPCL_SCTPCONN, KM_NOSLEEP,
190f4b3ec61Sdh 		    sctps->sctps_netstack)) == NULL) {
1917c478bd9Sstevel@tonic-gate 		return (NULL);
192f4b3ec61Sdh 	}
19345916cd2Sjpk 
19445916cd2Sjpk 	connp->conn_ulp_labeled = is_system_labeled();
19545916cd2Sjpk 
1967c478bd9Sstevel@tonic-gate 	sctp = CONN2SCTP(connp);
197f4b3ec61Sdh 	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);
203f4b3ec61Sdh 		netstack_rele(sctps->sctps_netstack);
204f4b3ec61Sdh 		connp->conn_netstack = NULL;
205f4b3ec61Sdh 		sctp->sctp_sctps = NULL;
206f4b3ec61Sdh 		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);
224f4b3ec61Sdh 		netstack_rele(sctps->sctps_netstack);
225f4b3ec61Sdh 		connp->conn_netstack = NULL;
226f4b3ec61Sdh 		sctp->sctp_sctps = NULL;
227f4b3ec61Sdh 		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 	 */
256f4b3ec61Sdh 	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;
627*a0263d1cSnh 			sctp->sctp_instr[i].istr_nmsgs = 0;
6287c478bd9Sstevel@tonic-gate 			sctp_free_reass((sctp->sctp_instr) + i);
6297c478bd9Sstevel@tonic-gate 			sctp->sctp_instr[i].nextseq = 0;
6307c478bd9Sstevel@tonic-gate 		}
6317c478bd9Sstevel@tonic-gate 		if (free) {
6327c478bd9Sstevel@tonic-gate 			kmem_free(sctp->sctp_instr,
6337c478bd9Sstevel@tonic-gate 			    sizeof (*sctp->sctp_instr) * sctp->sctp_num_istr);
6347c478bd9Sstevel@tonic-gate 			sctp->sctp_instr = NULL;
6357c478bd9Sstevel@tonic-gate 			sctp->sctp_num_istr = 0;
6367c478bd9Sstevel@tonic-gate 		}
6377c478bd9Sstevel@tonic-gate 	}
6387c478bd9Sstevel@tonic-gate 	/* un-ordered fragments */
6397c478bd9Sstevel@tonic-gate 	if (sctp->sctp_uo_frags != NULL) {
6407c478bd9Sstevel@tonic-gate 		for (mp = sctp->sctp_uo_frags; mp != NULL; mp = mp1) {
6417c478bd9Sstevel@tonic-gate 			mp1 = mp->b_next;
6427c478bd9Sstevel@tonic-gate 			mp->b_next = mp->b_prev = NULL;
6437c478bd9Sstevel@tonic-gate 			freemsg(mp);
6447c478bd9Sstevel@tonic-gate 		}
6457c478bd9Sstevel@tonic-gate 	}
6467c478bd9Sstevel@tonic-gate }
6477c478bd9Sstevel@tonic-gate 
6487c478bd9Sstevel@tonic-gate /*
6497c478bd9Sstevel@tonic-gate  * Last reference to the sctp_t is gone. Free all memory associated with it.
6507c478bd9Sstevel@tonic-gate  * Called from SCTP_REFRELE. Called inline in sctp_close()
6517c478bd9Sstevel@tonic-gate  */
6527c478bd9Sstevel@tonic-gate void
6537c478bd9Sstevel@tonic-gate sctp_free(conn_t *connp)
6547c478bd9Sstevel@tonic-gate {
6557c478bd9Sstevel@tonic-gate 	sctp_t *sctp = CONN2SCTP(connp);
6567c478bd9Sstevel@tonic-gate 	int		cnt;
657f4b3ec61Sdh 	sctp_stack_t	*sctps = sctp->sctp_sctps;
658f4b3ec61Sdh 	netstack_t	*ns;
6597c478bd9Sstevel@tonic-gate 
660f4b3ec61Sdh 	ASSERT(sctps != NULL);
6617c478bd9Sstevel@tonic-gate 	/* Unlink it from the global list */
662f4b3ec61Sdh 	SCTP_UNLINK(sctp, sctps);
6637c478bd9Sstevel@tonic-gate 
6647c478bd9Sstevel@tonic-gate 	ASSERT(connp->conn_ref == 0);
6657c478bd9Sstevel@tonic-gate 	ASSERT(connp->conn_ulp == IPPROTO_SCTP);
6667c478bd9Sstevel@tonic-gate 	ASSERT(!MUTEX_HELD(&sctp->sctp_reflock));
6677c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_refcnt == 0);
6687c478bd9Sstevel@tonic-gate 
6697c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_ptpbhn == NULL && sctp->sctp_bind_hash == NULL);
6707c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_conn_hash_next == NULL &&
6717c478bd9Sstevel@tonic-gate 	    sctp->sctp_conn_hash_prev == NULL);
6727c478bd9Sstevel@tonic-gate 
6737c478bd9Sstevel@tonic-gate 
6747c478bd9Sstevel@tonic-gate 	/* Free up all the resources. */
6757c478bd9Sstevel@tonic-gate 
6767c478bd9Sstevel@tonic-gate 	/* blow away sctp stream management */
6777c478bd9Sstevel@tonic-gate 	if (sctp->sctp_ostrcntrs != NULL) {
6787c478bd9Sstevel@tonic-gate 		kmem_free(sctp->sctp_ostrcntrs,
6797c478bd9Sstevel@tonic-gate 		    sizeof (uint16_t) * sctp->sctp_num_ostr);
6807c478bd9Sstevel@tonic-gate 		sctp->sctp_ostrcntrs = NULL;
6817c478bd9Sstevel@tonic-gate 	}
6827c478bd9Sstevel@tonic-gate 	sctp_instream_cleanup(sctp, B_TRUE);
6837c478bd9Sstevel@tonic-gate 
6847c478bd9Sstevel@tonic-gate 	/* Remove all data transfer resources. */
6857c478bd9Sstevel@tonic-gate 	sctp->sctp_istr_nmsgs = 0;
6867c478bd9Sstevel@tonic-gate 	sctp->sctp_rxqueued = 0;
6877c478bd9Sstevel@tonic-gate 	sctp_free_xmit_data(sctp);
6887c478bd9Sstevel@tonic-gate 	sctp->sctp_unacked = 0;
6897c478bd9Sstevel@tonic-gate 	sctp->sctp_unsent = 0;
6901d8c4025Svi 	if (sctp->sctp_cxmit_list != NULL)
6911d8c4025Svi 		sctp_asconf_free_cxmit(sctp, NULL);
6921d8c4025Svi 
6937c478bd9Sstevel@tonic-gate 	sctp->sctp_lastdata = NULL;
6947c478bd9Sstevel@tonic-gate 
6957c478bd9Sstevel@tonic-gate 	/* Clear out default xmit settings */
6967c478bd9Sstevel@tonic-gate 	sctp->sctp_def_stream = 0;
6977c478bd9Sstevel@tonic-gate 	sctp->sctp_def_flags = 0;
6987c478bd9Sstevel@tonic-gate 	sctp->sctp_def_ppid = 0;
6997c478bd9Sstevel@tonic-gate 	sctp->sctp_def_context = 0;
7007c478bd9Sstevel@tonic-gate 	sctp->sctp_def_timetolive = 0;
7017c478bd9Sstevel@tonic-gate 
7027c478bd9Sstevel@tonic-gate 	if (sctp->sctp_sack_info != NULL) {
7037c478bd9Sstevel@tonic-gate 		sctp_free_set(sctp->sctp_sack_info);
7047c478bd9Sstevel@tonic-gate 		sctp->sctp_sack_info = NULL;
7057c478bd9Sstevel@tonic-gate 	}
7067c478bd9Sstevel@tonic-gate 	sctp->sctp_sack_gaps = 0;
7077c478bd9Sstevel@tonic-gate 
7087c478bd9Sstevel@tonic-gate 	if (sctp->sctp_cookie_mp != NULL) {
7097c478bd9Sstevel@tonic-gate 		freemsg(sctp->sctp_cookie_mp);
7107c478bd9Sstevel@tonic-gate 		sctp->sctp_cookie_mp = NULL;
7117c478bd9Sstevel@tonic-gate 	}
7127c478bd9Sstevel@tonic-gate 
7137c478bd9Sstevel@tonic-gate 	/* Remove all the address resources. */
7147c478bd9Sstevel@tonic-gate 	sctp_zap_addrs(sctp);
7157c478bd9Sstevel@tonic-gate 	for (cnt = 0; cnt < SCTP_IPIF_HASH; cnt++) {
7167c478bd9Sstevel@tonic-gate 		ASSERT(sctp->sctp_saddrs[cnt].ipif_count == 0);
7177c478bd9Sstevel@tonic-gate 		list_destroy(&sctp->sctp_saddrs[cnt].sctp_ipif_list);
7187c478bd9Sstevel@tonic-gate 	}
7197c478bd9Sstevel@tonic-gate 
72045916cd2Sjpk 	ip6_pkt_free(&sctp->sctp_sticky_ipp);
7217c478bd9Sstevel@tonic-gate 
7227c478bd9Sstevel@tonic-gate 	if (sctp->sctp_hopopts != NULL) {
7237c478bd9Sstevel@tonic-gate 		mi_free(sctp->sctp_hopopts);
7247c478bd9Sstevel@tonic-gate 		sctp->sctp_hopopts = NULL;
7257c478bd9Sstevel@tonic-gate 		sctp->sctp_hopoptslen = 0;
7267c478bd9Sstevel@tonic-gate 	}
7277c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_hopoptslen == 0);
7287c478bd9Sstevel@tonic-gate 	if (sctp->sctp_dstopts != NULL) {
7297c478bd9Sstevel@tonic-gate 		mi_free(sctp->sctp_dstopts);
7307c478bd9Sstevel@tonic-gate 		sctp->sctp_dstopts = NULL;
7317c478bd9Sstevel@tonic-gate 		sctp->sctp_dstoptslen = 0;
7327c478bd9Sstevel@tonic-gate 	}
7337c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_dstoptslen == 0);
7347c478bd9Sstevel@tonic-gate 	if (sctp->sctp_rtdstopts != NULL) {
7357c478bd9Sstevel@tonic-gate 		mi_free(sctp->sctp_rtdstopts);
7367c478bd9Sstevel@tonic-gate 		sctp->sctp_rtdstopts = NULL;
7377c478bd9Sstevel@tonic-gate 		sctp->sctp_rtdstoptslen = 0;
7387c478bd9Sstevel@tonic-gate 	}
7397c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_rtdstoptslen == 0);
7407c478bd9Sstevel@tonic-gate 	if (sctp->sctp_rthdr != NULL) {
7417c478bd9Sstevel@tonic-gate 		mi_free(sctp->sctp_rthdr);
7427c478bd9Sstevel@tonic-gate 		sctp->sctp_rthdr = NULL;
7437c478bd9Sstevel@tonic-gate 		sctp->sctp_rthdrlen = 0;
7447c478bd9Sstevel@tonic-gate 	}
7457c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_rthdrlen == 0);
7467c478bd9Sstevel@tonic-gate 	sctp_headers_free(sctp);
7477c478bd9Sstevel@tonic-gate 
7487c478bd9Sstevel@tonic-gate 	sctp->sctp_shutdown_faddr = NULL;
7497c478bd9Sstevel@tonic-gate 
7507c478bd9Sstevel@tonic-gate 	/* Clear all the bitfields. */
7517c478bd9Sstevel@tonic-gate 	bzero(&sctp->sctp_bits, sizeof (sctp->sctp_bits));
7527c478bd9Sstevel@tonic-gate 
7537c478bd9Sstevel@tonic-gate 	/* It is time to update the global statistics. */
754f4b3ec61Sdh 	UPDATE_MIB(&sctps->sctps_mib, sctpOutSCTPPkts, sctp->sctp_opkts);
755f4b3ec61Sdh 	UPDATE_MIB(&sctps->sctps_mib, sctpOutCtrlChunks, sctp->sctp_obchunks);
756f4b3ec61Sdh 	UPDATE_MIB(&sctps->sctps_mib, sctpOutOrderChunks, sctp->sctp_odchunks);
757f4b3ec61Sdh 	UPDATE_MIB(&sctps->sctps_mib,
758f4b3ec61Sdh 	    sctpOutUnorderChunks, sctp->sctp_oudchunks);
759f4b3ec61Sdh 	UPDATE_MIB(&sctps->sctps_mib, sctpRetransChunks, sctp->sctp_rxtchunks);
760f4b3ec61Sdh 	UPDATE_MIB(&sctps->sctps_mib, sctpInSCTPPkts, sctp->sctp_ipkts);
761f4b3ec61Sdh 	UPDATE_MIB(&sctps->sctps_mib, sctpInCtrlChunks, sctp->sctp_ibchunks);
762f4b3ec61Sdh 	UPDATE_MIB(&sctps->sctps_mib, sctpInOrderChunks, sctp->sctp_idchunks);
763f4b3ec61Sdh 	UPDATE_MIB(&sctps->sctps_mib,
764f4b3ec61Sdh 	    sctpInUnorderChunks, sctp->sctp_iudchunks);
765f4b3ec61Sdh 	UPDATE_MIB(&sctps->sctps_mib, sctpFragUsrMsgs, sctp->sctp_fragdmsgs);
766f4b3ec61Sdh 	UPDATE_MIB(&sctps->sctps_mib, sctpReasmUsrMsgs, sctp->sctp_reassmsgs);
7677c478bd9Sstevel@tonic-gate 	sctp->sctp_opkts = 0;
7687c478bd9Sstevel@tonic-gate 	sctp->sctp_obchunks = 0;
7697c478bd9Sstevel@tonic-gate 	sctp->sctp_odchunks = 0;
7707c478bd9Sstevel@tonic-gate 	sctp->sctp_oudchunks = 0;
7717c478bd9Sstevel@tonic-gate 	sctp->sctp_rxtchunks = 0;
7727c478bd9Sstevel@tonic-gate 	sctp->sctp_ipkts = 0;
7737c478bd9Sstevel@tonic-gate 	sctp->sctp_ibchunks = 0;
7747c478bd9Sstevel@tonic-gate 	sctp->sctp_idchunks = 0;
7757c478bd9Sstevel@tonic-gate 	sctp->sctp_iudchunks = 0;
7767c478bd9Sstevel@tonic-gate 	sctp->sctp_fragdmsgs = 0;
7777c478bd9Sstevel@tonic-gate 	sctp->sctp_reassmsgs = 0;
7787c478bd9Sstevel@tonic-gate 
7797c478bd9Sstevel@tonic-gate 	sctp->sctp_autoclose = 0;
7807c478bd9Sstevel@tonic-gate 	sctp->sctp_tx_adaption_code = 0;
7817c478bd9Sstevel@tonic-gate 
7827f99e6fcSkp 	sctp->sctp_v6label_len = 0;
7837f99e6fcSkp 	sctp->sctp_v4label_len = 0;
7847f99e6fcSkp 
7857c478bd9Sstevel@tonic-gate 	/* Clean up conn_t stuff */
7867c478bd9Sstevel@tonic-gate 	connp->conn_policy_cached = B_FALSE;
7877c478bd9Sstevel@tonic-gate 	if (connp->conn_latch != NULL) {
788f4b3ec61Sdh 		IPLATCH_REFRELE(connp->conn_latch, connp->conn_netstack);
7897c478bd9Sstevel@tonic-gate 		connp->conn_latch = NULL;
7907c478bd9Sstevel@tonic-gate 	}
7917c478bd9Sstevel@tonic-gate 	if (connp->conn_policy != NULL) {
792f4b3ec61Sdh 		IPPH_REFRELE(connp->conn_policy, connp->conn_netstack);
7937c478bd9Sstevel@tonic-gate 		connp->conn_policy = NULL;
7947c478bd9Sstevel@tonic-gate 	}
7957c478bd9Sstevel@tonic-gate 	if (connp->conn_ipsec_opt_mp != NULL) {
7967c478bd9Sstevel@tonic-gate 		freemsg(connp->conn_ipsec_opt_mp);
7977c478bd9Sstevel@tonic-gate 		connp->conn_ipsec_opt_mp = NULL;
7987c478bd9Sstevel@tonic-gate 	}
7997c478bd9Sstevel@tonic-gate 	if (connp->conn_cred != NULL) {
8007c478bd9Sstevel@tonic-gate 		crfree(connp->conn_cred);
8017c478bd9Sstevel@tonic-gate 		connp->conn_cred = NULL;
8027c478bd9Sstevel@tonic-gate 	}
8037c478bd9Sstevel@tonic-gate 
804f4b3ec61Sdh 	/* Every sctp_t holds one reference on the default queue */
805f4b3ec61Sdh 	sctp->sctp_sctps = NULL;
806f4b3ec61Sdh 	SCTP_G_Q_REFRELE(sctps);
807f4b3ec61Sdh 
808f4b3ec61Sdh 	ns = connp->conn_netstack;
809f4b3ec61Sdh 	connp->conn_netstack = NULL;
810f4b3ec61Sdh 	netstack_rele(ns);
8117c478bd9Sstevel@tonic-gate 	kmem_cache_free(sctp_conn_cache, connp);
8127c478bd9Sstevel@tonic-gate }
8137c478bd9Sstevel@tonic-gate 
8147c478bd9Sstevel@tonic-gate /* Diagnostic routine used to return a string associated with the sctp state. */
8157c478bd9Sstevel@tonic-gate char *
8167c478bd9Sstevel@tonic-gate sctp_display(sctp_t *sctp, char *sup_buf)
8177c478bd9Sstevel@tonic-gate {
8187c478bd9Sstevel@tonic-gate 	char	*buf;
8197c478bd9Sstevel@tonic-gate 	char	buf1[30];
8207c478bd9Sstevel@tonic-gate 	static char	priv_buf[INET6_ADDRSTRLEN * 2 + 80];
8217c478bd9Sstevel@tonic-gate 	char	*cp;
8227c478bd9Sstevel@tonic-gate 
8237c478bd9Sstevel@tonic-gate 	if (sctp == NULL)
8247c478bd9Sstevel@tonic-gate 		return ("NULL_SCTP");
8257c478bd9Sstevel@tonic-gate 
8267c478bd9Sstevel@tonic-gate 	buf = (sup_buf != NULL) ? sup_buf : priv_buf;
8277c478bd9Sstevel@tonic-gate 
8287c478bd9Sstevel@tonic-gate 	switch (sctp->sctp_state) {
8297c478bd9Sstevel@tonic-gate 	case SCTPS_IDLE:
8307c478bd9Sstevel@tonic-gate 		cp = "SCTP_IDLE";
8317c478bd9Sstevel@tonic-gate 		break;
8327c478bd9Sstevel@tonic-gate 	case SCTPS_BOUND:
8337c478bd9Sstevel@tonic-gate 		cp = "SCTP_BOUND";
8347c478bd9Sstevel@tonic-gate 		break;
8357c478bd9Sstevel@tonic-gate 	case SCTPS_LISTEN:
8367c478bd9Sstevel@tonic-gate 		cp = "SCTP_LISTEN";
8377c478bd9Sstevel@tonic-gate 		break;
8387c478bd9Sstevel@tonic-gate 	case SCTPS_COOKIE_WAIT:
8397c478bd9Sstevel@tonic-gate 		cp = "SCTP_COOKIE_WAIT";
8407c478bd9Sstevel@tonic-gate 		break;
8417c478bd9Sstevel@tonic-gate 	case SCTPS_COOKIE_ECHOED:
8427c478bd9Sstevel@tonic-gate 		cp = "SCTP_COOKIE_ECHOED";
8437c478bd9Sstevel@tonic-gate 		break;
8447c478bd9Sstevel@tonic-gate 	case SCTPS_ESTABLISHED:
8457c478bd9Sstevel@tonic-gate 		cp = "SCTP_ESTABLISHED";
8467c478bd9Sstevel@tonic-gate 		break;
8477c478bd9Sstevel@tonic-gate 	case SCTPS_SHUTDOWN_PENDING:
8487c478bd9Sstevel@tonic-gate 		cp = "SCTP_SHUTDOWN_PENDING";
8497c478bd9Sstevel@tonic-gate 		break;
8507c478bd9Sstevel@tonic-gate 	case SCTPS_SHUTDOWN_SENT:
8517c478bd9Sstevel@tonic-gate 		cp = "SCTPS_SHUTDOWN_SENT";
8527c478bd9Sstevel@tonic-gate 		break;
8537c478bd9Sstevel@tonic-gate 	case SCTPS_SHUTDOWN_RECEIVED:
8547c478bd9Sstevel@tonic-gate 		cp = "SCTPS_SHUTDOWN_RECEIVED";
8557c478bd9Sstevel@tonic-gate 		break;
8567c478bd9Sstevel@tonic-gate 	case SCTPS_SHUTDOWN_ACK_SENT:
8577c478bd9Sstevel@tonic-gate 		cp = "SCTPS_SHUTDOWN_ACK_SENT";
8587c478bd9Sstevel@tonic-gate 		break;
8597c478bd9Sstevel@tonic-gate 	default:
8607c478bd9Sstevel@tonic-gate 		(void) mi_sprintf(buf1, "SCTPUnkState(%d)", sctp->sctp_state);
8617c478bd9Sstevel@tonic-gate 		cp = buf1;
8627c478bd9Sstevel@tonic-gate 		break;
8637c478bd9Sstevel@tonic-gate 	}
8647c478bd9Sstevel@tonic-gate 	(void) mi_sprintf(buf, "[%u, %u] %s",
8657c478bd9Sstevel@tonic-gate 	    ntohs(sctp->sctp_lport), ntohs(sctp->sctp_fport), cp);
8667c478bd9Sstevel@tonic-gate 
8677c478bd9Sstevel@tonic-gate 	return (buf);
8687c478bd9Sstevel@tonic-gate }
8697c478bd9Sstevel@tonic-gate 
8707c478bd9Sstevel@tonic-gate /*
8717c478bd9Sstevel@tonic-gate  * Initialize protocol control block. If a parent exists, inherit
8727c478bd9Sstevel@tonic-gate  * all values set through setsockopt().
8737c478bd9Sstevel@tonic-gate  */
8747c478bd9Sstevel@tonic-gate static int
8757c478bd9Sstevel@tonic-gate sctp_init_values(sctp_t *sctp, sctp_t *psctp, int sleep)
8767c478bd9Sstevel@tonic-gate {
8777c478bd9Sstevel@tonic-gate 	int	err;
8787c478bd9Sstevel@tonic-gate 	int	cnt;
879f4b3ec61Sdh 	sctp_stack_t	*sctps = sctp->sctp_sctps;
8805d0bc3edSsommerfe 	conn_t 	*connp, *pconnp;
8817c478bd9Sstevel@tonic-gate 
8827c478bd9Sstevel@tonic-gate 	ASSERT((sctp->sctp_family == AF_INET &&
8837c478bd9Sstevel@tonic-gate 	    sctp->sctp_ipversion == IPV4_VERSION) ||
8847c478bd9Sstevel@tonic-gate 	    (sctp->sctp_family == AF_INET6 &&
8857c478bd9Sstevel@tonic-gate 	    (sctp->sctp_ipversion == IPV4_VERSION ||
8867c478bd9Sstevel@tonic-gate 	    sctp->sctp_ipversion == IPV6_VERSION)));
8877c478bd9Sstevel@tonic-gate 
8887c478bd9Sstevel@tonic-gate 	sctp->sctp_nsaddrs = 0;
8897c478bd9Sstevel@tonic-gate 	for (cnt = 0; cnt < SCTP_IPIF_HASH; cnt++) {
8907c478bd9Sstevel@tonic-gate 		sctp->sctp_saddrs[cnt].ipif_count = 0;
8917c478bd9Sstevel@tonic-gate 		list_create(&sctp->sctp_saddrs[cnt].sctp_ipif_list,
8927c478bd9Sstevel@tonic-gate 		    sizeof (sctp_saddr_ipif_t), offsetof(sctp_saddr_ipif_t,
8937c478bd9Sstevel@tonic-gate 		    saddr_ipif));
8947c478bd9Sstevel@tonic-gate 	}
8957c478bd9Sstevel@tonic-gate 	sctp->sctp_ports = 0;
8967c478bd9Sstevel@tonic-gate 	sctp->sctp_running = B_FALSE;
8977c478bd9Sstevel@tonic-gate 	sctp->sctp_state = SCTPS_IDLE;
8987c478bd9Sstevel@tonic-gate 
8997c478bd9Sstevel@tonic-gate 	sctp->sctp_refcnt = 1;
9007c478bd9Sstevel@tonic-gate 
9017c478bd9Sstevel@tonic-gate 	sctp->sctp_strikes = 0;
9027c478bd9Sstevel@tonic-gate 
9037c478bd9Sstevel@tonic-gate 	sctp->sctp_last_mtu_probe = lbolt64;
904f4b3ec61Sdh 	sctp->sctp_mtu_probe_intvl = sctps->sctps_mtu_probe_interval;
9057c478bd9Sstevel@tonic-gate 
9067c478bd9Sstevel@tonic-gate 	sctp->sctp_sack_gaps = 0;
9077c478bd9Sstevel@tonic-gate 	sctp->sctp_sack_toggle = 2;
9087c478bd9Sstevel@tonic-gate 
9097c478bd9Sstevel@tonic-gate 	if (psctp != NULL) {
9107c478bd9Sstevel@tonic-gate 		/*
9117c478bd9Sstevel@tonic-gate 		 * Inherit from parent
9127c478bd9Sstevel@tonic-gate 		 */
9137c478bd9Sstevel@tonic-gate 		sctp->sctp_iphc = kmem_zalloc(psctp->sctp_iphc_len,
9147c478bd9Sstevel@tonic-gate 		    KM_NOSLEEP);
9157c478bd9Sstevel@tonic-gate 		if (sctp->sctp_iphc == NULL)
9167c478bd9Sstevel@tonic-gate 			return (ENOMEM);
9177c478bd9Sstevel@tonic-gate 		sctp->sctp_iphc_len = psctp->sctp_iphc_len;
9187c478bd9Sstevel@tonic-gate 		sctp->sctp_hdr_len = psctp->sctp_hdr_len;
9197c478bd9Sstevel@tonic-gate 
9207c478bd9Sstevel@tonic-gate 		sctp->sctp_iphc6 = kmem_zalloc(psctp->sctp_iphc6_len,
9217c478bd9Sstevel@tonic-gate 		    KM_NOSLEEP);
9227c478bd9Sstevel@tonic-gate 		if (sctp->sctp_iphc6 == NULL) {
9237c478bd9Sstevel@tonic-gate 			sctp->sctp_iphc6_len = 0;
9247c478bd9Sstevel@tonic-gate 			return (ENOMEM);
9257c478bd9Sstevel@tonic-gate 		}
9267c478bd9Sstevel@tonic-gate 		sctp->sctp_iphc6_len = psctp->sctp_iphc6_len;
9277c478bd9Sstevel@tonic-gate 		sctp->sctp_hdr6_len = psctp->sctp_hdr6_len;
9287c478bd9Sstevel@tonic-gate 
9297c478bd9Sstevel@tonic-gate 		sctp->sctp_ip_hdr_len = psctp->sctp_ip_hdr_len;
9307c478bd9Sstevel@tonic-gate 		sctp->sctp_ip_hdr6_len = psctp->sctp_ip_hdr6_len;
9317c478bd9Sstevel@tonic-gate 
9327c478bd9Sstevel@tonic-gate 		/*
9337c478bd9Sstevel@tonic-gate 		 * Copy the IP+SCTP header templates from listener
9347c478bd9Sstevel@tonic-gate 		 */
9357c478bd9Sstevel@tonic-gate 		bcopy(psctp->sctp_iphc, sctp->sctp_iphc,
9367c478bd9Sstevel@tonic-gate 		    psctp->sctp_hdr_len);
9377c478bd9Sstevel@tonic-gate 		sctp->sctp_ipha = (ipha_t *)sctp->sctp_iphc;
9387c478bd9Sstevel@tonic-gate 		sctp->sctp_sctph = (sctp_hdr_t *)(sctp->sctp_iphc +
9397c478bd9Sstevel@tonic-gate 		    sctp->sctp_ip_hdr_len);
9407c478bd9Sstevel@tonic-gate 
9417c478bd9Sstevel@tonic-gate 		bcopy(psctp->sctp_iphc6, sctp->sctp_iphc6,
9427c478bd9Sstevel@tonic-gate 		    psctp->sctp_hdr6_len);
9437c478bd9Sstevel@tonic-gate 		if (((ip6i_t *)(sctp->sctp_iphc6))->ip6i_nxt == IPPROTO_RAW) {
9447c478bd9Sstevel@tonic-gate 			sctp->sctp_ip6h = (ip6_t *)(sctp->sctp_iphc6 +
9457c478bd9Sstevel@tonic-gate 			    sizeof (ip6i_t));
9467c478bd9Sstevel@tonic-gate 		} else {
9477c478bd9Sstevel@tonic-gate 			sctp->sctp_ip6h = (ip6_t *)sctp->sctp_iphc6;
9487c478bd9Sstevel@tonic-gate 		}
9497c478bd9Sstevel@tonic-gate 		sctp->sctp_sctph6 = (sctp_hdr_t *)(sctp->sctp_iphc6 +
9507c478bd9Sstevel@tonic-gate 		    sctp->sctp_ip_hdr6_len);
9517c478bd9Sstevel@tonic-gate 
9527c478bd9Sstevel@tonic-gate 		sctp->sctp_cookie_lifetime = psctp->sctp_cookie_lifetime;
9537c478bd9Sstevel@tonic-gate 		sctp->sctp_xmit_lowater = psctp->sctp_xmit_lowater;
9547c478bd9Sstevel@tonic-gate 		sctp->sctp_xmit_hiwater = psctp->sctp_xmit_hiwater;
9557c478bd9Sstevel@tonic-gate 		sctp->sctp_cwnd_max = psctp->sctp_cwnd_max;
9567c478bd9Sstevel@tonic-gate 		sctp->sctp_rwnd = psctp->sctp_rwnd;
9571d8c4025Svi 		sctp->sctp_irwnd = psctp->sctp_rwnd;
9587d546a59Svi 		sctp->sctp_pd_point = psctp->sctp_pd_point;
9597c478bd9Sstevel@tonic-gate 		sctp->sctp_rto_max = psctp->sctp_rto_max;
9607c478bd9Sstevel@tonic-gate 		sctp->sctp_init_rto_max = psctp->sctp_init_rto_max;
9617c478bd9Sstevel@tonic-gate 		sctp->sctp_rto_min = psctp->sctp_rto_min;
9627c478bd9Sstevel@tonic-gate 		sctp->sctp_rto_initial = psctp->sctp_rto_initial;
9637c478bd9Sstevel@tonic-gate 		sctp->sctp_pa_max_rxt = psctp->sctp_pa_max_rxt;
9647c478bd9Sstevel@tonic-gate 		sctp->sctp_pp_max_rxt = psctp->sctp_pp_max_rxt;
9657c478bd9Sstevel@tonic-gate 		sctp->sctp_max_init_rxt = psctp->sctp_max_init_rxt;
9667c478bd9Sstevel@tonic-gate 
9677c478bd9Sstevel@tonic-gate 		sctp->sctp_def_stream = psctp->sctp_def_stream;
9687c478bd9Sstevel@tonic-gate 		sctp->sctp_def_flags = psctp->sctp_def_flags;
9697c478bd9Sstevel@tonic-gate 		sctp->sctp_def_ppid = psctp->sctp_def_ppid;
9707c478bd9Sstevel@tonic-gate 		sctp->sctp_def_context = psctp->sctp_def_context;
9717c478bd9Sstevel@tonic-gate 		sctp->sctp_def_timetolive = psctp->sctp_def_timetolive;
9727c478bd9Sstevel@tonic-gate 
9737c478bd9Sstevel@tonic-gate 		sctp->sctp_num_istr = psctp->sctp_num_istr;
9747c478bd9Sstevel@tonic-gate 		sctp->sctp_num_ostr = psctp->sctp_num_ostr;
9757c478bd9Sstevel@tonic-gate 
9767c478bd9Sstevel@tonic-gate 		sctp->sctp_hb_interval = psctp->sctp_hb_interval;
9777c478bd9Sstevel@tonic-gate 		sctp->sctp_autoclose = psctp->sctp_autoclose;
9787c478bd9Sstevel@tonic-gate 		sctp->sctp_tx_adaption_code = psctp->sctp_tx_adaption_code;
9797c478bd9Sstevel@tonic-gate 
9807c478bd9Sstevel@tonic-gate 		/* xxx should be a better way to copy these flags xxx */
9817c478bd9Sstevel@tonic-gate 		sctp->sctp_debug = psctp->sctp_debug;
9827c478bd9Sstevel@tonic-gate 		sctp->sctp_bound_to_all = psctp->sctp_bound_to_all;
9837c478bd9Sstevel@tonic-gate 		sctp->sctp_cansleep = psctp->sctp_cansleep;
9847c478bd9Sstevel@tonic-gate 		sctp->sctp_send_adaption = psctp->sctp_send_adaption;
9857c478bd9Sstevel@tonic-gate 		sctp->sctp_ndelay = psctp->sctp_ndelay;
9867c478bd9Sstevel@tonic-gate 		sctp->sctp_events = psctp->sctp_events;
9877c478bd9Sstevel@tonic-gate 		sctp->sctp_ipv6_recvancillary = psctp->sctp_ipv6_recvancillary;
9885d0bc3edSsommerfe 
9895d0bc3edSsommerfe 		/* Copy IP-layer options */
9905d0bc3edSsommerfe 		connp = sctp->sctp_connp;
9915d0bc3edSsommerfe 		pconnp = psctp->sctp_connp;
9925d0bc3edSsommerfe 
9935d0bc3edSsommerfe 		connp->conn_broadcast = pconnp->conn_broadcast;
9945d0bc3edSsommerfe 		connp->conn_loopback = pconnp->conn_loopback;
9955d0bc3edSsommerfe 		connp->conn_dontroute = pconnp->conn_dontroute;
9965d0bc3edSsommerfe 		connp->conn_reuseaddr = pconnp->conn_reuseaddr;
9975d0bc3edSsommerfe 
9987c478bd9Sstevel@tonic-gate 	} else {
9997c478bd9Sstevel@tonic-gate 		/*
10007c478bd9Sstevel@tonic-gate 		 * Initialize the header template
10017c478bd9Sstevel@tonic-gate 		 */
10027c478bd9Sstevel@tonic-gate 		if ((err = sctp_header_init_ipv4(sctp, sleep)) != 0) {
10037c478bd9Sstevel@tonic-gate 			return (err);
10047c478bd9Sstevel@tonic-gate 		}
10057c478bd9Sstevel@tonic-gate 		if ((err = sctp_header_init_ipv6(sctp, sleep)) != 0) {
10067c478bd9Sstevel@tonic-gate 			return (err);
10077c478bd9Sstevel@tonic-gate 		}
10087c478bd9Sstevel@tonic-gate 
10097c478bd9Sstevel@tonic-gate 		/*
10107c478bd9Sstevel@tonic-gate 		 * Set to system defaults
10117c478bd9Sstevel@tonic-gate 		 */
1012f4b3ec61Sdh 		sctp->sctp_cookie_lifetime =
1013f4b3ec61Sdh 		    MSEC_TO_TICK(sctps->sctps_cookie_life);
1014f4b3ec61Sdh 		sctp->sctp_xmit_lowater = sctps->sctps_xmit_lowat;
1015f4b3ec61Sdh 		sctp->sctp_xmit_hiwater = sctps->sctps_xmit_hiwat;
1016f4b3ec61Sdh 		sctp->sctp_cwnd_max = sctps->sctps_cwnd_max_;
1017f4b3ec61Sdh 		sctp->sctp_rwnd = sctps->sctps_recv_hiwat;
10181d8c4025Svi 		sctp->sctp_irwnd = sctp->sctp_rwnd;
10197d546a59Svi 		sctp->sctp_pd_point = sctp->sctp_rwnd;
1020f4b3ec61Sdh 		sctp->sctp_rto_max = MSEC_TO_TICK(sctps->sctps_rto_maxg);
10217c478bd9Sstevel@tonic-gate 		sctp->sctp_init_rto_max = sctp->sctp_rto_max;
1022f4b3ec61Sdh 		sctp->sctp_rto_min = MSEC_TO_TICK(sctps->sctps_rto_ming);
1023f4b3ec61Sdh 		sctp->sctp_rto_initial = MSEC_TO_TICK(
1024f4b3ec61Sdh 		    sctps->sctps_rto_initialg);
1025f4b3ec61Sdh 		sctp->sctp_pa_max_rxt = sctps->sctps_pa_max_retr;
1026f4b3ec61Sdh 		sctp->sctp_pp_max_rxt = sctps->sctps_pp_max_retr;
1027f4b3ec61Sdh 		sctp->sctp_max_init_rxt = sctps->sctps_max_init_retr;
1028f4b3ec61Sdh 
1029f4b3ec61Sdh 		sctp->sctp_num_istr = sctps->sctps_max_in_streams;
1030f4b3ec61Sdh 		sctp->sctp_num_ostr = sctps->sctps_initial_out_streams;
1031f4b3ec61Sdh 
1032f4b3ec61Sdh 		sctp->sctp_hb_interval =
1033f4b3ec61Sdh 		    MSEC_TO_TICK(sctps->sctps_heartbeat_interval);
10347c478bd9Sstevel@tonic-gate 	}
10357c478bd9Sstevel@tonic-gate 	sctp->sctp_understands_asconf = B_TRUE;
10367c478bd9Sstevel@tonic-gate 	sctp->sctp_understands_addip = B_TRUE;
10377c478bd9Sstevel@tonic-gate 	sctp->sctp_prsctp_aware = B_FALSE;
10387c478bd9Sstevel@tonic-gate 
10397c478bd9Sstevel@tonic-gate 	sctp->sctp_connp->conn_ref = 1;
10407c478bd9Sstevel@tonic-gate 	sctp->sctp_connp->conn_fully_bound = B_FALSE;
10417c478bd9Sstevel@tonic-gate 
10427c478bd9Sstevel@tonic-gate 	sctp->sctp_prsctpdrop = 0;
10437c478bd9Sstevel@tonic-gate 	sctp->sctp_msgcount = 0;
10447c478bd9Sstevel@tonic-gate 
10457c478bd9Sstevel@tonic-gate 	return (0);
10467c478bd9Sstevel@tonic-gate }
10477c478bd9Sstevel@tonic-gate 
10487c478bd9Sstevel@tonic-gate /*
10497c478bd9Sstevel@tonic-gate  * Extracts the init tag from an INIT chunk and checks if it matches
10507c478bd9Sstevel@tonic-gate  * the sctp's verification tag. Returns 0 if it doesn't match, 1 if
10517c478bd9Sstevel@tonic-gate  * it does.
10527c478bd9Sstevel@tonic-gate  */
10537c478bd9Sstevel@tonic-gate static boolean_t
10547c478bd9Sstevel@tonic-gate sctp_icmp_verf(sctp_t *sctp, sctp_hdr_t *sh, mblk_t *mp)
10557c478bd9Sstevel@tonic-gate {
10567c478bd9Sstevel@tonic-gate 	sctp_chunk_hdr_t *sch;
10577c478bd9Sstevel@tonic-gate 	uint32_t verf, *vp;
10587c478bd9Sstevel@tonic-gate 
10597c478bd9Sstevel@tonic-gate 	sch = (sctp_chunk_hdr_t *)(sh + 1);
10607c478bd9Sstevel@tonic-gate 	vp = (uint32_t *)(sch + 1);
10617c478bd9Sstevel@tonic-gate 
10627c478bd9Sstevel@tonic-gate 	/* Need at least the data chunk hdr and the first 4 bytes of INIT */
10637c478bd9Sstevel@tonic-gate 	if ((unsigned char *)(vp + 1) > mp->b_wptr) {
10647c478bd9Sstevel@tonic-gate 		return (B_FALSE);
10657c478bd9Sstevel@tonic-gate 	}
10667c478bd9Sstevel@tonic-gate 
10677c478bd9Sstevel@tonic-gate 	bcopy(vp, &verf, sizeof (verf));
10687c478bd9Sstevel@tonic-gate 
10697c478bd9Sstevel@tonic-gate 	if (verf == sctp->sctp_lvtag) {
10707c478bd9Sstevel@tonic-gate 		return (B_TRUE);
10717c478bd9Sstevel@tonic-gate 	}
10727c478bd9Sstevel@tonic-gate 	return (B_FALSE);
10737c478bd9Sstevel@tonic-gate }
10747c478bd9Sstevel@tonic-gate 
10757c478bd9Sstevel@tonic-gate /*
10767c478bd9Sstevel@tonic-gate  * sctp_icmp_error is called by sctp_input() to process ICMP error messages
10777c478bd9Sstevel@tonic-gate  * passed up by IP.  The queue is the default queue.  We need to find a sctp_t
10787c478bd9Sstevel@tonic-gate  * that corresponds to the returned datagram.  Passes the message back in on
10797c478bd9Sstevel@tonic-gate  * the correct queue once it has located the connection.
10807c478bd9Sstevel@tonic-gate  * Assumes that IP has pulled up everything up to and including
10817c478bd9Sstevel@tonic-gate  * the ICMP header.
10827c478bd9Sstevel@tonic-gate  */
10837c478bd9Sstevel@tonic-gate void
10847c478bd9Sstevel@tonic-gate sctp_icmp_error(sctp_t *sctp, mblk_t *mp)
10857c478bd9Sstevel@tonic-gate {
10867c478bd9Sstevel@tonic-gate 	icmph_t *icmph;
10877c478bd9Sstevel@tonic-gate 	ipha_t	*ipha;
10887c478bd9Sstevel@tonic-gate 	int	iph_hdr_length;
10897c478bd9Sstevel@tonic-gate 	sctp_hdr_t *sctph;
10907c478bd9Sstevel@tonic-gate 	mblk_t *first_mp;
10917c478bd9Sstevel@tonic-gate 	uint32_t new_mtu;
10927c478bd9Sstevel@tonic-gate 	in6_addr_t dst;
10937c478bd9Sstevel@tonic-gate 	sctp_faddr_t *fp;
1094f4b3ec61Sdh 	sctp_stack_t	*sctps = sctp->sctp_sctps;
10957c478bd9Sstevel@tonic-gate 
109645916cd2Sjpk 	dprint(1, ("sctp_icmp_error: sctp=%p, mp=%p\n", (void *)sctp,
109745916cd2Sjpk 	    (void *)mp));
10987c478bd9Sstevel@tonic-gate 
10997c478bd9Sstevel@tonic-gate 	first_mp = mp;
11007c478bd9Sstevel@tonic-gate 
11017c478bd9Sstevel@tonic-gate 	ipha = (ipha_t *)mp->b_rptr;
11027c478bd9Sstevel@tonic-gate 	if (IPH_HDR_VERSION(ipha) != IPV4_VERSION) {
11037c478bd9Sstevel@tonic-gate 		ASSERT(IPH_HDR_VERSION(ipha) == IPV6_VERSION);
11047c478bd9Sstevel@tonic-gate 		sctp_icmp_error_ipv6(sctp, first_mp);
11057c478bd9Sstevel@tonic-gate 		return;
11067c478bd9Sstevel@tonic-gate 	}
11077c478bd9Sstevel@tonic-gate 
11087c478bd9Sstevel@tonic-gate 	iph_hdr_length = IPH_HDR_LENGTH(ipha);
11097c478bd9Sstevel@tonic-gate 	icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length];
11107c478bd9Sstevel@tonic-gate 	ipha = (ipha_t *)&icmph[1];
11117c478bd9Sstevel@tonic-gate 	iph_hdr_length = IPH_HDR_LENGTH(ipha);
11127c478bd9Sstevel@tonic-gate 	sctph = (sctp_hdr_t *)((char *)ipha + iph_hdr_length);
11137c478bd9Sstevel@tonic-gate 	if ((uchar_t *)(sctph + 1) >= mp->b_wptr) {
11147c478bd9Sstevel@tonic-gate 		/* not enough data for SCTP header */
11157c478bd9Sstevel@tonic-gate 		freemsg(first_mp);
11167c478bd9Sstevel@tonic-gate 		return;
11177c478bd9Sstevel@tonic-gate 	}
11187c478bd9Sstevel@tonic-gate 
11197c478bd9Sstevel@tonic-gate 	switch (icmph->icmph_type) {
11207c478bd9Sstevel@tonic-gate 	case ICMP_DEST_UNREACHABLE:
11217c478bd9Sstevel@tonic-gate 		switch (icmph->icmph_code) {
11227c478bd9Sstevel@tonic-gate 		case ICMP_FRAGMENTATION_NEEDED:
11237c478bd9Sstevel@tonic-gate 			/*
11247c478bd9Sstevel@tonic-gate 			 * Reduce the MSS based on the new MTU.  This will
11257c478bd9Sstevel@tonic-gate 			 * eliminate any fragmentation locally.
11267c478bd9Sstevel@tonic-gate 			 * N.B.  There may well be some funny side-effects on
11277c478bd9Sstevel@tonic-gate 			 * the local send policy and the remote receive policy.
11287c478bd9Sstevel@tonic-gate 			 * Pending further research, we provide
11297c478bd9Sstevel@tonic-gate 			 * sctp_ignore_path_mtu just in case this proves
11307c478bd9Sstevel@tonic-gate 			 * disastrous somewhere.
11317c478bd9Sstevel@tonic-gate 			 *
11327c478bd9Sstevel@tonic-gate 			 * After updating the MSS, retransmit part of the
11337c478bd9Sstevel@tonic-gate 			 * dropped segment using the new mss by calling
11347c478bd9Sstevel@tonic-gate 			 * sctp_wput_slow().  Need to adjust all those
11357c478bd9Sstevel@tonic-gate 			 * params to make sure sctp_wput_slow() work properly.
11367c478bd9Sstevel@tonic-gate 			 */
1137f4b3ec61Sdh 			if (sctps->sctps_ignore_path_mtu)
11387c478bd9Sstevel@tonic-gate 				break;
11397c478bd9Sstevel@tonic-gate 
11407c478bd9Sstevel@tonic-gate 			/* find the offending faddr */
11417c478bd9Sstevel@tonic-gate 			IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, &dst);
11427c478bd9Sstevel@tonic-gate 			fp = sctp_lookup_faddr(sctp, &dst);
11437c478bd9Sstevel@tonic-gate 			if (fp == NULL) {
11447c478bd9Sstevel@tonic-gate 				break;
11457c478bd9Sstevel@tonic-gate 			}
11467c478bd9Sstevel@tonic-gate 
11477c478bd9Sstevel@tonic-gate 			new_mtu = ntohs(icmph->icmph_du_mtu);
11487c478bd9Sstevel@tonic-gate 
11497c478bd9Sstevel@tonic-gate 			if (new_mtu - sctp->sctp_hdr_len >= fp->sfa_pmss)
11507c478bd9Sstevel@tonic-gate 				break;
11517c478bd9Sstevel@tonic-gate 
11527c478bd9Sstevel@tonic-gate 			/*
11537c478bd9Sstevel@tonic-gate 			 * Make sure that sfa_pmss is a multiple of
11547c478bd9Sstevel@tonic-gate 			 * SCTP_ALIGN.
11557c478bd9Sstevel@tonic-gate 			 */
11567c478bd9Sstevel@tonic-gate 			fp->sfa_pmss = (new_mtu - sctp->sctp_hdr_len) &
11577c478bd9Sstevel@tonic-gate 				~(SCTP_ALIGN - 1);
11587c478bd9Sstevel@tonic-gate 			fp->pmtu_discovered = 1;
11597c478bd9Sstevel@tonic-gate 
11607c478bd9Sstevel@tonic-gate 			break;
11617c478bd9Sstevel@tonic-gate 		case ICMP_PORT_UNREACHABLE:
11627c478bd9Sstevel@tonic-gate 		case ICMP_PROTOCOL_UNREACHABLE:
11637c478bd9Sstevel@tonic-gate 			switch (sctp->sctp_state) {
11647c478bd9Sstevel@tonic-gate 			case SCTPS_COOKIE_WAIT:
11657c478bd9Sstevel@tonic-gate 			case SCTPS_COOKIE_ECHOED:
11667c478bd9Sstevel@tonic-gate 				/* make sure the verification tag matches */
11677c478bd9Sstevel@tonic-gate 				if (!sctp_icmp_verf(sctp, sctph, mp)) {
11687c478bd9Sstevel@tonic-gate 					break;
11697c478bd9Sstevel@tonic-gate 				}
1170f4b3ec61Sdh 				BUMP_MIB(&sctps->sctps_mib, sctpAborted);
1171c9da23f8Skcpoon 				sctp_assoc_event(sctp, SCTP_CANT_STR_ASSOC, 0,
1172c9da23f8Skcpoon 				    NULL);
11737c478bd9Sstevel@tonic-gate 				sctp_clean_death(sctp, ECONNREFUSED);
11747c478bd9Sstevel@tonic-gate 				break;
11757c478bd9Sstevel@tonic-gate 			}
11767c478bd9Sstevel@tonic-gate 			break;
11777c478bd9Sstevel@tonic-gate 		case ICMP_HOST_UNREACHABLE:
11787c478bd9Sstevel@tonic-gate 		case ICMP_NET_UNREACHABLE:
11797c478bd9Sstevel@tonic-gate 			/* Record the error in case we finally time out. */
11807c478bd9Sstevel@tonic-gate 			sctp->sctp_client_errno = (icmph->icmph_code ==
11817c478bd9Sstevel@tonic-gate 			    ICMP_HOST_UNREACHABLE) ? EHOSTUNREACH : ENETUNREACH;
11827c478bd9Sstevel@tonic-gate 			break;
11837c478bd9Sstevel@tonic-gate 		default:
11847c478bd9Sstevel@tonic-gate 			break;
11857c478bd9Sstevel@tonic-gate 		}
11867c478bd9Sstevel@tonic-gate 		break;
11877c478bd9Sstevel@tonic-gate 	case ICMP_SOURCE_QUENCH: {
11887c478bd9Sstevel@tonic-gate 		/* Reduce the sending rate as if we got a retransmit timeout */
11897c478bd9Sstevel@tonic-gate 		break;
11907c478bd9Sstevel@tonic-gate 	}
11917c478bd9Sstevel@tonic-gate 	}
11927c478bd9Sstevel@tonic-gate 	freemsg(first_mp);
11937c478bd9Sstevel@tonic-gate }
11947c478bd9Sstevel@tonic-gate 
11957c478bd9Sstevel@tonic-gate /*
11967c478bd9Sstevel@tonic-gate  * sctp_icmp_error_ipv6() is called by sctp_icmp_error() to process ICMPv6
11977c478bd9Sstevel@tonic-gate  * error messages passed up by IP.
11987c478bd9Sstevel@tonic-gate  * Assumes that IP has pulled up all the extension headers as well
11997c478bd9Sstevel@tonic-gate  * as the ICMPv6 header.
12007c478bd9Sstevel@tonic-gate  */
12017c478bd9Sstevel@tonic-gate static void
12027c478bd9Sstevel@tonic-gate sctp_icmp_error_ipv6(sctp_t *sctp, mblk_t *mp)
12037c478bd9Sstevel@tonic-gate {
12047c478bd9Sstevel@tonic-gate 	icmp6_t *icmp6;
12057c478bd9Sstevel@tonic-gate 	ip6_t	*ip6h;
12067c478bd9Sstevel@tonic-gate 	uint16_t	iph_hdr_length;
12077c478bd9Sstevel@tonic-gate 	sctp_hdr_t *sctpha;
12087c478bd9Sstevel@tonic-gate 	uint8_t	*nexthdrp;
12097c478bd9Sstevel@tonic-gate 	uint32_t new_mtu;
12107c478bd9Sstevel@tonic-gate 	sctp_faddr_t *fp;
1211f4b3ec61Sdh 	sctp_stack_t	*sctps = sctp->sctp_sctps;
12127c478bd9Sstevel@tonic-gate 
12137c478bd9Sstevel@tonic-gate 	ip6h = (ip6_t *)mp->b_rptr;
12147c478bd9Sstevel@tonic-gate 	iph_hdr_length = (ip6h->ip6_nxt != IPPROTO_SCTP) ?
12157c478bd9Sstevel@tonic-gate 	    ip_hdr_length_v6(mp, ip6h) : IPV6_HDR_LEN;
12167c478bd9Sstevel@tonic-gate 
12177c478bd9Sstevel@tonic-gate 	icmp6 = (icmp6_t *)&mp->b_rptr[iph_hdr_length];
12187c478bd9Sstevel@tonic-gate 	ip6h = (ip6_t *)&icmp6[1];
12197c478bd9Sstevel@tonic-gate 	if (!ip_hdr_length_nexthdr_v6(mp, ip6h, &iph_hdr_length, &nexthdrp)) {
12207c478bd9Sstevel@tonic-gate 		freemsg(mp);
12217c478bd9Sstevel@tonic-gate 		return;
12227c478bd9Sstevel@tonic-gate 	}
12237c478bd9Sstevel@tonic-gate 	ASSERT(*nexthdrp == IPPROTO_SCTP);
12247c478bd9Sstevel@tonic-gate 
12257c478bd9Sstevel@tonic-gate 	/* XXX need ifindex to find connection */
12267c478bd9Sstevel@tonic-gate 	sctpha = (sctp_hdr_t *)((char *)ip6h + iph_hdr_length);
12277c478bd9Sstevel@tonic-gate 	if ((uchar_t *)sctpha >= mp->b_wptr) {
12287c478bd9Sstevel@tonic-gate 		/* not enough data for SCTP header */
12297c478bd9Sstevel@tonic-gate 		freemsg(mp);
12307c478bd9Sstevel@tonic-gate 		return;
12317c478bd9Sstevel@tonic-gate 	}
12327c478bd9Sstevel@tonic-gate 	switch (icmp6->icmp6_type) {
12337c478bd9Sstevel@tonic-gate 	case ICMP6_PACKET_TOO_BIG:
12347c478bd9Sstevel@tonic-gate 		/*
12357c478bd9Sstevel@tonic-gate 		 * Reduce the MSS based on the new MTU.  This will
12367c478bd9Sstevel@tonic-gate 		 * eliminate any fragmentation locally.
12377c478bd9Sstevel@tonic-gate 		 * N.B.  There may well be some funny side-effects on
12387c478bd9Sstevel@tonic-gate 		 * the local send policy and the remote receive policy.
12397c478bd9Sstevel@tonic-gate 		 * Pending further research, we provide
12407c478bd9Sstevel@tonic-gate 		 * sctp_ignore_path_mtu just in case this proves
12417c478bd9Sstevel@tonic-gate 		 * disastrous somewhere.
12427c478bd9Sstevel@tonic-gate 		 *
12437c478bd9Sstevel@tonic-gate 		 * After updating the MSS, retransmit part of the
12447c478bd9Sstevel@tonic-gate 		 * dropped segment using the new mss by calling
12457c478bd9Sstevel@tonic-gate 		 * sctp_wput_slow().  Need to adjust all those
12467c478bd9Sstevel@tonic-gate 		 * params to make sure sctp_wput_slow() work properly.
12477c478bd9Sstevel@tonic-gate 		 */
1248f4b3ec61Sdh 		if (sctps->sctps_ignore_path_mtu)
12497c478bd9Sstevel@tonic-gate 			break;
12507c478bd9Sstevel@tonic-gate 
12517c478bd9Sstevel@tonic-gate 		/* find the offending faddr */
12527c478bd9Sstevel@tonic-gate 		fp = sctp_lookup_faddr(sctp, &ip6h->ip6_dst);
12537c478bd9Sstevel@tonic-gate 		if (fp == NULL) {
12547c478bd9Sstevel@tonic-gate 			break;
12557c478bd9Sstevel@tonic-gate 		}
12567c478bd9Sstevel@tonic-gate 
12577c478bd9Sstevel@tonic-gate 		new_mtu = ntohs(icmp6->icmp6_mtu);
12587c478bd9Sstevel@tonic-gate 
12597c478bd9Sstevel@tonic-gate 		if (new_mtu - sctp->sctp_hdr6_len >= fp->sfa_pmss)
12607c478bd9Sstevel@tonic-gate 			break;
12617c478bd9Sstevel@tonic-gate 
12627c478bd9Sstevel@tonic-gate 		/* Make sure that sfa_pmss is a multiple of SCTP_ALIGN. */
12637c478bd9Sstevel@tonic-gate 		fp->sfa_pmss = (new_mtu - sctp->sctp_hdr6_len) &
12647c478bd9Sstevel@tonic-gate 			~(SCTP_ALIGN - 1);
12657c478bd9Sstevel@tonic-gate 		fp->pmtu_discovered = 1;
12667c478bd9Sstevel@tonic-gate 
12677c478bd9Sstevel@tonic-gate 		break;
12687c478bd9Sstevel@tonic-gate 
12697c478bd9Sstevel@tonic-gate 	case ICMP6_DST_UNREACH:
12707c478bd9Sstevel@tonic-gate 		switch (icmp6->icmp6_code) {
12717c478bd9Sstevel@tonic-gate 		case ICMP6_DST_UNREACH_NOPORT:
12727c478bd9Sstevel@tonic-gate 			/* make sure the verification tag matches */
12737c478bd9Sstevel@tonic-gate 			if (!sctp_icmp_verf(sctp, sctpha, mp)) {
12747c478bd9Sstevel@tonic-gate 				break;
12757c478bd9Sstevel@tonic-gate 			}
12767c478bd9Sstevel@tonic-gate 			if (sctp->sctp_state == SCTPS_COOKIE_WAIT ||
12777c478bd9Sstevel@tonic-gate 			    sctp->sctp_state == SCTPS_COOKIE_ECHOED) {
1278f4b3ec61Sdh 				BUMP_MIB(&sctps->sctps_mib, sctpAborted);
1279c9da23f8Skcpoon 				sctp_assoc_event(sctp, SCTP_CANT_STR_ASSOC, 0,
1280c9da23f8Skcpoon 				    NULL);
12817c478bd9Sstevel@tonic-gate 				sctp_clean_death(sctp, ECONNREFUSED);
12827c478bd9Sstevel@tonic-gate 			}
12837c478bd9Sstevel@tonic-gate 			break;
12847c478bd9Sstevel@tonic-gate 
12857c478bd9Sstevel@tonic-gate 		case ICMP6_DST_UNREACH_ADMIN:
12867c478bd9Sstevel@tonic-gate 		case ICMP6_DST_UNREACH_NOROUTE:
12877c478bd9Sstevel@tonic-gate 		case ICMP6_DST_UNREACH_NOTNEIGHBOR:
12887c478bd9Sstevel@tonic-gate 		case ICMP6_DST_UNREACH_ADDR:
12897c478bd9Sstevel@tonic-gate 			/* Record the error in case we finally time out. */
12907c478bd9Sstevel@tonic-gate 			sctp->sctp_client_errno = EHOSTUNREACH;
12917c478bd9Sstevel@tonic-gate 			break;
12927c478bd9Sstevel@tonic-gate 		default:
12937c478bd9Sstevel@tonic-gate 			break;
12947c478bd9Sstevel@tonic-gate 		}
12957c478bd9Sstevel@tonic-gate 		break;
12967c478bd9Sstevel@tonic-gate 
12977c478bd9Sstevel@tonic-gate 	case ICMP6_PARAM_PROB:
12987c478bd9Sstevel@tonic-gate 		/* If this corresponds to an ICMP_PROTOCOL_UNREACHABLE */
12997c478bd9Sstevel@tonic-gate 		if (icmp6->icmp6_code == ICMP6_PARAMPROB_NEXTHEADER &&
13007c478bd9Sstevel@tonic-gate 		    (uchar_t *)ip6h + icmp6->icmp6_pptr ==
13017c478bd9Sstevel@tonic-gate 		    (uchar_t *)nexthdrp) {
13027c478bd9Sstevel@tonic-gate 			/* make sure the verification tag matches */
13037c478bd9Sstevel@tonic-gate 			if (!sctp_icmp_verf(sctp, sctpha, mp)) {
13047c478bd9Sstevel@tonic-gate 				break;
13057c478bd9Sstevel@tonic-gate 			}
13067c478bd9Sstevel@tonic-gate 			if (sctp->sctp_state == SCTPS_COOKIE_WAIT) {
1307f4b3ec61Sdh 				BUMP_MIB(&sctps->sctps_mib, sctpAborted);
1308c9da23f8Skcpoon 				sctp_assoc_event(sctp, SCTP_CANT_STR_ASSOC, 0,
1309c9da23f8Skcpoon 				    NULL);
13107c478bd9Sstevel@tonic-gate 				sctp_clean_death(sctp, ECONNREFUSED);
13117c478bd9Sstevel@tonic-gate 			}
13127c478bd9Sstevel@tonic-gate 			break;
13137c478bd9Sstevel@tonic-gate 		}
13147c478bd9Sstevel@tonic-gate 		break;
13157c478bd9Sstevel@tonic-gate 
13167c478bd9Sstevel@tonic-gate 	case ICMP6_TIME_EXCEEDED:
13177c478bd9Sstevel@tonic-gate 	default:
13187c478bd9Sstevel@tonic-gate 		break;
13197c478bd9Sstevel@tonic-gate 	}
13207c478bd9Sstevel@tonic-gate 	freemsg(mp);
13217c478bd9Sstevel@tonic-gate }
13227c478bd9Sstevel@tonic-gate 
13237c478bd9Sstevel@tonic-gate /*
13247c478bd9Sstevel@tonic-gate  * Called by sockfs to create a new sctp instance.
13257c478bd9Sstevel@tonic-gate  *
13267c478bd9Sstevel@tonic-gate  * If parent pointer is passed in, inherit settings from it.
13277c478bd9Sstevel@tonic-gate  */
13287c478bd9Sstevel@tonic-gate sctp_t *
13297c478bd9Sstevel@tonic-gate sctp_create(void *sctp_ulpd, sctp_t *parent, int family, int flags,
13307c478bd9Sstevel@tonic-gate     const sctp_upcalls_t *sctp_upcalls, sctp_sockbuf_limits_t *sbl,
13317c478bd9Sstevel@tonic-gate     cred_t *credp)
13327c478bd9Sstevel@tonic-gate {
13337c478bd9Sstevel@tonic-gate 	sctp_t		*sctp, *psctp;
13347c478bd9Sstevel@tonic-gate 	conn_t		*sctp_connp;
13357c478bd9Sstevel@tonic-gate 	mblk_t		*ack_mp, *hb_mp;
13367c478bd9Sstevel@tonic-gate 	int		sleep = flags & SCTP_CAN_BLOCK ? KM_SLEEP : KM_NOSLEEP;
1337f4b3ec61Sdh 	zoneid_t	zoneid;
1338f4b3ec61Sdh 	sctp_stack_t	*sctps;
13397c478bd9Sstevel@tonic-gate 
13407c478bd9Sstevel@tonic-gate 	/* User must supply a credential. */
13417c478bd9Sstevel@tonic-gate 	if (credp == NULL)
13427c478bd9Sstevel@tonic-gate 		return (NULL);
13437c478bd9Sstevel@tonic-gate 
13447c478bd9Sstevel@tonic-gate 	psctp = (sctp_t *)parent;
1345f4b3ec61Sdh 	if (psctp != NULL) {
1346f4b3ec61Sdh 		sctps = psctp->sctp_sctps;
1347f4b3ec61Sdh 		/* Increase here to have common decrease at end */
1348f4b3ec61Sdh 		netstack_hold(sctps->sctps_netstack);
1349f4b3ec61Sdh 	} else {
1350f4b3ec61Sdh 		netstack_t *ns;
1351f4b3ec61Sdh 
1352f4b3ec61Sdh 		ns = netstack_find_by_cred(credp);
1353f4b3ec61Sdh 		ASSERT(ns != NULL);
1354f4b3ec61Sdh 		sctps = ns->netstack_sctp;
1355f4b3ec61Sdh 		ASSERT(sctps != NULL);
13567c478bd9Sstevel@tonic-gate 
1357f4b3ec61Sdh 		/*
1358f4b3ec61Sdh 		 * For exclusive stacks we set the zoneid to zero
1359f4b3ec61Sdh 		 * to make SCTP operate as if in the global zone.
1360f4b3ec61Sdh 		 */
1361f4b3ec61Sdh 		if (sctps->sctps_netstack->netstack_stackid !=
1362f4b3ec61Sdh 		    GLOBAL_NETSTACKID)
1363f4b3ec61Sdh 			zoneid = GLOBAL_ZONEID;
1364f4b3ec61Sdh 		else
1365f4b3ec61Sdh 			zoneid = crgetzoneid(credp);
1366f4b3ec61Sdh 
1367f4b3ec61Sdh 		/*
1368f4b3ec61Sdh 		 * For stackid zero this is done from strplumb.c, but
1369f4b3ec61Sdh 		 * non-zero stackids are handled here.
1370f4b3ec61Sdh 		 */
1371f4b3ec61Sdh 		if (sctps->sctps_g_q == NULL &&
1372f4b3ec61Sdh 		    sctps->sctps_netstack->netstack_stackid !=
1373f4b3ec61Sdh 		    GLOBAL_NETSTACKID) {
1374f4b3ec61Sdh 			sctp_g_q_setup(sctps);
1375f4b3ec61Sdh 		}
1376f4b3ec61Sdh 	}
1377f4b3ec61Sdh 	if ((sctp_connp = ipcl_conn_create(IPCL_SCTPCONN, sleep,
1378f4b3ec61Sdh 		    sctps->sctps_netstack)) == NULL) {
1379f4b3ec61Sdh 		netstack_rele(sctps->sctps_netstack);
1380f4b3ec61Sdh 		SCTP_KSTAT(sctps, sctp_conn_create);
1381f4b3ec61Sdh 		return (NULL);
1382f4b3ec61Sdh 	}
1383f4b3ec61Sdh 	/*
1384f4b3ec61Sdh 	 * ipcl_conn_create did a netstack_hold. Undo the hold that was
1385f4b3ec61Sdh 	 * done at top of sctp_create.
1386f4b3ec61Sdh 	 */
1387f4b3ec61Sdh 	netstack_rele(sctps->sctps_netstack);
13887c478bd9Sstevel@tonic-gate 	sctp = CONN2SCTP(sctp_connp);
1389f4b3ec61Sdh 	sctp->sctp_sctps = sctps;
13907c478bd9Sstevel@tonic-gate 
1391f4b3ec61Sdh 	sctp_connp->conn_ulp_labeled = is_system_labeled();
13927c478bd9Sstevel@tonic-gate 	if ((ack_mp = sctp_timer_alloc(sctp, sctp_ack_timer)) == NULL ||
13937c478bd9Sstevel@tonic-gate 	    (hb_mp = sctp_timer_alloc(sctp, sctp_heartbeat_timer)) == NULL) {
13947c478bd9Sstevel@tonic-gate 		if (ack_mp != NULL)
13957c478bd9Sstevel@tonic-gate 			freeb(ack_mp);
1396f4b3ec61Sdh 		netstack_rele(sctp_connp->conn_netstack);
1397f4b3ec61Sdh 		sctp_connp->conn_netstack = NULL;
1398f4b3ec61Sdh 		sctp->sctp_sctps = NULL;
1399f4b3ec61Sdh 		SCTP_G_Q_REFRELE(sctps);
14007c478bd9Sstevel@tonic-gate 		kmem_cache_free(sctp_conn_cache, sctp_connp);
14017c478bd9Sstevel@tonic-gate 		return (NULL);
14027c478bd9Sstevel@tonic-gate 	}
14037c478bd9Sstevel@tonic-gate 
14047c478bd9Sstevel@tonic-gate 	sctp->sctp_ack_mp = ack_mp;
14057c478bd9Sstevel@tonic-gate 	sctp->sctp_heartbeat_mp = hb_mp;
14067c478bd9Sstevel@tonic-gate 
14077c478bd9Sstevel@tonic-gate 	switch (family) {
14087c478bd9Sstevel@tonic-gate 	case AF_INET6:
14097c478bd9Sstevel@tonic-gate 		sctp_connp->conn_af_isv6 = B_TRUE;
14107c478bd9Sstevel@tonic-gate 		sctp->sctp_ipversion = IPV6_VERSION;
14117c478bd9Sstevel@tonic-gate 		sctp->sctp_family = AF_INET6;
14127c478bd9Sstevel@tonic-gate 		break;
14137c478bd9Sstevel@tonic-gate 
14147c478bd9Sstevel@tonic-gate 	case AF_INET:
14157c478bd9Sstevel@tonic-gate 		sctp_connp->conn_af_isv6 = B_FALSE;
14167c478bd9Sstevel@tonic-gate 		sctp_connp->conn_pkt_isv6 = B_FALSE;
14177c478bd9Sstevel@tonic-gate 		sctp->sctp_ipversion = IPV4_VERSION;
14187c478bd9Sstevel@tonic-gate 		sctp->sctp_family = AF_INET;
14197c478bd9Sstevel@tonic-gate 		break;
14207c478bd9Sstevel@tonic-gate 	default:
14217c478bd9Sstevel@tonic-gate 		ASSERT(0);
14227c478bd9Sstevel@tonic-gate 		break;
14237c478bd9Sstevel@tonic-gate 	}
14247c478bd9Sstevel@tonic-gate 	if (sctp_init_values(sctp, psctp, sleep) != 0) {
14257c478bd9Sstevel@tonic-gate 		freeb(ack_mp);
14267c478bd9Sstevel@tonic-gate 		freeb(hb_mp);
1427f4b3ec61Sdh 		netstack_rele(sctp_connp->conn_netstack);
1428f4b3ec61Sdh 		sctp_connp->conn_netstack = NULL;
1429f4b3ec61Sdh 		sctp->sctp_sctps = NULL;
1430f4b3ec61Sdh 		SCTP_G_Q_REFRELE(sctps);
14317c478bd9Sstevel@tonic-gate 		kmem_cache_free(sctp_conn_cache, sctp_connp);
14327c478bd9Sstevel@tonic-gate 		return (NULL);
14337c478bd9Sstevel@tonic-gate 	}
14347c478bd9Sstevel@tonic-gate 	sctp->sctp_cansleep = ((flags & SCTP_CAN_BLOCK) == SCTP_CAN_BLOCK);
14357c478bd9Sstevel@tonic-gate 
1436f4b3ec61Sdh 	sctp->sctp_mss = sctps->sctps_initial_mtu - ((family == AF_INET6) ?
14377c478bd9Sstevel@tonic-gate 		sctp->sctp_hdr6_len : sctp->sctp_hdr_len);
14387c478bd9Sstevel@tonic-gate 
14397c478bd9Sstevel@tonic-gate 	if (psctp != NULL) {
14407c478bd9Sstevel@tonic-gate 		RUN_SCTP(psctp);
14417c478bd9Sstevel@tonic-gate 		/*
14427c478bd9Sstevel@tonic-gate 		 * Inherit local address list, local port. Parent is either
14437c478bd9Sstevel@tonic-gate 		 * in SCTPS_BOUND, or SCTPS_LISTEN state.
14447c478bd9Sstevel@tonic-gate 		 */
14457c478bd9Sstevel@tonic-gate 		ASSERT((psctp->sctp_state == SCTPS_BOUND) ||
14467c478bd9Sstevel@tonic-gate 		    (psctp->sctp_state == SCTPS_LISTEN));
14477c478bd9Sstevel@tonic-gate 		if (sctp_dup_saddrs(psctp, sctp, sleep)) {
14487c478bd9Sstevel@tonic-gate 			WAKE_SCTP(psctp);
14497c478bd9Sstevel@tonic-gate 			freeb(ack_mp);
14507c478bd9Sstevel@tonic-gate 			freeb(hb_mp);
14517c478bd9Sstevel@tonic-gate 			sctp_headers_free(sctp);
1452f4b3ec61Sdh 			netstack_rele(sctps->sctps_netstack);
1453f4b3ec61Sdh 			sctp_connp->conn_netstack = NULL;
1454f4b3ec61Sdh 			sctp->sctp_sctps = NULL;
1455f4b3ec61Sdh 			SCTP_G_Q_REFRELE(sctps);
14567c478bd9Sstevel@tonic-gate 			kmem_cache_free(sctp_conn_cache, sctp_connp);
14577c478bd9Sstevel@tonic-gate 			return (NULL);
14587c478bd9Sstevel@tonic-gate 		}
14597c478bd9Sstevel@tonic-gate 
14607c478bd9Sstevel@tonic-gate 		/*
14617c478bd9Sstevel@tonic-gate 		 * If the parent is specified, it'll be immediatelly
14627c478bd9Sstevel@tonic-gate 		 * followed by sctp_connect(). So don't add this guy to
14637c478bd9Sstevel@tonic-gate 		 * bind hash.
14647c478bd9Sstevel@tonic-gate 		 */
14657c478bd9Sstevel@tonic-gate 		sctp->sctp_lport = psctp->sctp_lport;
14667c478bd9Sstevel@tonic-gate 		sctp->sctp_state = SCTPS_BOUND;
14675d0bc3edSsommerfe 		sctp->sctp_allzones = psctp->sctp_allzones;
14687c478bd9Sstevel@tonic-gate 		sctp->sctp_zoneid = psctp->sctp_zoneid;
14697c478bd9Sstevel@tonic-gate 		WAKE_SCTP(psctp);
14707c478bd9Sstevel@tonic-gate 	} else {
1471f4b3ec61Sdh 		sctp->sctp_zoneid = zoneid;
14727c478bd9Sstevel@tonic-gate 	}
14737c478bd9Sstevel@tonic-gate 
14747c478bd9Sstevel@tonic-gate 	sctp_connp->conn_cred = credp;
14757c478bd9Sstevel@tonic-gate 	crhold(credp);
14767c478bd9Sstevel@tonic-gate 
147745916cd2Sjpk 	/*
147845916cd2Sjpk 	 * If the caller has the process-wide flag set, then default to MAC
147945916cd2Sjpk 	 * exempt mode.  This allows read-down to unlabeled hosts.
148045916cd2Sjpk 	 */
148145916cd2Sjpk 	if (getpflags(NET_MAC_AWARE, credp) != 0)
148245916cd2Sjpk 		sctp_connp->conn_mac_exempt = B_TRUE;
148345916cd2Sjpk 
14847c478bd9Sstevel@tonic-gate 	/* Initialize SCTP instance values,  our verf tag must never be 0 */
14857c478bd9Sstevel@tonic-gate 	(void) random_get_pseudo_bytes((uint8_t *)&sctp->sctp_lvtag,
14867c478bd9Sstevel@tonic-gate 	    sizeof (sctp->sctp_lvtag));
14877c478bd9Sstevel@tonic-gate 	if (sctp->sctp_lvtag == 0)
14887c478bd9Sstevel@tonic-gate 		sctp->sctp_lvtag = (uint32_t)gethrtime();
14897c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_lvtag != 0);
14907c478bd9Sstevel@tonic-gate 
14917c478bd9Sstevel@tonic-gate 	sctp->sctp_ltsn = sctp->sctp_lvtag + 1;
14927c478bd9Sstevel@tonic-gate 	sctp->sctp_lcsn = sctp->sctp_ltsn;
14937c478bd9Sstevel@tonic-gate 	sctp->sctp_recovery_tsn = sctp->sctp_lastack_rxd = sctp->sctp_ltsn - 1;
14947c478bd9Sstevel@tonic-gate 	sctp->sctp_adv_pap = sctp->sctp_lastack_rxd;
14957c478bd9Sstevel@tonic-gate 
14967c478bd9Sstevel@tonic-gate 	/* Information required by upper layer */
14977c478bd9Sstevel@tonic-gate 	if (sctp_ulpd != NULL) {
14987c478bd9Sstevel@tonic-gate 		sctp->sctp_ulpd = sctp_ulpd;
14997c478bd9Sstevel@tonic-gate 
15007c478bd9Sstevel@tonic-gate 		ASSERT(sctp_upcalls != NULL);
15017c478bd9Sstevel@tonic-gate 		bcopy(sctp_upcalls, &sctp->sctp_upcalls,
15027c478bd9Sstevel@tonic-gate 		    sizeof (sctp_upcalls_t));
15037c478bd9Sstevel@tonic-gate 		ASSERT(sbl != NULL);
15047c478bd9Sstevel@tonic-gate 		/* Fill in the socket buffer limits for sctpsockfs */
15057c478bd9Sstevel@tonic-gate 		sbl->sbl_txlowat = sctp->sctp_xmit_lowater;
15067c478bd9Sstevel@tonic-gate 		sbl->sbl_txbuf = sctp->sctp_xmit_hiwater;
15077c478bd9Sstevel@tonic-gate 		sbl->sbl_rxbuf = sctp->sctp_rwnd;
15087c478bd9Sstevel@tonic-gate 		sbl->sbl_rxlowat = SCTP_RECV_LOWATER;
15097c478bd9Sstevel@tonic-gate 	}
15107c478bd9Sstevel@tonic-gate 	/* If no sctp_ulpd, must be creating the default sctp */
1511f4b3ec61Sdh 	ASSERT(sctp_ulpd != NULL || sctps->sctps_gsctp == NULL);
15127c478bd9Sstevel@tonic-gate 
15137c478bd9Sstevel@tonic-gate 	/* Insert this in the global list. */
1514f4b3ec61Sdh 	SCTP_LINK(sctp, sctps);
15157c478bd9Sstevel@tonic-gate 
15167c478bd9Sstevel@tonic-gate 	return (sctp);
15177c478bd9Sstevel@tonic-gate }
15187c478bd9Sstevel@tonic-gate 
1519f4b3ec61Sdh /*
1520f4b3ec61Sdh  * Make sure we wait until the default queue is setup, yet allow
1521f4b3ec61Sdh  * sctp_g_q_create() to open a SCTP stream.
1522f4b3ec61Sdh  * We need to allow sctp_g_q_create() do do an open
1523f4b3ec61Sdh  * of sctp, hence we compare curhread.
1524f4b3ec61Sdh  * All others have to wait until the sctps_g_q has been
1525f4b3ec61Sdh  * setup.
1526f4b3ec61Sdh  */
15277c478bd9Sstevel@tonic-gate void
1528f4b3ec61Sdh sctp_g_q_setup(sctp_stack_t *sctps)
15297c478bd9Sstevel@tonic-gate {
1530f4b3ec61Sdh 	mutex_enter(&sctps->sctps_g_q_lock);
1531f4b3ec61Sdh 	if (sctps->sctps_g_q != NULL) {
1532f4b3ec61Sdh 		mutex_exit(&sctps->sctps_g_q_lock);
1533f4b3ec61Sdh 		return;
1534f4b3ec61Sdh 	}
1535f4b3ec61Sdh 	if (sctps->sctps_g_q_creator == NULL) {
1536f4b3ec61Sdh 		/* This thread will set it up */
1537f4b3ec61Sdh 		sctps->sctps_g_q_creator = curthread;
1538f4b3ec61Sdh 		mutex_exit(&sctps->sctps_g_q_lock);
1539f4b3ec61Sdh 		sctp_g_q_create(sctps);
1540f4b3ec61Sdh 		mutex_enter(&sctps->sctps_g_q_lock);
1541f4b3ec61Sdh 		ASSERT(sctps->sctps_g_q_creator == curthread);
1542f4b3ec61Sdh 		sctps->sctps_g_q_creator = NULL;
1543f4b3ec61Sdh 		cv_signal(&sctps->sctps_g_q_cv);
1544f4b3ec61Sdh 		ASSERT(sctps->sctps_g_q != NULL);
1545f4b3ec61Sdh 		mutex_exit(&sctps->sctps_g_q_lock);
1546f4b3ec61Sdh 		return;
1547f4b3ec61Sdh 	}
1548f4b3ec61Sdh 	/* Everybody but the creator has to wait */
1549f4b3ec61Sdh 	if (sctps->sctps_g_q_creator != curthread) {
1550f4b3ec61Sdh 		while (sctps->sctps_g_q == NULL)
1551f4b3ec61Sdh 			cv_wait(&sctps->sctps_g_q_cv, &sctps->sctps_g_q_lock);
1552f4b3ec61Sdh 	}
1553f4b3ec61Sdh 	mutex_exit(&sctps->sctps_g_q_lock);
1554f4b3ec61Sdh }
15557c478bd9Sstevel@tonic-gate 
1556f4b3ec61Sdh major_t IP_MAJ;
1557f4b3ec61Sdh #define	IP	"ip"
1558f4b3ec61Sdh 
1559f4b3ec61Sdh #define	SCTP6DEV		"/devices/pseudo/sctp6@0:sctp6"
1560f4b3ec61Sdh 
1561f4b3ec61Sdh /*
1562f4b3ec61Sdh  * Create a default sctp queue here instead of in strplumb
1563f4b3ec61Sdh  */
1564f4b3ec61Sdh void
1565f4b3ec61Sdh sctp_g_q_create(sctp_stack_t *sctps)
1566f4b3ec61Sdh {
1567f4b3ec61Sdh 	int error;
1568f4b3ec61Sdh 	ldi_handle_t	lh = NULL;
1569f4b3ec61Sdh 	ldi_ident_t	li = NULL;
1570f4b3ec61Sdh 	int		rval;
1571f4b3ec61Sdh 	cred_t		*cr;
1572f4b3ec61Sdh 
1573f4b3ec61Sdh #ifdef NS_DEBUG
1574f4b3ec61Sdh 	(void) printf("sctp_g_q_create()for stack %d\n",
1575f4b3ec61Sdh 	    sctps->sctps_netstack->netstack_stackid);
1576f4b3ec61Sdh #endif
1577f4b3ec61Sdh 
1578f4b3ec61Sdh 	ASSERT(sctps->sctps_g_q_creator == curthread);
1579f4b3ec61Sdh 
1580f4b3ec61Sdh 	error = ldi_ident_from_major(IP_MAJ, &li);
1581f4b3ec61Sdh 	if (error) {
1582f4b3ec61Sdh #ifdef DEBUG
1583f4b3ec61Sdh 		printf("sctp_g_q_create: lyr ident get failed error %d\n",
1584f4b3ec61Sdh 		    error);
1585f4b3ec61Sdh #endif
1586f4b3ec61Sdh 		return;
1587f4b3ec61Sdh 	}
1588f4b3ec61Sdh 
1589f4b3ec61Sdh 	cr = zone_get_kcred(netstackid_to_zoneid(
1590f4b3ec61Sdh 				sctps->sctps_netstack->netstack_stackid));
1591f4b3ec61Sdh 	ASSERT(cr != NULL);
1592f4b3ec61Sdh 	/*
1593f4b3ec61Sdh 	 * We set the sctp default queue to IPv6 because IPv4 falls
1594f4b3ec61Sdh 	 * back to IPv6 when it can't find a client, but
1595f4b3ec61Sdh 	 * IPv6 does not fall back to IPv4.
1596f4b3ec61Sdh 	 */
1597f4b3ec61Sdh 	error = ldi_open_by_name(SCTP6DEV, FREAD|FWRITE, cr, &lh, li);
1598f4b3ec61Sdh 	if (error) {
1599f4b3ec61Sdh #ifdef DEBUG
1600f4b3ec61Sdh 		printf("sctp_g_q_create: open of SCTP6DEV failed error %d\n",
1601f4b3ec61Sdh 		    error);
1602f4b3ec61Sdh #endif
1603f4b3ec61Sdh 		goto out;
1604f4b3ec61Sdh 	}
1605f4b3ec61Sdh 
1606f4b3ec61Sdh 	/*
1607f4b3ec61Sdh 	 * This ioctl causes the sctp framework to cache a pointer to
1608f4b3ec61Sdh 	 * this stream, so we don't want to close the stream after
1609f4b3ec61Sdh 	 * this operation.
1610f4b3ec61Sdh 	 * Use the kernel credentials that are for the zone we're in.
1611f4b3ec61Sdh 	 */
1612f4b3ec61Sdh 	error = ldi_ioctl(lh, SCTP_IOC_DEFAULT_Q,
1613f4b3ec61Sdh 	    (intptr_t)0, FKIOCTL, cr, &rval);
1614f4b3ec61Sdh 	if (error) {
1615f4b3ec61Sdh #ifdef DEBUG
1616f4b3ec61Sdh 		printf("sctp_g_q_create: ioctl SCTP_IOC_DEFAULT_Q failed "
1617f4b3ec61Sdh 		    "error %d\n", error);
1618f4b3ec61Sdh #endif
1619f4b3ec61Sdh 		goto out;
1620f4b3ec61Sdh 	}
1621f4b3ec61Sdh 	sctps->sctps_g_q_lh = lh;	/* For sctp_g_q_inactive */
1622f4b3ec61Sdh 	lh = NULL;
1623f4b3ec61Sdh out:
1624f4b3ec61Sdh 	/* Close layered handles */
1625f4b3ec61Sdh 	if (li)
1626f4b3ec61Sdh 		ldi_ident_release(li);
1627f4b3ec61Sdh 	/* Keep cred around until _inactive needs it */
1628f4b3ec61Sdh 	sctps->sctps_g_q_cr = cr;
1629f4b3ec61Sdh }
1630f4b3ec61Sdh 
1631f4b3ec61Sdh /*
1632f4b3ec61Sdh  * Remove the sctp_default queue so that new connections will not find it.
1633f4b3ec61Sdh  * SCTP uses sctp_g_q for all transmission, so all sctp'ts implicitly
1634f4b3ec61Sdh  * refer to it. Hence have each one have a reference on sctp_g_q_ref!
1635f4b3ec61Sdh  *
1636f4b3ec61Sdh  * We decrement the refcnt added in sctp_g_q_create. Once all the
1637f4b3ec61Sdh  * sctp_t's which use the default go away, sctp_g_q_close will be called
1638f4b3ec61Sdh  * and close the sctp_g_q. Once sctp_g_q is closed, sctp_close() will drop the
1639f4b3ec61Sdh  * last reference count on the stack by calling netstack_rele().
1640f4b3ec61Sdh  */
1641f4b3ec61Sdh void
1642f4b3ec61Sdh sctp_g_q_destroy(sctp_stack_t *sctps)
1643f4b3ec61Sdh {
1644f4b3ec61Sdh 	if (sctps->sctps_g_q == NULL) {
1645f4b3ec61Sdh 		return;	/* Nothing to cleanup */
1646f4b3ec61Sdh 	}
1647f4b3ec61Sdh 	/*
1648f4b3ec61Sdh 	 * Keep sctps_g_q and sctps_gsctp until the last reference has
1649f4b3ec61Sdh 	 * dropped, since the output is always done using those.
1650f4b3ec61Sdh 	 * Need to decrement twice to take sctp_g_q_create and
1651f4b3ec61Sdh 	 * the gsctp reference into account so that sctp_g_q_inactive is called
1652f4b3ec61Sdh 	 * when all but the default queue remains.
1653f4b3ec61Sdh 	 */
1654f4b3ec61Sdh #ifdef NS_DEBUG
1655f4b3ec61Sdh 	(void) printf("sctp_g_q_destroy: ref %d\n",
1656f4b3ec61Sdh 	    sctps->sctps_g_q_ref);
1657f4b3ec61Sdh #endif
1658f4b3ec61Sdh 	SCTP_G_Q_REFRELE(sctps);
1659f4b3ec61Sdh }
1660f4b3ec61Sdh 
1661f4b3ec61Sdh /*
1662f4b3ec61Sdh  * Called when last user (could be sctp_g_q_destroy) drops reference count
1663f4b3ec61Sdh  * using SCTP_G_Q_REFRELE.
1664f4b3ec61Sdh  * Run by sctp_q_q_inactive using a taskq.
1665f4b3ec61Sdh  */
1666f4b3ec61Sdh static void
1667f4b3ec61Sdh sctp_g_q_close(void *arg)
1668f4b3ec61Sdh {
1669f4b3ec61Sdh 	sctp_stack_t *sctps = arg;
1670f4b3ec61Sdh 	int error;
1671f4b3ec61Sdh 	ldi_handle_t	lh = NULL;
1672f4b3ec61Sdh 	ldi_ident_t	li = NULL;
1673f4b3ec61Sdh 	cred_t		*cr;
1674f4b3ec61Sdh 
1675f4b3ec61Sdh 	lh = sctps->sctps_g_q_lh;
1676f4b3ec61Sdh 	if (lh == NULL)
1677f4b3ec61Sdh 		return;	/* Nothing to cleanup */
1678f4b3ec61Sdh 
1679f4b3ec61Sdh 	error = ldi_ident_from_major(IP_MAJ, &li);
1680f4b3ec61Sdh 	if (error) {
1681f4b3ec61Sdh #ifdef NS_DEBUG
1682f4b3ec61Sdh 		printf("sctp_g_q_inactive: lyr ident get failed error %d\n",
1683f4b3ec61Sdh 		    error);
1684f4b3ec61Sdh #endif
1685f4b3ec61Sdh 		return;
1686f4b3ec61Sdh 	}
1687f4b3ec61Sdh 
1688f4b3ec61Sdh 	cr = sctps->sctps_g_q_cr;
1689f4b3ec61Sdh 	sctps->sctps_g_q_cr = NULL;
1690f4b3ec61Sdh 	ASSERT(cr != NULL);
1691f4b3ec61Sdh 
1692f4b3ec61Sdh 	/*
1693f4b3ec61Sdh 	 * Make sure we can break the recursion when sctp_close decrements
1694f4b3ec61Sdh 	 * the reference count causing g_q_inactive to be called again.
1695f4b3ec61Sdh 	 */
1696f4b3ec61Sdh 	sctps->sctps_g_q_lh = NULL;
16977c478bd9Sstevel@tonic-gate 
1698f4b3ec61Sdh 	/* close the default queue */
1699f4b3ec61Sdh 	(void) ldi_close(lh, FREAD|FWRITE, cr);
1700f4b3ec61Sdh 
1701f4b3ec61Sdh 	/* Close layered handles */
1702f4b3ec61Sdh 	ldi_ident_release(li);
1703f4b3ec61Sdh 	crfree(cr);
1704f4b3ec61Sdh 
1705f4b3ec61Sdh 	ASSERT(sctps->sctps_g_q != NULL);
1706f4b3ec61Sdh 	sctps->sctps_g_q = NULL;
1707f4b3ec61Sdh 	/*
1708f4b3ec61Sdh 	 * Now free sctps_gsctp.
1709f4b3ec61Sdh 	 */
1710f4b3ec61Sdh 	ASSERT(sctps->sctps_gsctp != NULL);
1711f4b3ec61Sdh 	sctp_closei_local(sctps->sctps_gsctp);
1712f4b3ec61Sdh 	SCTP_CONDEMNED(sctps->sctps_gsctp);
1713f4b3ec61Sdh 	SCTP_REFRELE(sctps->sctps_gsctp);
1714f4b3ec61Sdh 	sctps->sctps_gsctp = NULL;
1715f4b3ec61Sdh }
17167c478bd9Sstevel@tonic-gate 
1717f4b3ec61Sdh /*
1718f4b3ec61Sdh  * Called when last sctp_t drops reference count using SCTP_G_Q_REFRELE.
1719f4b3ec61Sdh  *
1720f4b3ec61Sdh  * Have to ensure that the ldi routines are not used by an
1721f4b3ec61Sdh  * interrupt thread by using a taskq.
1722f4b3ec61Sdh  */
1723f4b3ec61Sdh void
1724f4b3ec61Sdh sctp_g_q_inactive(sctp_stack_t *sctps)
1725f4b3ec61Sdh {
1726f4b3ec61Sdh 	if (sctps->sctps_g_q_lh == NULL)
1727f4b3ec61Sdh 		return;	/* Nothing to cleanup */
1728f4b3ec61Sdh 
1729f4b3ec61Sdh 	ASSERT(sctps->sctps_g_q_ref == 0);
1730f4b3ec61Sdh 	SCTP_G_Q_REFHOLD(sctps); /* Compensate for what g_q_destroy did */
1731f4b3ec61Sdh 
1732f4b3ec61Sdh 	if (servicing_interrupt()) {
1733f4b3ec61Sdh 		(void) taskq_dispatch(sctp_taskq, sctp_g_q_close,
1734f4b3ec61Sdh 			    (void *) sctps, TQ_SLEEP);
1735f4b3ec61Sdh 	} else {
1736f4b3ec61Sdh 		sctp_g_q_close(sctps);
17377c478bd9Sstevel@tonic-gate 	}
1738f4b3ec61Sdh }
1739f4b3ec61Sdh 
1740f4b3ec61Sdh /* Run at module load time */
1741f4b3ec61Sdh void
1742f4b3ec61Sdh sctp_ddi_g_init(void)
1743f4b3ec61Sdh {
1744f4b3ec61Sdh 	IP_MAJ = ddi_name_to_major(IP);
17457c478bd9Sstevel@tonic-gate 
17467c478bd9Sstevel@tonic-gate 	/* Create sctp_t/conn_t cache */
17477c478bd9Sstevel@tonic-gate 	sctp_conn_cache_init();
17487c478bd9Sstevel@tonic-gate 
17497c478bd9Sstevel@tonic-gate 	/* Create the faddr cache */
17507c478bd9Sstevel@tonic-gate 	sctp_faddr_init();
17517c478bd9Sstevel@tonic-gate 
17527c478bd9Sstevel@tonic-gate 	/* Create the sets cache */
17537c478bd9Sstevel@tonic-gate 	sctp_sets_init();
17547c478bd9Sstevel@tonic-gate 
17557c478bd9Sstevel@tonic-gate 	/* Create the PR-SCTP sets cache */
17567c478bd9Sstevel@tonic-gate 	sctp_ftsn_sets_init();
17577c478bd9Sstevel@tonic-gate 
1758f4b3ec61Sdh 	/* Initialize tables used for CRC calculation */
1759f4b3ec61Sdh 	sctp_crc32_init();
1760f4b3ec61Sdh 
1761f4b3ec61Sdh 	sctp_taskq = taskq_create("sctp_taskq", 1, minclsyspri, 1, 1,
1762f4b3ec61Sdh 	    TASKQ_PREPOPULATE);
1763f4b3ec61Sdh 
1764f4b3ec61Sdh 	/*
1765f4b3ec61Sdh 	 * We want to be informed each time a stack is created or
1766f4b3ec61Sdh 	 * destroyed in the kernel, so we can maintain the
1767f4b3ec61Sdh 	 * set of sctp_stack_t's.
1768f4b3ec61Sdh 	 */
1769f4b3ec61Sdh 	netstack_register(NS_SCTP, sctp_stack_init, sctp_stack_shutdown,
1770f4b3ec61Sdh 	    sctp_stack_fini);
1771f4b3ec61Sdh }
1772f4b3ec61Sdh 
1773f4b3ec61Sdh static void *
1774f4b3ec61Sdh sctp_stack_init(netstackid_t stackid, netstack_t *ns)
1775f4b3ec61Sdh {
1776f4b3ec61Sdh 	sctp_stack_t	*sctps;
1777f4b3ec61Sdh 
1778f4b3ec61Sdh 	sctps = kmem_zalloc(sizeof (*sctps), KM_SLEEP);
1779f4b3ec61Sdh 	sctps->sctps_netstack = ns;
1780f4b3ec61Sdh 
1781f4b3ec61Sdh 	/* Initialize locks */
1782f4b3ec61Sdh 	mutex_init(&sctps->sctps_g_q_lock, NULL, MUTEX_DEFAULT, NULL);
1783f4b3ec61Sdh 	cv_init(&sctps->sctps_g_q_cv, NULL, CV_DEFAULT, NULL);
1784f4b3ec61Sdh 	mutex_init(&sctps->sctps_g_lock, NULL, MUTEX_DEFAULT, NULL);
1785f4b3ec61Sdh 	mutex_init(&sctps->sctps_epriv_port_lock, NULL, MUTEX_DEFAULT, NULL);
1786f4b3ec61Sdh 	sctps->sctps_g_num_epriv_ports = SCTP_NUM_EPRIV_PORTS;
1787f4b3ec61Sdh 	sctps->sctps_g_epriv_ports[0] = 2049;
1788f4b3ec61Sdh 	sctps->sctps_g_epriv_ports[1] = 4045;
1789f4b3ec61Sdh 
1790f4b3ec61Sdh 	/* Initialize SCTP hash arrays. */
1791f4b3ec61Sdh 	sctp_hash_init(sctps);
1792f4b3ec61Sdh 
1793f4b3ec61Sdh 	sctps->sctps_pad_mp = allocb(SCTP_ALIGN, BPRI_MED);
1794f4b3ec61Sdh 	bzero(sctps->sctps_pad_mp->b_rptr, SCTP_ALIGN);
1795f4b3ec61Sdh 	ASSERT(sctps->sctps_pad_mp);
1796f4b3ec61Sdh 
1797f4b3ec61Sdh 	if (!sctp_nd_init(sctps)) {
1798f4b3ec61Sdh 		sctp_nd_free(sctps);
1799f4b3ec61Sdh 	}
1800f4b3ec61Sdh 
18017c478bd9Sstevel@tonic-gate 	/* Initialize the recvq taskq. */
1802f4b3ec61Sdh 	sctp_rq_tq_init(sctps);
18037c478bd9Sstevel@tonic-gate 
18047c478bd9Sstevel@tonic-gate 	/* saddr init */
1805f4b3ec61Sdh 	sctp_saddr_init(sctps);
18067c478bd9Sstevel@tonic-gate 
18077c478bd9Sstevel@tonic-gate 	/* Global SCTP PCB list. */
1808f4b3ec61Sdh 	list_create(&sctps->sctps_g_list, sizeof (sctp_t),
18097c478bd9Sstevel@tonic-gate 	    offsetof(sctp_t, sctp_list));
18107c478bd9Sstevel@tonic-gate 
18117c478bd9Sstevel@tonic-gate 	/* Initialize sctp kernel stats. */
1812f4b3ec61Sdh 	sctps->sctps_mibkp = sctp_kstat_init(stackid);
1813f4b3ec61Sdh 	sctps->sctps_kstat =
1814f4b3ec61Sdh 	    sctp_kstat2_init(stackid, &sctps->sctps_statistics);
1815f4b3ec61Sdh 
1816f4b3ec61Sdh 	return (sctps);
18177c478bd9Sstevel@tonic-gate }
18187c478bd9Sstevel@tonic-gate 
1819f4b3ec61Sdh /*
1820f4b3ec61Sdh  * Called when the module is about to be unloaded.
1821f4b3ec61Sdh  */
18227c478bd9Sstevel@tonic-gate void
1823f4b3ec61Sdh sctp_ddi_g_destroy(void)
18247c478bd9Sstevel@tonic-gate {
18257c478bd9Sstevel@tonic-gate 	/* Destroy sctp_t/conn_t caches */
18267c478bd9Sstevel@tonic-gate 	sctp_conn_cache_fini();
18277c478bd9Sstevel@tonic-gate 
18287c478bd9Sstevel@tonic-gate 	/* Destroy the faddr cache */
18297c478bd9Sstevel@tonic-gate 	sctp_faddr_fini();
18307c478bd9Sstevel@tonic-gate 
18317c478bd9Sstevel@tonic-gate 	/* Destroy the sets cache */
18327c478bd9Sstevel@tonic-gate 	sctp_sets_fini();
18337c478bd9Sstevel@tonic-gate 
18347c478bd9Sstevel@tonic-gate 	/* Destroy the PR-SCTP sets cache */
18357c478bd9Sstevel@tonic-gate 	sctp_ftsn_sets_fini();
18367c478bd9Sstevel@tonic-gate 
1837f4b3ec61Sdh 	netstack_unregister(NS_SCTP);
1838f4b3ec61Sdh 	taskq_destroy(sctp_taskq);
1839f4b3ec61Sdh }
1840f4b3ec61Sdh 
1841f4b3ec61Sdh /*
1842f4b3ec61Sdh  * Shut down the SCTP stack instance.
1843f4b3ec61Sdh  */
1844f4b3ec61Sdh /* ARGSUSED */
1845f4b3ec61Sdh static void
1846f4b3ec61Sdh sctp_stack_shutdown(netstackid_t stackid, void *arg)
1847f4b3ec61Sdh {
1848f4b3ec61Sdh 	sctp_stack_t *sctps = (sctp_stack_t *)arg;
1849f4b3ec61Sdh 
1850f4b3ec61Sdh 	sctp_g_q_destroy(sctps);
1851f4b3ec61Sdh }
1852f4b3ec61Sdh 
1853f4b3ec61Sdh /*
1854f4b3ec61Sdh  * Free the SCTP stack instance.
1855f4b3ec61Sdh  */
1856f4b3ec61Sdh static void
1857f4b3ec61Sdh sctp_stack_fini(netstackid_t stackid, void *arg)
1858f4b3ec61Sdh {
1859f4b3ec61Sdh 	sctp_stack_t *sctps = (sctp_stack_t *)arg;
1860f4b3ec61Sdh 
1861f4b3ec61Sdh 	sctp_nd_free(sctps);
1862f4b3ec61Sdh 
18637c478bd9Sstevel@tonic-gate 	/* Destroy the recvq taskqs. */
1864f4b3ec61Sdh 	sctp_rq_tq_fini(sctps);
18657c478bd9Sstevel@tonic-gate 
18667c478bd9Sstevel@tonic-gate 	/* Destroy saddr  */
1867f4b3ec61Sdh 	sctp_saddr_fini(sctps);
18687c478bd9Sstevel@tonic-gate 
18697c478bd9Sstevel@tonic-gate 	/* Global SCTP PCB list. */
1870f4b3ec61Sdh 	list_destroy(&sctps->sctps_g_list);
18717c478bd9Sstevel@tonic-gate 
18727c478bd9Sstevel@tonic-gate 	/* Destroy SCTP hash arrays. */
1873f4b3ec61Sdh 	sctp_hash_destroy(sctps);
1874f4b3ec61Sdh 
1875f4b3ec61Sdh 	/* Destroy SCTP kernel stats. */
1876f4b3ec61Sdh 	sctp_kstat2_fini(stackid, sctps->sctps_kstat);
1877f4b3ec61Sdh 	sctps->sctps_kstat = NULL;
1878f4b3ec61Sdh 	bzero(&sctps->sctps_statistics, sizeof (sctps->sctps_statistics));
1879f4b3ec61Sdh 
1880f4b3ec61Sdh 	sctp_kstat_fini(stackid, sctps->sctps_mibkp);
1881f4b3ec61Sdh 	sctps->sctps_mibkp = NULL;
1882f4b3ec61Sdh 
1883f4b3ec61Sdh 	freeb(sctps->sctps_pad_mp);
1884f4b3ec61Sdh 	sctps->sctps_pad_mp = NULL;
18857c478bd9Sstevel@tonic-gate 
1886f4b3ec61Sdh 	mutex_destroy(&sctps->sctps_g_lock);
1887f4b3ec61Sdh 	mutex_destroy(&sctps->sctps_epriv_port_lock);
1888f4b3ec61Sdh 	mutex_destroy(&sctps->sctps_g_q_lock);
1889f4b3ec61Sdh 	cv_destroy(&sctps->sctps_g_q_cv);
18907c478bd9Sstevel@tonic-gate 
1891f4b3ec61Sdh 	kmem_free(sctps, sizeof (*sctps));
18927c478bd9Sstevel@tonic-gate }
18937c478bd9Sstevel@tonic-gate 
18947c478bd9Sstevel@tonic-gate void
1895f4b3ec61Sdh sctp_display_all(sctp_stack_t *sctps)
18967c478bd9Sstevel@tonic-gate {
18977c478bd9Sstevel@tonic-gate 	sctp_t *sctp_walker;
18987c478bd9Sstevel@tonic-gate 
1899f4b3ec61Sdh 	mutex_enter(&sctps->sctps_g_lock);
1900f4b3ec61Sdh 	for (sctp_walker = sctps->sctps_gsctp; sctp_walker != NULL;
1901f4b3ec61Sdh 	    sctp_walker = (sctp_t *)list_next(&sctps->sctps_g_list,
1902f4b3ec61Sdh 	    sctp_walker)) {
19037c478bd9Sstevel@tonic-gate 		(void) sctp_display(sctp_walker, NULL);
19047c478bd9Sstevel@tonic-gate 	}
1905f4b3ec61Sdh 	mutex_exit(&sctps->sctps_g_lock);
19067c478bd9Sstevel@tonic-gate }
19077c478bd9Sstevel@tonic-gate 
19087c478bd9Sstevel@tonic-gate static void
1909f4b3ec61Sdh sctp_rq_tq_init(sctp_stack_t *sctps)
19107c478bd9Sstevel@tonic-gate {
1911f4b3ec61Sdh 	sctps->sctps_recvq_tq_list_max_sz = 16;
1912f4b3ec61Sdh 	sctps->sctps_recvq_tq_list_cur_sz = 1;
19137c478bd9Sstevel@tonic-gate 	/*
19147c478bd9Sstevel@tonic-gate 	 * Initialize the recvq_tq_list and create the first recvq taskq.
19157c478bd9Sstevel@tonic-gate 	 * What to do if it fails?
19167c478bd9Sstevel@tonic-gate 	 */
1917f4b3ec61Sdh 	sctps->sctps_recvq_tq_list =
1918f4b3ec61Sdh 	    kmem_zalloc(sctps->sctps_recvq_tq_list_max_sz * sizeof (taskq_t *),
1919f4b3ec61Sdh 		KM_SLEEP);
1920f4b3ec61Sdh 	sctps->sctps_recvq_tq_list[0] = taskq_create("sctp_def_recvq_taskq",
19217c478bd9Sstevel@tonic-gate 	    MIN(sctp_recvq_tq_thr_max, MAX(sctp_recvq_tq_thr_min, ncpus)),
19227c478bd9Sstevel@tonic-gate 	    minclsyspri, sctp_recvq_tq_task_min, sctp_recvq_tq_task_max,
19237c478bd9Sstevel@tonic-gate 	    TASKQ_PREPOPULATE);
1924f4b3ec61Sdh 	mutex_init(&sctps->sctps_rq_tq_lock, NULL, MUTEX_DEFAULT, NULL);
19257c478bd9Sstevel@tonic-gate }
19267c478bd9Sstevel@tonic-gate 
19277c478bd9Sstevel@tonic-gate static void
1928f4b3ec61Sdh sctp_rq_tq_fini(sctp_stack_t *sctps)
19297c478bd9Sstevel@tonic-gate {
19307c478bd9Sstevel@tonic-gate 	int i;
19317c478bd9Sstevel@tonic-gate 
1932f4b3ec61Sdh 	for (i = 0; i < sctps->sctps_recvq_tq_list_cur_sz; i++) {
1933f4b3ec61Sdh 		ASSERT(sctps->sctps_recvq_tq_list[i] != NULL);
1934f4b3ec61Sdh 		taskq_destroy(sctps->sctps_recvq_tq_list[i]);
19357c478bd9Sstevel@tonic-gate 	}
1936f4b3ec61Sdh 	kmem_free(sctps->sctps_recvq_tq_list,
1937f4b3ec61Sdh 	    sctps->sctps_recvq_tq_list_max_sz * sizeof (taskq_t *));
1938f4b3ec61Sdh 	sctps->sctps_recvq_tq_list = NULL;
19397c478bd9Sstevel@tonic-gate }
19407c478bd9Sstevel@tonic-gate 
19417c478bd9Sstevel@tonic-gate /* Add another taskq for a new ill. */
19427c478bd9Sstevel@tonic-gate void
1943f4b3ec61Sdh sctp_inc_taskq(sctp_stack_t *sctps)
19447c478bd9Sstevel@tonic-gate {
19457c478bd9Sstevel@tonic-gate 	taskq_t *tq;
19467c478bd9Sstevel@tonic-gate 	char tq_name[TASKQ_NAMELEN];
19477c478bd9Sstevel@tonic-gate 
1948f4b3ec61Sdh 	mutex_enter(&sctps->sctps_rq_tq_lock);
1949f4b3ec61Sdh 	if (sctps->sctps_recvq_tq_list_cur_sz + 1 >
1950f4b3ec61Sdh 	    sctps->sctps_recvq_tq_list_max_sz) {
1951f4b3ec61Sdh 		mutex_exit(&sctps->sctps_rq_tq_lock);
19527c478bd9Sstevel@tonic-gate 		cmn_err(CE_NOTE, "Cannot create more SCTP recvq taskq");
19537c478bd9Sstevel@tonic-gate 		return;
19547c478bd9Sstevel@tonic-gate 	}
19557c478bd9Sstevel@tonic-gate 
19567c478bd9Sstevel@tonic-gate 	(void) snprintf(tq_name, sizeof (tq_name), "sctp_recvq_taskq_%u",
1957f4b3ec61Sdh 	    sctps->sctps_recvq_tq_list_cur_sz);
19587c478bd9Sstevel@tonic-gate 	tq = taskq_create(tq_name,
19597c478bd9Sstevel@tonic-gate 	    MIN(sctp_recvq_tq_thr_max, MAX(sctp_recvq_tq_thr_min, ncpus)),
19607c478bd9Sstevel@tonic-gate 	    minclsyspri, sctp_recvq_tq_task_min, sctp_recvq_tq_task_max,
19617c478bd9Sstevel@tonic-gate 	    TASKQ_PREPOPULATE);
19627c478bd9Sstevel@tonic-gate 	if (tq == NULL) {
1963f4b3ec61Sdh 		mutex_exit(&sctps->sctps_rq_tq_lock);
19647c478bd9Sstevel@tonic-gate 		cmn_err(CE_NOTE, "SCTP recvq taskq creation failed");
19657c478bd9Sstevel@tonic-gate 		return;
19667c478bd9Sstevel@tonic-gate 	}
1967f4b3ec61Sdh 	ASSERT(sctps->sctps_recvq_tq_list[
1968f4b3ec61Sdh 		    sctps->sctps_recvq_tq_list_cur_sz] == NULL);
1969f4b3ec61Sdh 	sctps->sctps_recvq_tq_list[sctps->sctps_recvq_tq_list_cur_sz] = tq;
1970f4b3ec61Sdh 	atomic_add_32(&sctps->sctps_recvq_tq_list_cur_sz, 1);
1971f4b3ec61Sdh 	mutex_exit(&sctps->sctps_rq_tq_lock);
19727c478bd9Sstevel@tonic-gate }
19737c478bd9Sstevel@tonic-gate 
19747c478bd9Sstevel@tonic-gate #ifdef DEBUG
19757c478bd9Sstevel@tonic-gate uint32_t sendq_loop_cnt = 0;
19767c478bd9Sstevel@tonic-gate uint32_t sendq_collision = 0;
19777c478bd9Sstevel@tonic-gate uint32_t sendq_empty = 0;
19787c478bd9Sstevel@tonic-gate #endif
19797c478bd9Sstevel@tonic-gate 
19807c478bd9Sstevel@tonic-gate void
19817c478bd9Sstevel@tonic-gate sctp_add_sendq(sctp_t *sctp, mblk_t *mp)
19827c478bd9Sstevel@tonic-gate {
19837c478bd9Sstevel@tonic-gate 	mutex_enter(&sctp->sctp_sendq_lock);
19847c478bd9Sstevel@tonic-gate 	if (sctp->sctp_sendq == NULL) {
19857c478bd9Sstevel@tonic-gate 		sctp->sctp_sendq = mp;
19867c478bd9Sstevel@tonic-gate 		sctp->sctp_sendq_tail = mp;
19877c478bd9Sstevel@tonic-gate 	} else {
19887c478bd9Sstevel@tonic-gate 		sctp->sctp_sendq_tail->b_next = mp;
19897c478bd9Sstevel@tonic-gate 		sctp->sctp_sendq_tail = mp;
19907c478bd9Sstevel@tonic-gate 	}
19917c478bd9Sstevel@tonic-gate 	mutex_exit(&sctp->sctp_sendq_lock);
19927c478bd9Sstevel@tonic-gate }
19937c478bd9Sstevel@tonic-gate 
19947c478bd9Sstevel@tonic-gate void
19957c478bd9Sstevel@tonic-gate sctp_process_sendq(sctp_t *sctp)
19967c478bd9Sstevel@tonic-gate {
19977c478bd9Sstevel@tonic-gate 	mblk_t *mp;
19987c478bd9Sstevel@tonic-gate #ifdef DEBUG
19997c478bd9Sstevel@tonic-gate 	uint32_t loop_cnt = 0;
20007c478bd9Sstevel@tonic-gate #endif
20017c478bd9Sstevel@tonic-gate 
20027c478bd9Sstevel@tonic-gate 	mutex_enter(&sctp->sctp_sendq_lock);
20037c478bd9Sstevel@tonic-gate 	if (sctp->sctp_sendq == NULL || sctp->sctp_sendq_sending) {
20047c478bd9Sstevel@tonic-gate #ifdef DEBUG
20057c478bd9Sstevel@tonic-gate 		if (sctp->sctp_sendq == NULL)
20067c478bd9Sstevel@tonic-gate 			sendq_empty++;
20077c478bd9Sstevel@tonic-gate 		else
20087c478bd9Sstevel@tonic-gate 			sendq_collision++;
20097c478bd9Sstevel@tonic-gate #endif
20107c478bd9Sstevel@tonic-gate 		mutex_exit(&sctp->sctp_sendq_lock);
20117c478bd9Sstevel@tonic-gate 		return;
20127c478bd9Sstevel@tonic-gate 	}
20137c478bd9Sstevel@tonic-gate 	sctp->sctp_sendq_sending = B_TRUE;
20147c478bd9Sstevel@tonic-gate 
20157c478bd9Sstevel@tonic-gate 	/*
20167c478bd9Sstevel@tonic-gate 	 * Note that while we are in this loop, other thread can put
20177c478bd9Sstevel@tonic-gate 	 * new packets in the receive queue.  We may be looping for
20187c478bd9Sstevel@tonic-gate 	 * quite a while.  This is OK even for an interrupt thread.
20197c478bd9Sstevel@tonic-gate 	 * The reason is that SCTP should only able to send a limited
20207c478bd9Sstevel@tonic-gate 	 * number of packets out in a burst.  So the number of times
20217c478bd9Sstevel@tonic-gate 	 * we go through this loop should not be many.
20227c478bd9Sstevel@tonic-gate 	 */
20237c478bd9Sstevel@tonic-gate 	while ((mp = sctp->sctp_sendq) != NULL) {
20247c478bd9Sstevel@tonic-gate 		sctp->sctp_sendq = mp->b_next;
20257c478bd9Sstevel@tonic-gate 		ASSERT(sctp->sctp_connp->conn_ref > 0);
20267c478bd9Sstevel@tonic-gate 		mutex_exit(&sctp->sctp_sendq_lock);
20277c478bd9Sstevel@tonic-gate 		mp->b_next = NULL;
20287c478bd9Sstevel@tonic-gate 		CONN_INC_REF(sctp->sctp_connp);
20297c478bd9Sstevel@tonic-gate 		mp->b_flag |= MSGHASREF;
20307c478bd9Sstevel@tonic-gate 		/* If we don't have sctp_current, default to IPv4 */
20317c478bd9Sstevel@tonic-gate 		IP_PUT(mp, sctp->sctp_connp, sctp->sctp_current == NULL ?
20327c478bd9Sstevel@tonic-gate 		    B_TRUE : sctp->sctp_current->isv4);
20337c478bd9Sstevel@tonic-gate 		BUMP_LOCAL(sctp->sctp_opkts);
20347c478bd9Sstevel@tonic-gate #ifdef DEBUG
20357c478bd9Sstevel@tonic-gate 		loop_cnt++;
20367c478bd9Sstevel@tonic-gate #endif
20377c478bd9Sstevel@tonic-gate 		mutex_enter(&sctp->sctp_sendq_lock);
20387c478bd9Sstevel@tonic-gate 	}
20397c478bd9Sstevel@tonic-gate 
20407c478bd9Sstevel@tonic-gate 	sctp->sctp_sendq_tail = NULL;
20417c478bd9Sstevel@tonic-gate 	sctp->sctp_sendq_sending = B_FALSE;
20427c478bd9Sstevel@tonic-gate #ifdef DEBUG
20437c478bd9Sstevel@tonic-gate 	if (loop_cnt > sendq_loop_cnt)
20447c478bd9Sstevel@tonic-gate 		sendq_loop_cnt = loop_cnt;
20457c478bd9Sstevel@tonic-gate #endif
20467c478bd9Sstevel@tonic-gate 	mutex_exit(&sctp->sctp_sendq_lock);
20477c478bd9Sstevel@tonic-gate }
20487c478bd9Sstevel@tonic-gate 
20497c478bd9Sstevel@tonic-gate #ifdef DEBUG
20507c478bd9Sstevel@tonic-gate uint32_t recvq_loop_cnt = 0;
20517c478bd9Sstevel@tonic-gate uint32_t recvq_call = 0;
20527c478bd9Sstevel@tonic-gate #endif
20537c478bd9Sstevel@tonic-gate 
20547c478bd9Sstevel@tonic-gate /*
20557c478bd9Sstevel@tonic-gate  * Find the next recvq_tq to use.  This routine will go thru all the
20567c478bd9Sstevel@tonic-gate  * taskqs until it can dispatch a job for the sctp.  If this fails,
20577c478bd9Sstevel@tonic-gate  * it will create a new taskq and try it.
20587c478bd9Sstevel@tonic-gate  */
20597c478bd9Sstevel@tonic-gate static boolean_t
20607c478bd9Sstevel@tonic-gate sctp_find_next_tq(sctp_t *sctp)
20617c478bd9Sstevel@tonic-gate {
20627c478bd9Sstevel@tonic-gate 	int next_tq, try;
20637c478bd9Sstevel@tonic-gate 	taskq_t *tq;
2064f4b3ec61Sdh 	sctp_stack_t	*sctps = sctp->sctp_sctps;
20657c478bd9Sstevel@tonic-gate 
20667c478bd9Sstevel@tonic-gate 	/*
20677c478bd9Sstevel@tonic-gate 	 * Note that since we don't hold a lock on sctp_rq_tq_lock for
20687c478bd9Sstevel@tonic-gate 	 * performance reason, recvq_ta_list_cur_sz can be changed during
20697c478bd9Sstevel@tonic-gate 	 * this loop.  The problem this will create is that the loop may
20707c478bd9Sstevel@tonic-gate 	 * not have tried all the recvq_tq.  This should be OK.
20717c478bd9Sstevel@tonic-gate 	 */
2072f4b3ec61Sdh 	next_tq = atomic_add_32_nv(&sctps->sctps_recvq_tq_list_cur, 1) %
2073f4b3ec61Sdh 	    sctps->sctps_recvq_tq_list_cur_sz;
2074f4b3ec61Sdh 	for (try = 0; try < sctps->sctps_recvq_tq_list_cur_sz; try++) {
2075f4b3ec61Sdh 		tq = sctps->sctps_recvq_tq_list[next_tq];
20767c478bd9Sstevel@tonic-gate 		if (taskq_dispatch(tq, sctp_process_recvq, sctp,
20777c478bd9Sstevel@tonic-gate 		    TQ_NOSLEEP) != NULL) {
20787c478bd9Sstevel@tonic-gate 			sctp->sctp_recvq_tq = tq;
20797c478bd9Sstevel@tonic-gate 			return (B_TRUE);
20807c478bd9Sstevel@tonic-gate 		}
2081f4b3ec61Sdh 		next_tq = (next_tq + 1) % sctps->sctps_recvq_tq_list_cur_sz;
20827c478bd9Sstevel@tonic-gate 	}
20837c478bd9Sstevel@tonic-gate 
20847c478bd9Sstevel@tonic-gate 	/*
20857c478bd9Sstevel@tonic-gate 	 * Create one more taskq and try it.  Note that sctp_inc_taskq()
20867c478bd9Sstevel@tonic-gate 	 * may not have created another taskq if the number of recvq
20877c478bd9Sstevel@tonic-gate 	 * taskqs is at the maximum.  We are probably in a pretty bad
20887c478bd9Sstevel@tonic-gate 	 * shape if this actually happens...
20897c478bd9Sstevel@tonic-gate 	 */
2090f4b3ec61Sdh 	sctp_inc_taskq(sctps);
2091f4b3ec61Sdh 	tq = sctps->sctps_recvq_tq_list[sctps->sctps_recvq_tq_list_cur_sz - 1];
20927c478bd9Sstevel@tonic-gate 	if (taskq_dispatch(tq, sctp_process_recvq, sctp, TQ_NOSLEEP) != NULL) {
20937c478bd9Sstevel@tonic-gate 		sctp->sctp_recvq_tq = tq;
20947c478bd9Sstevel@tonic-gate 		return (B_TRUE);
20957c478bd9Sstevel@tonic-gate 	}
2096f4b3ec61Sdh 	SCTP_KSTAT(sctps, sctp_find_next_tq);
20977c478bd9Sstevel@tonic-gate 	return (B_FALSE);
20987c478bd9Sstevel@tonic-gate }
20997c478bd9Sstevel@tonic-gate 
21007c478bd9Sstevel@tonic-gate /*
21017c478bd9Sstevel@tonic-gate  * To add a message to the recvq.  Note that the sctp_timer_fire()
21027c478bd9Sstevel@tonic-gate  * routine also uses this function to add the timer message to the
21037c478bd9Sstevel@tonic-gate  * receive queue for later processing.  And it should be the only
21047c478bd9Sstevel@tonic-gate  * caller of sctp_add_recvq() which sets the try_harder argument
21057c478bd9Sstevel@tonic-gate  * to B_TRUE.
21067c478bd9Sstevel@tonic-gate  *
21077c478bd9Sstevel@tonic-gate  * If the try_harder argument is B_TRUE, this routine sctp_find_next_tq()
21087c478bd9Sstevel@tonic-gate  * will try very hard to dispatch the task.  Refer to the comment
21097c478bd9Sstevel@tonic-gate  * for that routine on how it does that.
21107c478bd9Sstevel@tonic-gate  */
21117c478bd9Sstevel@tonic-gate boolean_t
21127c478bd9Sstevel@tonic-gate sctp_add_recvq(sctp_t *sctp, mblk_t *mp, boolean_t caller_hold_lock)
21137c478bd9Sstevel@tonic-gate {
21147c478bd9Sstevel@tonic-gate 	if (!caller_hold_lock)
21157c478bd9Sstevel@tonic-gate 		mutex_enter(&sctp->sctp_recvq_lock);
21167c478bd9Sstevel@tonic-gate 
21177c478bd9Sstevel@tonic-gate 	/* If the taskq dispatch has not been scheduled, do it now. */
21187c478bd9Sstevel@tonic-gate 	if (sctp->sctp_recvq_tq == NULL) {
21197c478bd9Sstevel@tonic-gate 		ASSERT(sctp->sctp_recvq == NULL);
21207c478bd9Sstevel@tonic-gate 		if (!sctp_find_next_tq(sctp)) {
21217c478bd9Sstevel@tonic-gate 			if (!caller_hold_lock)
21227c478bd9Sstevel@tonic-gate 				mutex_exit(&sctp->sctp_recvq_lock);
21237c478bd9Sstevel@tonic-gate 			return (B_FALSE);
21247c478bd9Sstevel@tonic-gate 		}
21257c478bd9Sstevel@tonic-gate 		/* Make sure the sctp_t will not go away. */
21267c478bd9Sstevel@tonic-gate 		SCTP_REFHOLD(sctp);
21277c478bd9Sstevel@tonic-gate 	}
21287c478bd9Sstevel@tonic-gate 
21297c478bd9Sstevel@tonic-gate 	if (sctp->sctp_recvq == NULL) {
21307c478bd9Sstevel@tonic-gate 		sctp->sctp_recvq = mp;
21317c478bd9Sstevel@tonic-gate 		sctp->sctp_recvq_tail = mp;
21327c478bd9Sstevel@tonic-gate 	} else {
21337c478bd9Sstevel@tonic-gate 		sctp->sctp_recvq_tail->b_next = mp;
21347c478bd9Sstevel@tonic-gate 		sctp->sctp_recvq_tail = mp;
21357c478bd9Sstevel@tonic-gate 	}
21367c478bd9Sstevel@tonic-gate 
21377c478bd9Sstevel@tonic-gate 	if (!caller_hold_lock)
21387c478bd9Sstevel@tonic-gate 		mutex_exit(&sctp->sctp_recvq_lock);
21397c478bd9Sstevel@tonic-gate 	return (B_TRUE);
21407c478bd9Sstevel@tonic-gate }
21417c478bd9Sstevel@tonic-gate 
21427c478bd9Sstevel@tonic-gate static void
21437c478bd9Sstevel@tonic-gate sctp_process_recvq(void *arg)
21447c478bd9Sstevel@tonic-gate {
21457c478bd9Sstevel@tonic-gate 	sctp_t		*sctp = (sctp_t *)arg;
21467c478bd9Sstevel@tonic-gate 	mblk_t		*mp;
21477c478bd9Sstevel@tonic-gate 	mblk_t		*ipsec_mp;
21487c478bd9Sstevel@tonic-gate #ifdef DEBUG
21497c478bd9Sstevel@tonic-gate 	uint32_t	loop_cnt = 0;
21507c478bd9Sstevel@tonic-gate #endif
21517c478bd9Sstevel@tonic-gate 
21527c478bd9Sstevel@tonic-gate #ifdef	_BIG_ENDIAN
21537c478bd9Sstevel@tonic-gate #define	IPVER(ip6h)	((((uint32_t *)ip6h)[0] >> 28) & 0x7)
21547c478bd9Sstevel@tonic-gate #else
21557c478bd9Sstevel@tonic-gate #define	IPVER(ip6h)	((((uint32_t *)ip6h)[0] >> 4) & 0x7)
21567c478bd9Sstevel@tonic-gate #endif
21577c478bd9Sstevel@tonic-gate 
21587c478bd9Sstevel@tonic-gate 	RUN_SCTP(sctp);
21597c478bd9Sstevel@tonic-gate 	mutex_enter(&sctp->sctp_recvq_lock);
21607c478bd9Sstevel@tonic-gate 
21617c478bd9Sstevel@tonic-gate #ifdef DEBUG
21627c478bd9Sstevel@tonic-gate 	recvq_call++;
21637c478bd9Sstevel@tonic-gate #endif
21647c478bd9Sstevel@tonic-gate 	/*
21657c478bd9Sstevel@tonic-gate 	 * Note that while we are in this loop, other thread can put
21667c478bd9Sstevel@tonic-gate 	 * new packets in the receive queue.  We may be looping for
21677c478bd9Sstevel@tonic-gate 	 * quite a while.
21687c478bd9Sstevel@tonic-gate 	 */
21697c478bd9Sstevel@tonic-gate 	while ((mp = sctp->sctp_recvq) != NULL) {
21707c478bd9Sstevel@tonic-gate 		sctp->sctp_recvq = mp->b_next;
21717c478bd9Sstevel@tonic-gate 		mutex_exit(&sctp->sctp_recvq_lock);
21727c478bd9Sstevel@tonic-gate 		mp->b_next = NULL;
21737c478bd9Sstevel@tonic-gate #ifdef DEBUG
21747c478bd9Sstevel@tonic-gate 		loop_cnt++;
21757c478bd9Sstevel@tonic-gate #endif
21767c478bd9Sstevel@tonic-gate 		ipsec_mp = mp->b_prev;
21777c478bd9Sstevel@tonic-gate 		mp->b_prev = NULL;
21787c478bd9Sstevel@tonic-gate 		sctp_input_data(sctp, mp, ipsec_mp);
21797c478bd9Sstevel@tonic-gate 
21807c478bd9Sstevel@tonic-gate 		mutex_enter(&sctp->sctp_recvq_lock);
21817c478bd9Sstevel@tonic-gate 	}
21827c478bd9Sstevel@tonic-gate 
21837c478bd9Sstevel@tonic-gate 	sctp->sctp_recvq_tail = NULL;
21847c478bd9Sstevel@tonic-gate 	sctp->sctp_recvq_tq = NULL;
21857c478bd9Sstevel@tonic-gate 
21867c478bd9Sstevel@tonic-gate 	mutex_exit(&sctp->sctp_recvq_lock);
21877c478bd9Sstevel@tonic-gate 
21887c478bd9Sstevel@tonic-gate 	WAKE_SCTP(sctp);
21897c478bd9Sstevel@tonic-gate 
21907c478bd9Sstevel@tonic-gate 	/* We may have sent something when processing the receive queue. */
21917c478bd9Sstevel@tonic-gate 	sctp_process_sendq(sctp);
21927c478bd9Sstevel@tonic-gate #ifdef DEBUG
21937c478bd9Sstevel@tonic-gate 	if (loop_cnt > recvq_loop_cnt)
21947c478bd9Sstevel@tonic-gate 		recvq_loop_cnt = loop_cnt;
21957c478bd9Sstevel@tonic-gate #endif
21967c478bd9Sstevel@tonic-gate 	/* Now it can go away. */
21977c478bd9Sstevel@tonic-gate 	SCTP_REFRELE(sctp);
21987c478bd9Sstevel@tonic-gate }
21997c478bd9Sstevel@tonic-gate 
22007c478bd9Sstevel@tonic-gate /* ARGSUSED */
22017c478bd9Sstevel@tonic-gate static int
22027c478bd9Sstevel@tonic-gate sctp_conn_cache_constructor(void *buf, void *cdrarg, int kmflags)
22037c478bd9Sstevel@tonic-gate {
22047c478bd9Sstevel@tonic-gate 	conn_t	*sctp_connp = (conn_t *)buf;
22057c478bd9Sstevel@tonic-gate 	sctp_t	*sctp = (sctp_t *)&sctp_connp[1];
22067c478bd9Sstevel@tonic-gate 
22077c478bd9Sstevel@tonic-gate 	bzero(buf, (char *)&sctp[1] - (char *)buf);
22087c478bd9Sstevel@tonic-gate 
22097c478bd9Sstevel@tonic-gate 	sctp->sctp_connp = sctp_connp;
22107c478bd9Sstevel@tonic-gate 	mutex_init(&sctp->sctp_reflock, NULL, MUTEX_DEFAULT, NULL);
22117c478bd9Sstevel@tonic-gate 	mutex_init(&sctp->sctp_lock, NULL, MUTEX_DEFAULT, NULL);
22127c478bd9Sstevel@tonic-gate 	mutex_init(&sctp->sctp_recvq_lock, NULL, MUTEX_DEFAULT, NULL);
22137c478bd9Sstevel@tonic-gate 	cv_init(&sctp->sctp_cv, NULL, CV_DEFAULT, NULL);
22147c478bd9Sstevel@tonic-gate 	mutex_init(&sctp->sctp_sendq_lock, NULL, MUTEX_DEFAULT, NULL);
22157c478bd9Sstevel@tonic-gate 
22167c478bd9Sstevel@tonic-gate 	sctp_connp->conn_rq = sctp_connp->conn_wq = NULL;
22177c478bd9Sstevel@tonic-gate 	sctp_connp->conn_multicast_loop = IP_DEFAULT_MULTICAST_LOOP;
22187c478bd9Sstevel@tonic-gate 	sctp_connp->conn_ulp = IPPROTO_SCTP;
22197c478bd9Sstevel@tonic-gate 	mutex_init(&sctp_connp->conn_lock, NULL, MUTEX_DEFAULT, NULL);
22207c478bd9Sstevel@tonic-gate 	cv_init(&sctp_connp->conn_cv, NULL, CV_DEFAULT, NULL);
22217c478bd9Sstevel@tonic-gate 
22227c478bd9Sstevel@tonic-gate 	return (0);
22237c478bd9Sstevel@tonic-gate }
22247c478bd9Sstevel@tonic-gate 
22257c478bd9Sstevel@tonic-gate /* ARGSUSED */
22267c478bd9Sstevel@tonic-gate static void
22277c478bd9Sstevel@tonic-gate sctp_conn_cache_destructor(void *buf, void *cdrarg)
22287c478bd9Sstevel@tonic-gate {
22297c478bd9Sstevel@tonic-gate 	conn_t	*sctp_connp = (conn_t *)buf;
22307c478bd9Sstevel@tonic-gate 	sctp_t	*sctp = (sctp_t *)&sctp_connp[1];
22317c478bd9Sstevel@tonic-gate 
22327c478bd9Sstevel@tonic-gate 	ASSERT(!MUTEX_HELD(&sctp->sctp_lock));
22337c478bd9Sstevel@tonic-gate 	ASSERT(!MUTEX_HELD(&sctp->sctp_reflock));
22347c478bd9Sstevel@tonic-gate 	ASSERT(!MUTEX_HELD(&sctp->sctp_recvq_lock));
22357c478bd9Sstevel@tonic-gate 	ASSERT(!MUTEX_HELD(&sctp->sctp_sendq_lock));
22367c478bd9Sstevel@tonic-gate 	ASSERT(!MUTEX_HELD(&sctp->sctp_connp->conn_lock));
22377c478bd9Sstevel@tonic-gate 
22387c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_conn_hash_next == NULL);
22397c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_conn_hash_prev == NULL);
22407c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_listen_hash_next == NULL);
22417c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_listen_hash_prev == NULL);
22427c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_listen_tfp == NULL);
22437c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_conn_tfp == NULL);
22447c478bd9Sstevel@tonic-gate 
22457c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_faddrs == NULL);
22467c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_nsaddrs == 0);
22477c478bd9Sstevel@tonic-gate 
22487c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_ulpd == NULL);
22497c478bd9Sstevel@tonic-gate 
22507c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_lastfaddr == NULL);
22517c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_primary == NULL);
22527c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_current == NULL);
22537c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_lastdata == NULL);
22547c478bd9Sstevel@tonic-gate 
22557c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_xmit_head == NULL);
22567c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_xmit_tail == NULL);
22577c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_xmit_unsent == NULL);
22587c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_xmit_unsent_tail == NULL);
22597c478bd9Sstevel@tonic-gate 
22607c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_ostrcntrs == NULL);
22617c478bd9Sstevel@tonic-gate 
22627c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_sack_info == NULL);
22637c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_ack_mp == NULL);
22647c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_instr == NULL);
22657c478bd9Sstevel@tonic-gate 
22667c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_iphc == NULL);
22677c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_iphc6 == NULL);
22687c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_ipha == NULL);
22697c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_ip6h == NULL);
22707c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_sctph == NULL);
22717c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_sctph6 == NULL);
22727c478bd9Sstevel@tonic-gate 
22737c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_cookie_mp == NULL);
22747c478bd9Sstevel@tonic-gate 
22757c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_refcnt == 0);
22767c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_timer_mp == NULL);
22777c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_connp->conn_ref == 0);
22787c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_heartbeat_mp == NULL);
22797c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_ptpbhn == NULL && sctp->sctp_bind_hash == NULL);
22807c478bd9Sstevel@tonic-gate 
22817c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_shutdown_faddr == NULL);
22827c478bd9Sstevel@tonic-gate 
22837c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_cxmit_list == NULL);
22847c478bd9Sstevel@tonic-gate 
22857c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_recvq == NULL);
22867c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_recvq_tail == NULL);
22877c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_recvq_tq == NULL);
22887c478bd9Sstevel@tonic-gate 
22897c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_sendq == NULL);
22907c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_sendq_tail == NULL);
22917c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_sendq_sending == B_FALSE);
22927c478bd9Sstevel@tonic-gate 
22937f99e6fcSkp 	ASSERT(sctp->sctp_ipp_hopopts == NULL);
22947f99e6fcSkp 	ASSERT(sctp->sctp_ipp_rtdstopts == NULL);
22957f99e6fcSkp 	ASSERT(sctp->sctp_ipp_rthdr == NULL);
22967f99e6fcSkp 	ASSERT(sctp->sctp_ipp_dstopts == NULL);
22977f99e6fcSkp 	ASSERT(sctp->sctp_ipp_pathmtu == NULL);
22987f99e6fcSkp 
22997c478bd9Sstevel@tonic-gate 	mutex_destroy(&sctp->sctp_reflock);
23007c478bd9Sstevel@tonic-gate 	mutex_destroy(&sctp->sctp_lock);
23017c478bd9Sstevel@tonic-gate 	mutex_destroy(&sctp->sctp_recvq_lock);
23027c478bd9Sstevel@tonic-gate 	cv_destroy(&sctp->sctp_cv);
23037c478bd9Sstevel@tonic-gate 	mutex_destroy(&sctp->sctp_sendq_lock);
23047c478bd9Sstevel@tonic-gate 
23057c478bd9Sstevel@tonic-gate 	mutex_destroy(&sctp_connp->conn_lock);
23067c478bd9Sstevel@tonic-gate 	cv_destroy(&sctp_connp->conn_cv);
23077c478bd9Sstevel@tonic-gate }
23087c478bd9Sstevel@tonic-gate 
23097c478bd9Sstevel@tonic-gate static void
23107c478bd9Sstevel@tonic-gate sctp_conn_cache_init()
23117c478bd9Sstevel@tonic-gate {
23127c478bd9Sstevel@tonic-gate 	sctp_conn_cache = kmem_cache_create("sctp_conn_cache",
23137c478bd9Sstevel@tonic-gate 	    sizeof (sctp_t) + sizeof (conn_t), 0, sctp_conn_cache_constructor,
23147c478bd9Sstevel@tonic-gate 	    sctp_conn_cache_destructor, NULL, NULL, NULL, 0);
23157c478bd9Sstevel@tonic-gate }
23167c478bd9Sstevel@tonic-gate 
23177c478bd9Sstevel@tonic-gate static void
23187c478bd9Sstevel@tonic-gate sctp_conn_cache_fini()
23197c478bd9Sstevel@tonic-gate {
23207c478bd9Sstevel@tonic-gate 	kmem_cache_destroy(sctp_conn_cache);
23217c478bd9Sstevel@tonic-gate }
2322