xref: /illumos-gate/usr/src/uts/common/inet/sctp/sctp.c (revision fc8ae2ec)
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 /*
23fd7b5aedSGeorge Shepherd  * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #include <sys/types.h>
277c478bd9Sstevel@tonic-gate #include <sys/stream.h>
287c478bd9Sstevel@tonic-gate #include <sys/strsubr.h>
297c478bd9Sstevel@tonic-gate #include <sys/stropts.h>
307c478bd9Sstevel@tonic-gate #include <sys/strsun.h>
317c478bd9Sstevel@tonic-gate #define	_SUN_TPI_VERSION 2
327c478bd9Sstevel@tonic-gate #include <sys/tihdr.h>
337c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
347c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
357c478bd9Sstevel@tonic-gate #include <sys/xti_inet.h>
367c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
377c478bd9Sstevel@tonic-gate #include <sys/debug.h>
387c478bd9Sstevel@tonic-gate #include <sys/vtrace.h>
397c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
407c478bd9Sstevel@tonic-gate #include <sys/cpuvar.h>
417c478bd9Sstevel@tonic-gate #include <sys/random.h>
4245916cd2Sjpk #include <sys/priv.h>
43f4b3ec61Sdh #include <sys/sunldi.h>
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate #include <sys/errno.h>
467c478bd9Sstevel@tonic-gate #include <sys/signal.h>
477c478bd9Sstevel@tonic-gate #include <sys/socket.h>
487c478bd9Sstevel@tonic-gate #include <sys/isa_defs.h>
497c478bd9Sstevel@tonic-gate #include <netinet/in.h>
507c478bd9Sstevel@tonic-gate #include <netinet/tcp.h>
517c478bd9Sstevel@tonic-gate #include <netinet/ip6.h>
527c478bd9Sstevel@tonic-gate #include <netinet/icmp6.h>
537c478bd9Sstevel@tonic-gate #include <netinet/sctp.h>
547c478bd9Sstevel@tonic-gate #include <net/if.h>
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate #include <inet/common.h>
577c478bd9Sstevel@tonic-gate #include <inet/ip.h>
58bd670b35SErik Nordmark #include <inet/ip_if.h>
59bd670b35SErik Nordmark #include <inet/ip_ire.h>
607c478bd9Sstevel@tonic-gate #include <inet/ip6.h>
617c478bd9Sstevel@tonic-gate #include <inet/mi.h>
627c478bd9Sstevel@tonic-gate #include <inet/mib2.h>
63f4b3ec61Sdh #include <inet/kstatcom.h>
647c478bd9Sstevel@tonic-gate #include <inet/optcom.h>
657c478bd9Sstevel@tonic-gate #include <inet/ipclassifier.h>
667c478bd9Sstevel@tonic-gate #include <inet/ipsec_impl.h>
677c478bd9Sstevel@tonic-gate #include <inet/sctp_ip.h>
687c478bd9Sstevel@tonic-gate #include <inet/sctp_crc32.h>
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate #include "sctp_impl.h"
717c478bd9Sstevel@tonic-gate #include "sctp_addr.h"
721d8c4025Svi #include "sctp_asconf.h"
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate int sctpdebug;
757c478bd9Sstevel@tonic-gate sin6_t	sctp_sin6_null;	/* Zero address for quick clears */
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate static void	sctp_closei_local(sctp_t *sctp);
787c478bd9Sstevel@tonic-gate static int	sctp_init_values(sctp_t *, sctp_t *, int);
797c478bd9Sstevel@tonic-gate static void	sctp_icmp_error_ipv6(sctp_t *sctp, mblk_t *mp);
807c478bd9Sstevel@tonic-gate static void	sctp_process_recvq(void *);
81f4b3ec61Sdh static void	sctp_rq_tq_init(sctp_stack_t *);
82f4b3ec61Sdh static void	sctp_rq_tq_fini(sctp_stack_t *);
837c478bd9Sstevel@tonic-gate static void	sctp_conn_cache_init();
847c478bd9Sstevel@tonic-gate static void	sctp_conn_cache_fini();
857c478bd9Sstevel@tonic-gate static int	sctp_conn_cache_constructor();
867c478bd9Sstevel@tonic-gate static void	sctp_conn_cache_destructor();
87121e5416Skcpoon static void	sctp_conn_clear(conn_t *);
88bd670b35SErik Nordmark static void	sctp_notify(void *, ip_xmit_attr_t *, ixa_notify_type_t,
89bd670b35SErik Nordmark     ixa_notify_arg_t);
90f4b3ec61Sdh 
91f4b3ec61Sdh static void	*sctp_stack_init(netstackid_t stackid, netstack_t *ns);
92f4b3ec61Sdh static void	sctp_stack_fini(netstackid_t stackid, void *arg);
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate /*
957c478bd9Sstevel@tonic-gate  * SCTP receive queue taskq
967c478bd9Sstevel@tonic-gate  *
977c478bd9Sstevel@tonic-gate  * At SCTP initialization time, a default taskq is created for
987c478bd9Sstevel@tonic-gate  * servicing packets received when the interrupt thread cannot
997c478bd9Sstevel@tonic-gate  * get a hold on the sctp_t.  The number of taskq can be increased in
1007c478bd9Sstevel@tonic-gate  * sctp_find_next_tq() when an existing taskq cannot be dispatched.
1017c478bd9Sstevel@tonic-gate  * The taskqs are never removed.  But the max number of taskq which
1027c478bd9Sstevel@tonic-gate  * can be created is controlled by sctp_recvq_tq_list_max_sz.  Note
1037c478bd9Sstevel@tonic-gate  * that SCTP recvq taskq is not tied to any specific CPU or ill.
1047c478bd9Sstevel@tonic-gate  *
1057c478bd9Sstevel@tonic-gate  * Those taskqs are stored in an array recvq_tq_list.  And they are
1067c478bd9Sstevel@tonic-gate  * used in a round robin fashion.  The current taskq being used is
1077c478bd9Sstevel@tonic-gate  * determined by recvq_tq_list_cur.
1087c478bd9Sstevel@tonic-gate  */
1097c478bd9Sstevel@tonic-gate 
110f4b3ec61Sdh /* /etc/system variables */
1117c478bd9Sstevel@tonic-gate /* The minimum number of threads for each taskq. */
1125dd46ab5SKacheong Poon int sctp_recvq_tq_thr_min = 4;
1137c478bd9Sstevel@tonic-gate /* The maximum number of threads for each taskq. */
1145dd46ab5SKacheong Poon int sctp_recvq_tq_thr_max = 48;
1155dd46ab5SKacheong Poon /* The mnimum number of tasks for each taskq. */
1165dd46ab5SKacheong Poon int sctp_recvq_tq_task_min = 8;
1175dd46ab5SKacheong Poon /* Default value of sctp_recvq_tq_list_max_sz. */
1185dd46ab5SKacheong Poon int sctp_recvq_tq_list_max = 16;
1197c478bd9Sstevel@tonic-gate 
1206e91bba0SGirish Moodalbail /*
1216e91bba0SGirish Moodalbail  * SCTP tunables related declarations. Definitions are in sctp_tunables.c
1226e91bba0SGirish Moodalbail  */
1236e91bba0SGirish Moodalbail extern mod_prop_info_t sctp_propinfo_tbl[];
1246e91bba0SGirish Moodalbail extern int sctp_propinfo_count;
1256e91bba0SGirish Moodalbail 
1267c478bd9Sstevel@tonic-gate /*  sctp_t/conn_t kmem cache */
1277c478bd9Sstevel@tonic-gate struct kmem_cache	*sctp_conn_cache;
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate #define	SCTP_CONDEMNED(sctp)				\
1307c478bd9Sstevel@tonic-gate 	mutex_enter(&(sctp)->sctp_reflock);		\
1317c478bd9Sstevel@tonic-gate 	((sctp)->sctp_condemned = B_TRUE);		\
1325dddb8baSkcpoon 	mutex_exit(&(sctp)->sctp_reflock);
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate /* Link/unlink a sctp_t to/from the global list. */
135f4b3ec61Sdh #define	SCTP_LINK(sctp, sctps)				\
136f4b3ec61Sdh 	mutex_enter(&(sctps)->sctps_g_lock);		\
137f4b3ec61Sdh 	list_insert_tail(&sctps->sctps_g_list, (sctp));	\
138f4b3ec61Sdh 	mutex_exit(&(sctps)->sctps_g_lock);
1397c478bd9Sstevel@tonic-gate 
140f4b3ec61Sdh #define	SCTP_UNLINK(sctp, sctps)				\
141f4b3ec61Sdh 	mutex_enter(&(sctps)->sctps_g_lock);		\
1427c478bd9Sstevel@tonic-gate 	ASSERT((sctp)->sctp_condemned);			\
143f4b3ec61Sdh 	list_remove(&(sctps)->sctps_g_list, (sctp));	\
144f4b3ec61Sdh 	mutex_exit(&(sctps)->sctps_g_lock);
1457c478bd9Sstevel@tonic-gate 
1461d8c4025Svi /*
1471d8c4025Svi  * Hooks for Sun Cluster. On non-clustered nodes these will remain NULL.
1481d8c4025Svi  * PSARC/2005/602.
1491d8c4025Svi  */
1501d8c4025Svi void (*cl_sctp_listen)(sa_family_t, uchar_t *, uint_t, in_port_t) = NULL;
1511d8c4025Svi void (*cl_sctp_unlisten)(sa_family_t, uchar_t *, uint_t, in_port_t) = NULL;
1521d8c4025Svi void (*cl_sctp_connect)(sa_family_t, uchar_t *, uint_t, in_port_t,
1531d8c4025Svi     uchar_t *, uint_t, in_port_t, boolean_t, cl_sctp_handle_t) = NULL;
1541d8c4025Svi void (*cl_sctp_disconnect)(sa_family_t, cl_sctp_handle_t) = NULL;
1551d8c4025Svi void (*cl_sctp_assoc_change)(sa_family_t, uchar_t *, size_t, uint_t,
1561d8c4025Svi     uchar_t *, size_t, uint_t, int, cl_sctp_handle_t) = NULL;
1571d8c4025Svi void (*cl_sctp_check_addrs)(sa_family_t, in_port_t, uchar_t **, size_t,
1581d8c4025Svi     uint_t *, boolean_t) = NULL;
1597c478bd9Sstevel@tonic-gate /*
1607c478bd9Sstevel@tonic-gate  * Return the version number of the SCTP kernel interface.
1617c478bd9Sstevel@tonic-gate  */
1627c478bd9Sstevel@tonic-gate int
sctp_itf_ver(int cl_ver)1637c478bd9Sstevel@tonic-gate sctp_itf_ver(int cl_ver)
1647c478bd9Sstevel@tonic-gate {
1657c478bd9Sstevel@tonic-gate 	if (cl_ver != SCTP_ITF_VER)
1667c478bd9Sstevel@tonic-gate 		return (-1);
1677c478bd9Sstevel@tonic-gate 	return (SCTP_ITF_VER);
1687c478bd9Sstevel@tonic-gate }
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate /*
1717c478bd9Sstevel@tonic-gate  * Called when we need a new sctp instantiation but don't really have a
1727c478bd9Sstevel@tonic-gate  * new q to hang it off of. Copy the priv flag from the passed in structure.
1737c478bd9Sstevel@tonic-gate  */
1747c478bd9Sstevel@tonic-gate sctp_t *
sctp_create_eager(sctp_t * psctp)1757c478bd9Sstevel@tonic-gate sctp_create_eager(sctp_t *psctp)
1767c478bd9Sstevel@tonic-gate {
1777c478bd9Sstevel@tonic-gate 	sctp_t	*sctp;
1787c478bd9Sstevel@tonic-gate 	mblk_t	*ack_mp, *hb_mp;
179bd670b35SErik Nordmark 	conn_t	*connp;
180bd670b35SErik Nordmark 	cred_t	*credp;
181f4b3ec61Sdh 	sctp_stack_t	*sctps = psctp->sctp_sctps;
1827c478bd9Sstevel@tonic-gate 
183f4b3ec61Sdh 	if ((connp = ipcl_conn_create(IPCL_SCTPCONN, KM_NOSLEEP,
184b34b8d1aSkcpoon 	    sctps->sctps_netstack)) == NULL) {
1857c478bd9Sstevel@tonic-gate 		return (NULL);
186f4b3ec61Sdh 	}
18745916cd2Sjpk 
1887c478bd9Sstevel@tonic-gate 	sctp = CONN2SCTP(connp);
189f4b3ec61Sdh 	sctp->sctp_sctps = sctps;
1907c478bd9Sstevel@tonic-gate 
191121e5416Skcpoon 	if ((ack_mp = sctp_timer_alloc(sctp, sctp_ack_timer,
192121e5416Skcpoon 	    KM_NOSLEEP)) == NULL ||
193121e5416Skcpoon 	    (hb_mp = sctp_timer_alloc(sctp, sctp_heartbeat_timer,
194121e5416Skcpoon 	    KM_NOSLEEP)) == NULL) {
1957c478bd9Sstevel@tonic-gate 		if (ack_mp != NULL)
1967c478bd9Sstevel@tonic-gate 			freeb(ack_mp);
197121e5416Skcpoon 		sctp_conn_clear(connp);
198f4b3ec61Sdh 		sctp->sctp_sctps = NULL;
1997c478bd9Sstevel@tonic-gate 		kmem_cache_free(sctp_conn_cache, connp);
2007c478bd9Sstevel@tonic-gate 		return (NULL);
2017c478bd9Sstevel@tonic-gate 	}
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate 	sctp->sctp_ack_mp = ack_mp;
2047c478bd9Sstevel@tonic-gate 	sctp->sctp_heartbeat_mp = hb_mp;
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate 	if (sctp_init_values(sctp, psctp, KM_NOSLEEP) != 0) {
2077c478bd9Sstevel@tonic-gate 		freeb(ack_mp);
2087c478bd9Sstevel@tonic-gate 		freeb(hb_mp);
209121e5416Skcpoon 		sctp_conn_clear(connp);
210f4b3ec61Sdh 		sctp->sctp_sctps = NULL;
2117c478bd9Sstevel@tonic-gate 		kmem_cache_free(sctp_conn_cache, connp);
2127c478bd9Sstevel@tonic-gate 		return (NULL);
2137c478bd9Sstevel@tonic-gate 	}
21445916cd2Sjpk 
215bd670b35SErik Nordmark 	if ((credp = psctp->sctp_connp->conn_cred) != NULL) {
21645916cd2Sjpk 		connp->conn_cred = credp;
21745916cd2Sjpk 		crhold(credp);
2187c478bd9Sstevel@tonic-gate 	}
2195d0bc3edSsommerfe 
2207c478bd9Sstevel@tonic-gate 	sctp->sctp_mss = psctp->sctp_mss;
2217c478bd9Sstevel@tonic-gate 	sctp->sctp_detached = B_TRUE;
2227c478bd9Sstevel@tonic-gate 	/*
2237c478bd9Sstevel@tonic-gate 	 * Link to the global as soon as possible so that this sctp_t
2247c478bd9Sstevel@tonic-gate 	 * can be found.
2257c478bd9Sstevel@tonic-gate 	 */
226f4b3ec61Sdh 	SCTP_LINK(sctp, sctps);
2277c478bd9Sstevel@tonic-gate 
2285dd46ab5SKacheong Poon 	/* If the listener has a limit, inherit the counter info. */
2295dd46ab5SKacheong Poon 	sctp->sctp_listen_cnt = psctp->sctp_listen_cnt;
2305dd46ab5SKacheong Poon 
2317c478bd9Sstevel@tonic-gate 	return (sctp);
2327c478bd9Sstevel@tonic-gate }
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate /*
2357c478bd9Sstevel@tonic-gate  * We are dying for some reason.  Try to do it gracefully.
2367c478bd9Sstevel@tonic-gate  */
2377c478bd9Sstevel@tonic-gate void
sctp_clean_death(sctp_t * sctp,int err)2387c478bd9Sstevel@tonic-gate sctp_clean_death(sctp_t *sctp, int err)
2397c478bd9Sstevel@tonic-gate {
2407c478bd9Sstevel@tonic-gate 	ASSERT(sctp != NULL);
2417c478bd9Sstevel@tonic-gate 
24245916cd2Sjpk 	dprint(3, ("sctp_clean_death %p, state %d\n", (void *)sctp,
24345916cd2Sjpk 	    sctp->sctp_state));
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate 	sctp->sctp_client_errno = err;
2467c478bd9Sstevel@tonic-gate 	/*
2477c478bd9Sstevel@tonic-gate 	 * Check to see if we need to notify upper layer.
2487c478bd9Sstevel@tonic-gate 	 */
2497c478bd9Sstevel@tonic-gate 	if ((sctp->sctp_state >= SCTPS_COOKIE_WAIT) &&
2507c478bd9Sstevel@tonic-gate 	    !SCTP_IS_DETACHED(sctp)) {
2517c478bd9Sstevel@tonic-gate 		if (sctp->sctp_xmit_head || sctp->sctp_xmit_unsent) {
2527c478bd9Sstevel@tonic-gate 			sctp_regift_xmitlist(sctp);
2537c478bd9Sstevel@tonic-gate 		}
2540f1702c5SYu Xiangning 		if (sctp->sctp_ulp_disconnected(sctp->sctp_ulpd, 0, err)) {
2557c478bd9Sstevel@tonic-gate 			/*
2567c478bd9Sstevel@tonic-gate 			 * Socket is gone, detach.
2577c478bd9Sstevel@tonic-gate 			 */
2587c478bd9Sstevel@tonic-gate 			sctp->sctp_detached = B_TRUE;
2597c478bd9Sstevel@tonic-gate 			sctp->sctp_ulpd = NULL;
2600f1702c5SYu Xiangning 			sctp->sctp_upcalls = NULL;
2617c478bd9Sstevel@tonic-gate 		}
2627c478bd9Sstevel@tonic-gate 	}
2637c478bd9Sstevel@tonic-gate 
2647c478bd9Sstevel@tonic-gate 	/* Remove this sctp from all hashes. */
2657c478bd9Sstevel@tonic-gate 	sctp_closei_local(sctp);
2667c478bd9Sstevel@tonic-gate 
2677c478bd9Sstevel@tonic-gate 	/*
2687c478bd9Sstevel@tonic-gate 	 * If the sctp_t is detached, we need to finish freeing up
2697c478bd9Sstevel@tonic-gate 	 * the resources.  At this point, ip_fanout_sctp() should have
2707c478bd9Sstevel@tonic-gate 	 * a hold on this sctp_t.  Some thread doing snmp stuff can
2717c478bd9Sstevel@tonic-gate 	 * have a hold.  And a taskq can also have a hold waiting to
2727c478bd9Sstevel@tonic-gate 	 * work.  sctp_unlink() the sctp_t from the global list so
2737c478bd9Sstevel@tonic-gate 	 * that no new thread can find it.  Then do a SCTP_REFRELE().
2747c478bd9Sstevel@tonic-gate 	 * The sctp_t will be freed after all those threads are done.
2757c478bd9Sstevel@tonic-gate 	 */
2767c478bd9Sstevel@tonic-gate 	if (SCTP_IS_DETACHED(sctp)) {
2777c478bd9Sstevel@tonic-gate 		SCTP_CONDEMNED(sctp);
2787c478bd9Sstevel@tonic-gate 		SCTP_REFRELE(sctp);
2797c478bd9Sstevel@tonic-gate 	}
2807c478bd9Sstevel@tonic-gate }
2817c478bd9Sstevel@tonic-gate 
2827c478bd9Sstevel@tonic-gate /*
2837c478bd9Sstevel@tonic-gate  * Called by upper layer when it wants to close this association.
2847c478bd9Sstevel@tonic-gate  * Depending on the state of this assoication, we need to do
2857c478bd9Sstevel@tonic-gate  * different things.
2867c478bd9Sstevel@tonic-gate  *
2877c478bd9Sstevel@tonic-gate  * If the state is below COOKIE_ECHOED or it is COOKIE_ECHOED but with
2887c478bd9Sstevel@tonic-gate  * no sent data, just remove this sctp from all the hashes.  This
2897c478bd9Sstevel@tonic-gate  * makes sure that all packets from the other end will go to the default
2907c478bd9Sstevel@tonic-gate  * sctp handling.  The upper layer will then do a sctp_close() to clean
2917c478bd9Sstevel@tonic-gate  * up.
2927c478bd9Sstevel@tonic-gate  *
2937c478bd9Sstevel@tonic-gate  * Otherwise, check and see if SO_LINGER is set.  If it is set, check
2947c478bd9Sstevel@tonic-gate  * the value.  If the value is 0, consider this an abortive close.  Send
2957c478bd9Sstevel@tonic-gate  * an ABORT message and kill the associatiion.
2967c478bd9Sstevel@tonic-gate  *
2977c478bd9Sstevel@tonic-gate  */
2987c478bd9Sstevel@tonic-gate int
sctp_disconnect(sctp_t * sctp)2997c478bd9Sstevel@tonic-gate sctp_disconnect(sctp_t *sctp)
3007c478bd9Sstevel@tonic-gate {
301bd670b35SErik Nordmark 	int		error = 0;
302bd670b35SErik Nordmark 	conn_t		*connp = sctp->sctp_connp;
3037c478bd9Sstevel@tonic-gate 
30445916cd2Sjpk 	dprint(3, ("sctp_disconnect %p, state %d\n", (void *)sctp,
30545916cd2Sjpk 	    sctp->sctp_state));
3067c478bd9Sstevel@tonic-gate 
3077c478bd9Sstevel@tonic-gate 	RUN_SCTP(sctp);
3087c478bd9Sstevel@tonic-gate 
3097c478bd9Sstevel@tonic-gate 	switch (sctp->sctp_state) {
3107c478bd9Sstevel@tonic-gate 	case SCTPS_IDLE:
3117c478bd9Sstevel@tonic-gate 	case SCTPS_BOUND:
3127c478bd9Sstevel@tonic-gate 	case SCTPS_LISTEN:
3137c478bd9Sstevel@tonic-gate 		break;
3147c478bd9Sstevel@tonic-gate 	case SCTPS_COOKIE_WAIT:
3157c478bd9Sstevel@tonic-gate 	case SCTPS_COOKIE_ECHOED:
3167c478bd9Sstevel@tonic-gate 		/*
3177c478bd9Sstevel@tonic-gate 		 * Close during the connect 3-way handshake
3187c478bd9Sstevel@tonic-gate 		 * but here there may or may not be pending data
3197c478bd9Sstevel@tonic-gate 		 * already on queue. Process almost same as in
3207c478bd9Sstevel@tonic-gate 		 * the ESTABLISHED state.
3217c478bd9Sstevel@tonic-gate 		 */
3227c478bd9Sstevel@tonic-gate 		if (sctp->sctp_xmit_head == NULL &&
3237c478bd9Sstevel@tonic-gate 		    sctp->sctp_xmit_unsent == NULL) {
3247c478bd9Sstevel@tonic-gate 			break;
3257c478bd9Sstevel@tonic-gate 		}
3267c478bd9Sstevel@tonic-gate 		/* FALLTHRU */
3277c478bd9Sstevel@tonic-gate 	default:
3287c478bd9Sstevel@tonic-gate 		/*
329e6f13f86SKacheong Poon 		 * If SO_LINGER has set a zero linger time, terminate the
330e6f13f86SKacheong Poon 		 * association and send an ABORT.
3317c478bd9Sstevel@tonic-gate 		 */
332bd670b35SErik Nordmark 		if (connp->conn_linger && connp->conn_lingertime == 0) {
333e6f13f86SKacheong Poon 			sctp_user_abort(sctp, NULL);
334e6f13f86SKacheong Poon 			WAKE_SCTP(sctp);
335e6f13f86SKacheong Poon 			return (error);
3367c478bd9Sstevel@tonic-gate 		}
3377c478bd9Sstevel@tonic-gate 
3381d8c4025Svi 		/*
339a215d4ebSKacheong Poon 		 * If there is unread data, send an ABORT and terminate the
340e6f13f86SKacheong Poon 		 * association.
3411d8c4025Svi 		 */
342a215d4ebSKacheong Poon 		if (sctp->sctp_rxqueued > 0 || sctp->sctp_ulp_rxqueued > 0) {
343e6f13f86SKacheong Poon 			sctp_user_abort(sctp, NULL);
344e6f13f86SKacheong Poon 			WAKE_SCTP(sctp);
345e6f13f86SKacheong Poon 			return (error);
3461d8c4025Svi 		}
3477c478bd9Sstevel@tonic-gate 		/*
3487c478bd9Sstevel@tonic-gate 		 * Transmit the shutdown before detaching the sctp_t.
3497c478bd9Sstevel@tonic-gate 		 * After sctp_detach returns this queue/perimeter
3507c478bd9Sstevel@tonic-gate 		 * no longer owns the sctp_t thus others can modify it.
3517c478bd9Sstevel@tonic-gate 		 */
3527c478bd9Sstevel@tonic-gate 		sctp_send_shutdown(sctp, 0);
3537c478bd9Sstevel@tonic-gate 
3547c478bd9Sstevel@tonic-gate 		/* Pass gathered wisdom to IP for keeping */
355bd670b35SErik Nordmark 		sctp_update_dce(sctp);
3567c478bd9Sstevel@tonic-gate 
3577c478bd9Sstevel@tonic-gate 		/*
3587c478bd9Sstevel@tonic-gate 		 * If lingering on close then wait until the shutdown
3597c478bd9Sstevel@tonic-gate 		 * is complete, or the SO_LINGER time passes, or an
3607c478bd9Sstevel@tonic-gate 		 * ABORT is sent/received.  Note that sctp_disconnect()
3617c478bd9Sstevel@tonic-gate 		 * can be called more than once.  Make sure that only
3627c478bd9Sstevel@tonic-gate 		 * one thread waits.
3637c478bd9Sstevel@tonic-gate 		 */
364bd670b35SErik Nordmark 		if (connp->conn_linger && connp->conn_lingertime > 0 &&
3657c478bd9Sstevel@tonic-gate 		    sctp->sctp_state >= SCTPS_ESTABLISHED &&
3667c478bd9Sstevel@tonic-gate 		    !sctp->sctp_lingering) {
3677c478bd9Sstevel@tonic-gate 			clock_t stoptime;	/* in ticks */
3687c478bd9Sstevel@tonic-gate 			clock_t ret;
3697c478bd9Sstevel@tonic-gate 
3707c478bd9Sstevel@tonic-gate 			sctp->sctp_lingering = 1;
3717c478bd9Sstevel@tonic-gate 			sctp->sctp_client_errno = 0;
372d3d50737SRafael Vanoni 			stoptime = ddi_get_lbolt() +
373d3d50737SRafael Vanoni 			    connp->conn_lingertime * hz;
3747c478bd9Sstevel@tonic-gate 
3757c478bd9Sstevel@tonic-gate 			mutex_enter(&sctp->sctp_lock);
3767c478bd9Sstevel@tonic-gate 			sctp->sctp_running = B_FALSE;
3777c478bd9Sstevel@tonic-gate 			while (sctp->sctp_state >= SCTPS_ESTABLISHED &&
3787c478bd9Sstevel@tonic-gate 			    sctp->sctp_client_errno == 0) {
3795dd46ab5SKacheong Poon 				cv_signal(&sctp->sctp_cv);
3807c478bd9Sstevel@tonic-gate 				ret = cv_timedwait_sig(&sctp->sctp_cv,
3817c478bd9Sstevel@tonic-gate 				    &sctp->sctp_lock, stoptime);
3827c478bd9Sstevel@tonic-gate 				if (ret < 0) {
3837c478bd9Sstevel@tonic-gate 					/* Stoptime has reached. */
3847c478bd9Sstevel@tonic-gate 					sctp->sctp_client_errno = EWOULDBLOCK;
3857c478bd9Sstevel@tonic-gate 					break;
3867c478bd9Sstevel@tonic-gate 				} else if (ret == 0) {
3877c478bd9Sstevel@tonic-gate 					/* Got a signal. */
3887c478bd9Sstevel@tonic-gate 					break;
3897c478bd9Sstevel@tonic-gate 				}
3907c478bd9Sstevel@tonic-gate 			}
3917c478bd9Sstevel@tonic-gate 			error = sctp->sctp_client_errno;
3927c478bd9Sstevel@tonic-gate 			sctp->sctp_client_errno = 0;
3937c478bd9Sstevel@tonic-gate 			mutex_exit(&sctp->sctp_lock);
3947c478bd9Sstevel@tonic-gate 		}
3957c478bd9Sstevel@tonic-gate 
3967c478bd9Sstevel@tonic-gate 		WAKE_SCTP(sctp);
3977c478bd9Sstevel@tonic-gate 		return (error);
3987c478bd9Sstevel@tonic-gate 	}
3997c478bd9Sstevel@tonic-gate 
4007c478bd9Sstevel@tonic-gate 
4017c478bd9Sstevel@tonic-gate 	/* Remove this sctp from all hashes so nobody can find it. */
4027c478bd9Sstevel@tonic-gate 	sctp_closei_local(sctp);
4037c478bd9Sstevel@tonic-gate 	WAKE_SCTP(sctp);
4047c478bd9Sstevel@tonic-gate 	return (error);
4057c478bd9Sstevel@tonic-gate }
4067c478bd9Sstevel@tonic-gate 
4077c478bd9Sstevel@tonic-gate void
sctp_close(sctp_t * sctp)4087c478bd9Sstevel@tonic-gate sctp_close(sctp_t *sctp)
4097c478bd9Sstevel@tonic-gate {
41045916cd2Sjpk 	dprint(3, ("sctp_close %p, state %d\n", (void *)sctp,
41145916cd2Sjpk 	    sctp->sctp_state));
4127c478bd9Sstevel@tonic-gate 
4137c478bd9Sstevel@tonic-gate 	RUN_SCTP(sctp);
4147c478bd9Sstevel@tonic-gate 	sctp->sctp_detached = 1;
4157c478bd9Sstevel@tonic-gate 	sctp->sctp_ulpd = NULL;
4160f1702c5SYu Xiangning 	sctp->sctp_upcalls = NULL;
4177c478bd9Sstevel@tonic-gate 	bzero(&sctp->sctp_events, sizeof (sctp->sctp_events));
4187c478bd9Sstevel@tonic-gate 
4197c478bd9Sstevel@tonic-gate 	/* If the graceful shutdown has not been completed, just return. */
4207c478bd9Sstevel@tonic-gate 	if (sctp->sctp_state != SCTPS_IDLE) {
4217c478bd9Sstevel@tonic-gate 		WAKE_SCTP(sctp);
4227c478bd9Sstevel@tonic-gate 		return;
4237c478bd9Sstevel@tonic-gate 	}
4247c478bd9Sstevel@tonic-gate 
4257c478bd9Sstevel@tonic-gate 	/*
4267c478bd9Sstevel@tonic-gate 	 * Since sctp_t is in SCTPS_IDLE state, so the only thread which
4277c478bd9Sstevel@tonic-gate 	 * can have a hold on the sctp_t is doing snmp stuff.  Just do
4287c478bd9Sstevel@tonic-gate 	 * a SCTP_REFRELE() here after the SCTP_UNLINK().  It will
4297c478bd9Sstevel@tonic-gate 	 * be freed when the other thread is done.
4307c478bd9Sstevel@tonic-gate 	 */
4317c478bd9Sstevel@tonic-gate 	SCTP_CONDEMNED(sctp);
4327c478bd9Sstevel@tonic-gate 	WAKE_SCTP(sctp);
4337c478bd9Sstevel@tonic-gate 	SCTP_REFRELE(sctp);
4347c478bd9Sstevel@tonic-gate }
4357c478bd9Sstevel@tonic-gate 
4367c478bd9Sstevel@tonic-gate /*
4377c478bd9Sstevel@tonic-gate  * Unlink from global list and do the eager close.
4387c478bd9Sstevel@tonic-gate  * Remove the refhold implicit in being on the global list.
4397c478bd9Sstevel@tonic-gate  */
4407c478bd9Sstevel@tonic-gate void
sctp_close_eager(sctp_t * sctp)4417c478bd9Sstevel@tonic-gate sctp_close_eager(sctp_t *sctp)
4427c478bd9Sstevel@tonic-gate {
4437c478bd9Sstevel@tonic-gate 	SCTP_CONDEMNED(sctp);
4447c478bd9Sstevel@tonic-gate 	sctp_closei_local(sctp);
4457c478bd9Sstevel@tonic-gate 	SCTP_REFRELE(sctp);
4467c478bd9Sstevel@tonic-gate }
4477c478bd9Sstevel@tonic-gate 
4487c478bd9Sstevel@tonic-gate /*
4497c478bd9Sstevel@tonic-gate  * The sctp_t is going away. Remove it from all lists and set it
4507c478bd9Sstevel@tonic-gate  * to SCTPS_IDLE. The caller has to remove it from the
4517c478bd9Sstevel@tonic-gate  * global list. The freeing up of memory is deferred until
4527c478bd9Sstevel@tonic-gate  * sctp_free(). This is needed since a thread in sctp_input() might have
4537c478bd9Sstevel@tonic-gate  * done a SCTP_REFHOLD on this structure before it was removed from the
4547c478bd9Sstevel@tonic-gate  * hashes.
4557c478bd9Sstevel@tonic-gate  */
4567c478bd9Sstevel@tonic-gate static void
sctp_closei_local(sctp_t * sctp)4577c478bd9Sstevel@tonic-gate sctp_closei_local(sctp_t *sctp)
4587c478bd9Sstevel@tonic-gate {
4597c478bd9Sstevel@tonic-gate 	mblk_t	*mp;
4607c478bd9Sstevel@tonic-gate 	conn_t	*connp = sctp->sctp_connp;
4617c478bd9Sstevel@tonic-gate 
4625dd46ab5SKacheong Poon 	/* The counter is incremented only for established associations. */
4635dd46ab5SKacheong Poon 	if (sctp->sctp_state >= SCTPS_ESTABLISHED)
4645dd46ab5SKacheong Poon 		SCTPS_ASSOC_DEC(sctp->sctp_sctps);
4655dd46ab5SKacheong Poon 
4665dd46ab5SKacheong Poon 	if (sctp->sctp_listen_cnt != NULL)
4675dd46ab5SKacheong Poon 		SCTP_DECR_LISTEN_CNT(sctp);
4685dd46ab5SKacheong Poon 
469b34b8d1aSkcpoon 	/* Sanity check, don't do the same thing twice.  */
470b34b8d1aSkcpoon 	if (connp->conn_state_flags & CONN_CLOSING) {
471b34b8d1aSkcpoon 		ASSERT(sctp->sctp_state == SCTPS_IDLE);
472b34b8d1aSkcpoon 		return;
473b34b8d1aSkcpoon 	}
474b34b8d1aSkcpoon 
4757c478bd9Sstevel@tonic-gate 	/* Stop and free the timers */
4767c478bd9Sstevel@tonic-gate 	sctp_free_faddr_timers(sctp);
4777c478bd9Sstevel@tonic-gate 	if ((mp = sctp->sctp_heartbeat_mp) != NULL) {
4787c478bd9Sstevel@tonic-gate 		sctp_timer_free(mp);
4797c478bd9Sstevel@tonic-gate 		sctp->sctp_heartbeat_mp = NULL;
4807c478bd9Sstevel@tonic-gate 	}
4817c478bd9Sstevel@tonic-gate 	if ((mp = sctp->sctp_ack_mp) != NULL) {
4827c478bd9Sstevel@tonic-gate 		sctp_timer_free(mp);
4837c478bd9Sstevel@tonic-gate 		sctp->sctp_ack_mp = NULL;
4847c478bd9Sstevel@tonic-gate 	}
4857c478bd9Sstevel@tonic-gate 
4867c478bd9Sstevel@tonic-gate 	/* Set the CONN_CLOSING flag so that IP will not cache IRE again. */
4877c478bd9Sstevel@tonic-gate 	mutex_enter(&connp->conn_lock);
4887c478bd9Sstevel@tonic-gate 	connp->conn_state_flags |= CONN_CLOSING;
4897c478bd9Sstevel@tonic-gate 	mutex_exit(&connp->conn_lock);
4907c478bd9Sstevel@tonic-gate 
4917c478bd9Sstevel@tonic-gate 	/* Remove from all hashes. */
4927c478bd9Sstevel@tonic-gate 	sctp_bind_hash_remove(sctp);
4937c478bd9Sstevel@tonic-gate 	sctp_conn_hash_remove(sctp);
4947c478bd9Sstevel@tonic-gate 	sctp_listen_hash_remove(sctp);
4957c478bd9Sstevel@tonic-gate 	sctp->sctp_state = SCTPS_IDLE;
4967c478bd9Sstevel@tonic-gate 
4977c478bd9Sstevel@tonic-gate 	/*
4987c478bd9Sstevel@tonic-gate 	 * Clean up the recvq as much as possible.  All those packets
4997c478bd9Sstevel@tonic-gate 	 * will be silently dropped as this sctp_t is now in idle state.
5007c478bd9Sstevel@tonic-gate 	 */
5017c478bd9Sstevel@tonic-gate 	mutex_enter(&sctp->sctp_recvq_lock);
5027c478bd9Sstevel@tonic-gate 	while ((mp = sctp->sctp_recvq) != NULL) {
5037c478bd9Sstevel@tonic-gate 		sctp->sctp_recvq = mp->b_next;
5047c478bd9Sstevel@tonic-gate 		mp->b_next = NULL;
505bd670b35SErik Nordmark 
506bd670b35SErik Nordmark 		if (ip_recv_attr_is_mblk(mp))
507bd670b35SErik Nordmark 			mp = ip_recv_attr_free_mblk(mp);
508bd670b35SErik Nordmark 
5097c478bd9Sstevel@tonic-gate 		freemsg(mp);
5107c478bd9Sstevel@tonic-gate 	}
5117c478bd9Sstevel@tonic-gate 	mutex_exit(&sctp->sctp_recvq_lock);
5127c478bd9Sstevel@tonic-gate }
5137c478bd9Sstevel@tonic-gate 
5147c478bd9Sstevel@tonic-gate /*
5157c478bd9Sstevel@tonic-gate  * Free memory associated with the sctp/ip header template.
5167c478bd9Sstevel@tonic-gate  */
5177c478bd9Sstevel@tonic-gate static void
sctp_headers_free(sctp_t * sctp)5187c478bd9Sstevel@tonic-gate sctp_headers_free(sctp_t *sctp)
5197c478bd9Sstevel@tonic-gate {
5207c478bd9Sstevel@tonic-gate 	if (sctp->sctp_iphc != NULL) {
5217c478bd9Sstevel@tonic-gate 		kmem_free(sctp->sctp_iphc, sctp->sctp_iphc_len);
5227c478bd9Sstevel@tonic-gate 		sctp->sctp_iphc = NULL;
5237c478bd9Sstevel@tonic-gate 		sctp->sctp_ipha = NULL;
5247c478bd9Sstevel@tonic-gate 		sctp->sctp_hdr_len = 0;
5257c478bd9Sstevel@tonic-gate 		sctp->sctp_ip_hdr_len = 0;
5267c478bd9Sstevel@tonic-gate 		sctp->sctp_iphc_len = 0;
5277c478bd9Sstevel@tonic-gate 		sctp->sctp_sctph = NULL;
5287c478bd9Sstevel@tonic-gate 		sctp->sctp_hdr_len = 0;
5297c478bd9Sstevel@tonic-gate 	}
5307c478bd9Sstevel@tonic-gate 	if (sctp->sctp_iphc6 != NULL) {
5317c478bd9Sstevel@tonic-gate 		kmem_free(sctp->sctp_iphc6, sctp->sctp_iphc6_len);
5327c478bd9Sstevel@tonic-gate 		sctp->sctp_iphc6 = NULL;
5337c478bd9Sstevel@tonic-gate 		sctp->sctp_ip6h = NULL;
5347c478bd9Sstevel@tonic-gate 		sctp->sctp_hdr6_len = 0;
5357c478bd9Sstevel@tonic-gate 		sctp->sctp_ip_hdr6_len = 0;
5367c478bd9Sstevel@tonic-gate 		sctp->sctp_iphc6_len = 0;
5377c478bd9Sstevel@tonic-gate 		sctp->sctp_sctph6 = NULL;
5387c478bd9Sstevel@tonic-gate 		sctp->sctp_hdr6_len = 0;
5397c478bd9Sstevel@tonic-gate 	}
5407c478bd9Sstevel@tonic-gate }
5417c478bd9Sstevel@tonic-gate 
5427c478bd9Sstevel@tonic-gate static void
sctp_free_xmit_data(sctp_t * sctp)5437c478bd9Sstevel@tonic-gate sctp_free_xmit_data(sctp_t *sctp)
5447c478bd9Sstevel@tonic-gate {
5457c478bd9Sstevel@tonic-gate 	mblk_t	*ump = NULL;
5467c478bd9Sstevel@tonic-gate 	mblk_t	*nump;
5477c478bd9Sstevel@tonic-gate 	mblk_t	*mp;
5487c478bd9Sstevel@tonic-gate 	mblk_t	*nmp;
5497c478bd9Sstevel@tonic-gate 
5507c478bd9Sstevel@tonic-gate 	sctp->sctp_xmit_unacked = NULL;
5517c478bd9Sstevel@tonic-gate 	ump = sctp->sctp_xmit_head;
5527c478bd9Sstevel@tonic-gate 	sctp->sctp_xmit_tail = sctp->sctp_xmit_head = NULL;
5537c478bd9Sstevel@tonic-gate free_unsent:
5547c478bd9Sstevel@tonic-gate 	for (; ump != NULL; ump = nump) {
5557c478bd9Sstevel@tonic-gate 		for (mp = ump->b_cont; mp != NULL; mp = nmp) {
5567c478bd9Sstevel@tonic-gate 			nmp = mp->b_next;
5577c478bd9Sstevel@tonic-gate 			mp->b_next = NULL;
5587c478bd9Sstevel@tonic-gate 			mp->b_prev = NULL;
5597c478bd9Sstevel@tonic-gate 			freemsg(mp);
5607c478bd9Sstevel@tonic-gate 		}
5617c478bd9Sstevel@tonic-gate 		ASSERT(DB_REF(ump) == 1);
5627c478bd9Sstevel@tonic-gate 		nump = ump->b_next;
5637c478bd9Sstevel@tonic-gate 		ump->b_next = NULL;
5647c478bd9Sstevel@tonic-gate 		ump->b_prev = NULL;
5657c478bd9Sstevel@tonic-gate 		ump->b_cont = NULL;
5667c478bd9Sstevel@tonic-gate 		freeb(ump);
5677c478bd9Sstevel@tonic-gate 	}
5687c478bd9Sstevel@tonic-gate 	if ((ump = sctp->sctp_xmit_unsent) == NULL) {
5697c478bd9Sstevel@tonic-gate 		ASSERT(sctp->sctp_xmit_unsent_tail == NULL);
5707c478bd9Sstevel@tonic-gate 		return;
5717c478bd9Sstevel@tonic-gate 	}
5727c478bd9Sstevel@tonic-gate 	sctp->sctp_xmit_unsent = sctp->sctp_xmit_unsent_tail = NULL;
5737c478bd9Sstevel@tonic-gate 	goto free_unsent;
5747c478bd9Sstevel@tonic-gate }
5757c478bd9Sstevel@tonic-gate 
5767c478bd9Sstevel@tonic-gate /*
5777c478bd9Sstevel@tonic-gate  * Cleanup all the messages in the stream queue and the reassembly lists.
5787c478bd9Sstevel@tonic-gate  * If 'free' is true, then delete the streams as well.
5797c478bd9Sstevel@tonic-gate  */
5807c478bd9Sstevel@tonic-gate void
sctp_instream_cleanup(sctp_t * sctp,boolean_t free)5817c478bd9Sstevel@tonic-gate sctp_instream_cleanup(sctp_t *sctp, boolean_t free)
5827c478bd9Sstevel@tonic-gate {
5837c478bd9Sstevel@tonic-gate 	int	i;
5847c478bd9Sstevel@tonic-gate 	mblk_t	*mp;
5857c478bd9Sstevel@tonic-gate 	mblk_t	*mp1;
5867c478bd9Sstevel@tonic-gate 
5877c478bd9Sstevel@tonic-gate 	if (sctp->sctp_instr != NULL) {
5887c478bd9Sstevel@tonic-gate 		/* walk thru and flush out anything remaining in the Q */
5897c478bd9Sstevel@tonic-gate 		for (i = 0; i < sctp->sctp_num_istr; i++) {
5907c478bd9Sstevel@tonic-gate 			mp = sctp->sctp_instr[i].istr_msgs;
5917c478bd9Sstevel@tonic-gate 			while (mp != NULL) {
5927c478bd9Sstevel@tonic-gate 				mp1 = mp->b_next;
5937c478bd9Sstevel@tonic-gate 				mp->b_next = mp->b_prev = NULL;
5947c478bd9Sstevel@tonic-gate 				freemsg(mp);
5957c478bd9Sstevel@tonic-gate 				mp = mp1;
5967c478bd9Sstevel@tonic-gate 			}
5977c478bd9Sstevel@tonic-gate 			sctp->sctp_instr[i].istr_msgs = NULL;
598a0263d1cSnh 			sctp->sctp_instr[i].istr_nmsgs = 0;
5997c478bd9Sstevel@tonic-gate 			sctp_free_reass((sctp->sctp_instr) + i);
6007c478bd9Sstevel@tonic-gate 			sctp->sctp_instr[i].nextseq = 0;
6017c478bd9Sstevel@tonic-gate 		}
6027c478bd9Sstevel@tonic-gate 		if (free) {
6037c478bd9Sstevel@tonic-gate 			kmem_free(sctp->sctp_instr,
6047c478bd9Sstevel@tonic-gate 			    sizeof (*sctp->sctp_instr) * sctp->sctp_num_istr);
6057c478bd9Sstevel@tonic-gate 			sctp->sctp_instr = NULL;
6067c478bd9Sstevel@tonic-gate 			sctp->sctp_num_istr = 0;
6077c478bd9Sstevel@tonic-gate 		}
6087c478bd9Sstevel@tonic-gate 	}
6097c478bd9Sstevel@tonic-gate 	/* un-ordered fragments */
6107c478bd9Sstevel@tonic-gate 	if (sctp->sctp_uo_frags != NULL) {
6117c478bd9Sstevel@tonic-gate 		for (mp = sctp->sctp_uo_frags; mp != NULL; mp = mp1) {
6127c478bd9Sstevel@tonic-gate 			mp1 = mp->b_next;
6137c478bd9Sstevel@tonic-gate 			mp->b_next = mp->b_prev = NULL;
6147c478bd9Sstevel@tonic-gate 			freemsg(mp);
6157c478bd9Sstevel@tonic-gate 		}
61692baa190SGeorge Shepherd 		sctp->sctp_uo_frags = NULL;
6177c478bd9Sstevel@tonic-gate 	}
6187c478bd9Sstevel@tonic-gate }
6197c478bd9Sstevel@tonic-gate 
6207c478bd9Sstevel@tonic-gate /*
6217c478bd9Sstevel@tonic-gate  * Last reference to the sctp_t is gone. Free all memory associated with it.
6227c478bd9Sstevel@tonic-gate  * Called from SCTP_REFRELE. Called inline in sctp_close()
6237c478bd9Sstevel@tonic-gate  */
6247c478bd9Sstevel@tonic-gate void
sctp_free(conn_t * connp)6257c478bd9Sstevel@tonic-gate sctp_free(conn_t *connp)
6267c478bd9Sstevel@tonic-gate {
6277c478bd9Sstevel@tonic-gate 	sctp_t *sctp = CONN2SCTP(connp);
6287c478bd9Sstevel@tonic-gate 	int		cnt;
629f4b3ec61Sdh 	sctp_stack_t	*sctps = sctp->sctp_sctps;
6307c478bd9Sstevel@tonic-gate 
631f4b3ec61Sdh 	ASSERT(sctps != NULL);
6327c478bd9Sstevel@tonic-gate 	/* Unlink it from the global list */
633f4b3ec61Sdh 	SCTP_UNLINK(sctp, sctps);
6347c478bd9Sstevel@tonic-gate 
6357c478bd9Sstevel@tonic-gate 	ASSERT(connp->conn_ref == 0);
636bd670b35SErik Nordmark 	ASSERT(connp->conn_proto == IPPROTO_SCTP);
6377c478bd9Sstevel@tonic-gate 	ASSERT(!MUTEX_HELD(&sctp->sctp_reflock));
6387c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_refcnt == 0);
6397c478bd9Sstevel@tonic-gate 
6407c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_ptpbhn == NULL && sctp->sctp_bind_hash == NULL);
6417c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_conn_hash_next == NULL &&
6427c478bd9Sstevel@tonic-gate 	    sctp->sctp_conn_hash_prev == NULL);
6437c478bd9Sstevel@tonic-gate 
6447c478bd9Sstevel@tonic-gate 
6457c478bd9Sstevel@tonic-gate 	/* Free up all the resources. */
6467c478bd9Sstevel@tonic-gate 
6477c478bd9Sstevel@tonic-gate 	/* blow away sctp stream management */
6487c478bd9Sstevel@tonic-gate 	if (sctp->sctp_ostrcntrs != NULL) {
6497c478bd9Sstevel@tonic-gate 		kmem_free(sctp->sctp_ostrcntrs,
6507c478bd9Sstevel@tonic-gate 		    sizeof (uint16_t) * sctp->sctp_num_ostr);
6517c478bd9Sstevel@tonic-gate 		sctp->sctp_ostrcntrs = NULL;
6527c478bd9Sstevel@tonic-gate 	}
6537c478bd9Sstevel@tonic-gate 	sctp_instream_cleanup(sctp, B_TRUE);
6547c478bd9Sstevel@tonic-gate 
6557c478bd9Sstevel@tonic-gate 	/* Remove all data transfer resources. */
6567c478bd9Sstevel@tonic-gate 	sctp->sctp_istr_nmsgs = 0;
6577c478bd9Sstevel@tonic-gate 	sctp->sctp_rxqueued = 0;
6587c478bd9Sstevel@tonic-gate 	sctp_free_xmit_data(sctp);
6597c478bd9Sstevel@tonic-gate 	sctp->sctp_unacked = 0;
6607c478bd9Sstevel@tonic-gate 	sctp->sctp_unsent = 0;
6611d8c4025Svi 	if (sctp->sctp_cxmit_list != NULL)
6621d8c4025Svi 		sctp_asconf_free_cxmit(sctp, NULL);
6631d8c4025Svi 
6647c478bd9Sstevel@tonic-gate 	sctp->sctp_lastdata = NULL;
6657c478bd9Sstevel@tonic-gate 
6667c478bd9Sstevel@tonic-gate 	/* Clear out default xmit settings */
6677c478bd9Sstevel@tonic-gate 	sctp->sctp_def_stream = 0;
6687c478bd9Sstevel@tonic-gate 	sctp->sctp_def_flags = 0;
6697c478bd9Sstevel@tonic-gate 	sctp->sctp_def_ppid = 0;
6707c478bd9Sstevel@tonic-gate 	sctp->sctp_def_context = 0;
6717c478bd9Sstevel@tonic-gate 	sctp->sctp_def_timetolive = 0;
6727c478bd9Sstevel@tonic-gate 
6737c478bd9Sstevel@tonic-gate 	if (sctp->sctp_sack_info != NULL) {
6747c478bd9Sstevel@tonic-gate 		sctp_free_set(sctp->sctp_sack_info);
6757c478bd9Sstevel@tonic-gate 		sctp->sctp_sack_info = NULL;
6767c478bd9Sstevel@tonic-gate 	}
6777c478bd9Sstevel@tonic-gate 	sctp->sctp_sack_gaps = 0;
6787c478bd9Sstevel@tonic-gate 
6797c478bd9Sstevel@tonic-gate 	if (sctp->sctp_cookie_mp != NULL) {
6807c478bd9Sstevel@tonic-gate 		freemsg(sctp->sctp_cookie_mp);
6817c478bd9Sstevel@tonic-gate 		sctp->sctp_cookie_mp = NULL;
6827c478bd9Sstevel@tonic-gate 	}
683