xref: /illumos-gate/usr/src/uts/common/inet/sctp/sctp.c (revision a215d4eb)
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
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 *
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
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
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 		/*
339*a215d4ebSKacheong Poon 		 * If there is unread data, send an ABORT and terminate the
340e6f13f86SKacheong Poon 		 * association.
3411d8c4025Svi 		 */
342*a215d4ebSKacheong 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
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
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
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
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
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
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
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 	}
6837c478bd9Sstevel@tonic-gate 
6847c478bd9Sstevel@tonic-gate 	/* Remove all the address resources. */
6857c478bd9Sstevel@tonic-gate 	sctp_zap_addrs(sctp);
6867c478bd9Sstevel@tonic-gate 	for (cnt = 0; cnt < SCTP_IPIF_HASH; cnt++) {
6877c478bd9Sstevel@tonic-gate 		ASSERT(sctp->sctp_saddrs[cnt].ipif_count == 0);
6887c478bd9Sstevel@tonic-gate 		list_destroy(&sctp->sctp_saddrs[cnt].sctp_ipif_list);
6897c478bd9Sstevel@tonic-gate 	}
6907c478bd9Sstevel@tonic-gate 
6917c478bd9Sstevel@tonic-gate 	if (sctp->sctp_hopopts != NULL) {
6927c478bd9Sstevel@tonic-gate 		mi_free(sctp->sctp_hopopts);
6937c478bd9Sstevel@tonic-gate 		sctp->sctp_hopopts = NULL;
6947c478bd9Sstevel@tonic-gate 		sctp->sctp_hopoptslen = 0;
6957c478bd9Sstevel@tonic-gate 	}
6967c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_hopoptslen == 0);
6977c478bd9Sstevel@tonic-gate 	if (sctp->sctp_dstopts != NULL) {
6987c478bd9Sstevel@tonic-gate 		mi_free(sctp->sctp_dstopts);
6997c478bd9Sstevel@tonic-gate 		sctp->sctp_dstopts = NULL;
7007c478bd9Sstevel@tonic-gate 		sctp->sctp_dstoptslen = 0;
7017c478bd9Sstevel@tonic-gate 	}
7027c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_dstoptslen == 0);
703bd670b35SErik Nordmark 	if (sctp->sctp_rthdrdstopts != NULL) {
704bd670b35SErik Nordmark 		mi_free(sctp->sctp_rthdrdstopts);
705bd670b35SErik Nordmark 		sctp->sctp_rthdrdstopts = NULL;
706bd670b35SErik Nordmark 		sctp->sctp_rthdrdstoptslen = 0;
7077c478bd9Sstevel@tonic-gate 	}
708bd670b35SErik Nordmark 	ASSERT(sctp->sctp_rthdrdstoptslen == 0);
7097c478bd9Sstevel@tonic-gate 	if (sctp->sctp_rthdr != NULL) {
7107c478bd9Sstevel@tonic-gate 		mi_free(sctp->sctp_rthdr);
7117c478bd9Sstevel@tonic-gate 		sctp->sctp_rthdr = NULL;
7127c478bd9Sstevel@tonic-gate 		sctp->sctp_rthdrlen = 0;
7137c478bd9Sstevel@tonic-gate 	}
7147c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_rthdrlen == 0);
7157c478bd9Sstevel@tonic-gate 	sctp_headers_free(sctp);
7167c478bd9Sstevel@tonic-gate 
7177c478bd9Sstevel@tonic-gate 	sctp->sctp_shutdown_faddr = NULL;
7187c478bd9Sstevel@tonic-gate 
7197f093707Skcpoon 	if (sctp->sctp_err_chunks != NULL) {
7207f093707Skcpoon 		freemsg(sctp->sctp_err_chunks);
7217f093707Skcpoon 		sctp->sctp_err_chunks = NULL;
7227f093707Skcpoon 		sctp->sctp_err_len = 0;
7237f093707Skcpoon 	}
7247f093707Skcpoon 
7257c478bd9Sstevel@tonic-gate 	/* Clear all the bitfields. */
7267c478bd9Sstevel@tonic-gate 	bzero(&sctp->sctp_bits, sizeof (sctp->sctp_bits));
7277c478bd9Sstevel@tonic-gate 
7287c478bd9Sstevel@tonic-gate 	/* It is time to update the global statistics. */
7295dd46ab5SKacheong Poon 	SCTPS_UPDATE_MIB(sctps, sctpOutSCTPPkts, sctp->sctp_opkts);
7305dd46ab5SKacheong Poon 	SCTPS_UPDATE_MIB(sctps, sctpOutCtrlChunks, sctp->sctp_obchunks);
7315dd46ab5SKacheong Poon 	SCTPS_UPDATE_MIB(sctps, sctpOutOrderChunks, sctp->sctp_odchunks);
7325dd46ab5SKacheong Poon 	SCTPS_UPDATE_MIB(sctps, sctpOutUnorderChunks, sctp->sctp_oudchunks);
7335dd46ab5SKacheong Poon 	SCTPS_UPDATE_MIB(sctps, sctpRetransChunks, sctp->sctp_rxtchunks);
7345dd46ab5SKacheong Poon 	SCTPS_UPDATE_MIB(sctps, sctpInSCTPPkts, sctp->sctp_ipkts);
7355dd46ab5SKacheong Poon 	SCTPS_UPDATE_MIB(sctps, sctpInCtrlChunks, sctp->sctp_ibchunks);
7365dd46ab5SKacheong Poon 	SCTPS_UPDATE_MIB(sctps, sctpInOrderChunks, sctp->sctp_idchunks);
7375dd46ab5SKacheong Poon 	SCTPS_UPDATE_MIB(sctps, sctpInUnorderChunks, sctp->sctp_iudchunks);
7385dd46ab5SKacheong Poon 	SCTPS_UPDATE_MIB(sctps, sctpFragUsrMsgs, sctp->sctp_fragdmsgs);
7395dd46ab5SKacheong Poon 	SCTPS_UPDATE_MIB(sctps, sctpReasmUsrMsgs, sctp->sctp_reassmsgs);
7407c478bd9Sstevel@tonic-gate 	sctp->sctp_opkts = 0;
7417c478bd9Sstevel@tonic-gate 	sctp->sctp_obchunks = 0;
7427c478bd9Sstevel@tonic-gate 	sctp->sctp_odchunks = 0;
7437c478bd9Sstevel@tonic-gate 	sctp->sctp_oudchunks = 0;
7447c478bd9Sstevel@tonic-gate 	sctp->sctp_rxtchunks = 0;
7457c478bd9Sstevel@tonic-gate 	sctp->sctp_ipkts = 0;
7467c478bd9Sstevel@tonic-gate 	sctp->sctp_ibchunks = 0;
7477c478bd9Sstevel@tonic-gate 	sctp->sctp_idchunks = 0;
7487c478bd9Sstevel@tonic-gate 	sctp->sctp_iudchunks = 0;
7497c478bd9Sstevel@tonic-gate 	sctp->sctp_fragdmsgs = 0;
7507c478bd9Sstevel@tonic-gate 	sctp->sctp_reassmsgs = 0;
75135e12f9cSGeorge Shepherd 	sctp->sctp_outseqtsns = 0;
75235e12f9cSGeorge Shepherd 	sctp->sctp_osacks = 0;
75335e12f9cSGeorge Shepherd 	sctp->sctp_isacks = 0;
75435e12f9cSGeorge Shepherd 	sctp->sctp_idupchunks = 0;
75535e12f9cSGeorge Shepherd 	sctp->sctp_gapcnt = 0;
75635e12f9cSGeorge Shepherd 	sctp->sctp_cum_obchunks = 0;
75735e12f9cSGeorge Shepherd 	sctp->sctp_cum_odchunks = 0;
75835e12f9cSGeorge Shepherd 	sctp->sctp_cum_oudchunks = 0;
75935e12f9cSGeorge Shepherd 	sctp->sctp_cum_rxtchunks = 0;
76035e12f9cSGeorge Shepherd 	sctp->sctp_cum_ibchunks = 0;
76135e12f9cSGeorge Shepherd 	sctp->sctp_cum_idchunks = 0;
76235e12f9cSGeorge Shepherd 	sctp->sctp_cum_iudchunks = 0;
7637c478bd9Sstevel@tonic-gate 
7647c478bd9Sstevel@tonic-gate 	sctp->sctp_autoclose = 0;
765558fbd03Skcpoon 	sctp->sctp_tx_adaptation_code = 0;
7667c478bd9Sstevel@tonic-gate 
7677f99e6fcSkp 	sctp->sctp_v6label_len = 0;
7687f99e6fcSkp 	sctp->sctp_v4label_len = 0;
7697f99e6fcSkp 
770f4b3ec61Sdh 	sctp->sctp_sctps = NULL;
771f4b3ec61Sdh 
772121e5416Skcpoon 	sctp_conn_clear(connp);
7737c478bd9Sstevel@tonic-gate 	kmem_cache_free(sctp_conn_cache, connp);
7747c478bd9Sstevel@tonic-gate }
7757c478bd9Sstevel@tonic-gate 
7767c478bd9Sstevel@tonic-gate /*
7777c478bd9Sstevel@tonic-gate  * Initialize protocol control block. If a parent exists, inherit
7787c478bd9Sstevel@tonic-gate  * all values set through setsockopt().
7797c478bd9Sstevel@tonic-gate  */
7807c478bd9Sstevel@tonic-gate static int
7817c478bd9Sstevel@tonic-gate sctp_init_values(sctp_t *sctp, sctp_t *psctp, int sleep)
7827c478bd9Sstevel@tonic-gate {
7837c478bd9Sstevel@tonic-gate 	int	err;
7847c478bd9Sstevel@tonic-gate 	int	cnt;
785f4b3ec61Sdh 	sctp_stack_t	*sctps = sctp->sctp_sctps;
786bd670b35SErik Nordmark 	conn_t 	*connp;
7877c478bd9Sstevel@tonic-gate 
788bd670b35SErik Nordmark 	connp = sctp->sctp_connp;
7897c478bd9Sstevel@tonic-gate 
7907c478bd9Sstevel@tonic-gate 	sctp->sctp_nsaddrs = 0;
7917c478bd9Sstevel@tonic-gate 	for (cnt = 0; cnt < SCTP_IPIF_HASH; cnt++) {
7927c478bd9Sstevel@tonic-gate 		sctp->sctp_saddrs[cnt].ipif_count = 0;
7937c478bd9Sstevel@tonic-gate 		list_create(&sctp->sctp_saddrs[cnt].sctp_ipif_list,
7947c478bd9Sstevel@tonic-gate 		    sizeof (sctp_saddr_ipif_t), offsetof(sctp_saddr_ipif_t,
7957c478bd9Sstevel@tonic-gate 		    saddr_ipif));
7967c478bd9Sstevel@tonic-gate 	}
797bd670b35SErik Nordmark 	connp->conn_ports = 0;
7987c478bd9Sstevel@tonic-gate 	sctp->sctp_running = B_FALSE;
7997c478bd9Sstevel@tonic-gate 	sctp->sctp_state = SCTPS_IDLE;
8007c478bd9Sstevel@tonic-gate 
8017c478bd9Sstevel@tonic-gate 	sctp->sctp_refcnt = 1;
8027c478bd9Sstevel@tonic-gate 
8037c478bd9Sstevel@tonic-gate 	sctp->sctp_strikes = 0;
8047c478bd9Sstevel@tonic-gate 
805d3d50737SRafael Vanoni 	sctp->sctp_last_mtu_probe = ddi_get_lbolt64();
806f4b3ec61Sdh 	sctp->sctp_mtu_probe_intvl = sctps->sctps_mtu_probe_interval;
8077c478bd9Sstevel@tonic-gate 
8087c478bd9Sstevel@tonic-gate 	sctp->sctp_sack_gaps = 0;
809*a215d4ebSKacheong Poon 	/* So we will not delay sending the first SACK. */
810*a215d4ebSKacheong Poon 	sctp->sctp_sack_toggle = sctps->sctps_deferred_acks_max;
8117c478bd9Sstevel@tonic-gate 
812121e5416Skcpoon 	/* Only need to do the allocation if there is no "cached" one. */
813121e5416Skcpoon 	if (sctp->sctp_pad_mp == NULL) {
814121e5416Skcpoon 		if (sleep == KM_SLEEP) {
815121e5416Skcpoon 			sctp->sctp_pad_mp = allocb_wait(SCTP_ALIGN, BPRI_MED,
816121e5416Skcpoon 			    STR_NOSIG, NULL);
817121e5416Skcpoon 		} else {
818121e5416Skcpoon 			sctp->sctp_pad_mp = allocb(SCTP_ALIGN, BPRI_MED);
819121e5416Skcpoon 			if (sctp->sctp_pad_mp == NULL)
820121e5416Skcpoon 				return (ENOMEM);
821121e5416Skcpoon 		}
822121e5416Skcpoon 		bzero(sctp->sctp_pad_mp->b_rptr, SCTP_ALIGN);
823121e5416Skcpoon 	}
824121e5416Skcpoon 
8257c478bd9Sstevel@tonic-gate 	if (psctp != NULL) {
8267c478bd9Sstevel@tonic-gate 		/*
8277c478bd9Sstevel@tonic-gate 		 * Inherit from parent
828bd670b35SErik Nordmark 		 *
829bd670b35SErik Nordmark 		 * Start by inheriting from the conn_t, including conn_ixa and
830bd670b35SErik Nordmark 		 * conn_xmit_ipp.
8317c478bd9Sstevel@tonic-gate 		 */
832bd670b35SErik Nordmark 		err = conn_inherit_parent(psctp->sctp_connp, connp);
833bd670b35SErik Nordmark 		if (err != 0)
834121e5416Skcpoon 			goto failure;
8357c478bd9Sstevel@tonic-gate 
836*a215d4ebSKacheong Poon 		sctp->sctp_upcalls = psctp->sctp_upcalls;
837*a215d4ebSKacheong Poon 
8387c478bd9Sstevel@tonic-gate 		sctp->sctp_cookie_lifetime = psctp->sctp_cookie_lifetime;
839bd670b35SErik Nordmark 
8407c478bd9Sstevel@tonic-gate 		sctp->sctp_cwnd_max = psctp->sctp_cwnd_max;
8417c478bd9Sstevel@tonic-gate 		sctp->sctp_rwnd = psctp->sctp_rwnd;
842*a215d4ebSKacheong Poon 		sctp->sctp_arwnd = psctp->sctp_arwnd;
8437d546a59Svi 		sctp->sctp_pd_point = psctp->sctp_pd_point;
8447c478bd9Sstevel@tonic-gate 		sctp->sctp_rto_max = psctp->sctp_rto_max;
845fd7b5aedSGeorge Shepherd 		sctp->sctp_rto_max_init = psctp->sctp_rto_max_init;
8467c478bd9Sstevel@tonic-gate 		sctp->sctp_rto_min = psctp->sctp_rto_min;
8477c478bd9Sstevel@tonic-gate 		sctp->sctp_rto_initial = psctp->sctp_rto_initial;
8487c478bd9Sstevel@tonic-gate 		sctp->sctp_pa_max_rxt = psctp->sctp_pa_max_rxt;
8497c478bd9Sstevel@tonic-gate 		sctp->sctp_pp_max_rxt = psctp->sctp_pp_max_rxt;
8507c478bd9Sstevel@tonic-gate 		sctp->sctp_max_init_rxt = psctp->sctp_max_init_rxt;
8517c478bd9Sstevel@tonic-gate 
8527c478bd9Sstevel@tonic-gate 		sctp->sctp_def_stream = psctp->sctp_def_stream;
8537c478bd9Sstevel@tonic-gate 		sctp->sctp_def_flags = psctp->sctp_def_flags;
8547c478bd9Sstevel@tonic-gate 		sctp->sctp_def_ppid = psctp->sctp_def_ppid;
8557c478bd9Sstevel@tonic-gate 		sctp->sctp_def_context = psctp->sctp_def_context;
8567c478bd9Sstevel@tonic-gate 		sctp->sctp_def_timetolive = psctp->sctp_def_timetolive;
8577c478bd9Sstevel@tonic-gate 
8587c478bd9Sstevel@tonic-gate 		sctp->sctp_num_istr = psctp->sctp_num_istr;
8597c478bd9Sstevel@tonic-gate 		sctp->sctp_num_ostr = psctp->sctp_num_ostr;
8607c478bd9Sstevel@tonic-gate 
8617c478bd9Sstevel@tonic-gate 		sctp->sctp_hb_interval = psctp->sctp_hb_interval;
8627c478bd9Sstevel@tonic-gate 		sctp->sctp_autoclose = psctp->sctp_autoclose;
863558fbd03Skcpoon 		sctp->sctp_tx_adaptation_code = psctp->sctp_tx_adaptation_code;
8647c478bd9Sstevel@tonic-gate 
8657c478bd9Sstevel@tonic-gate 		/* xxx should be a better way to copy these flags xxx */
8667c478bd9Sstevel@tonic-gate 		sctp->sctp_bound_to_all = psctp->sctp_bound_to_all;
8677c478bd9Sstevel@tonic-gate 		sctp->sctp_cansleep = psctp->sctp_cansleep;
868558fbd03Skcpoon 		sctp->sctp_send_adaptation = psctp->sctp_send_adaptation;
8697c478bd9Sstevel@tonic-gate 		sctp->sctp_ndelay = psctp->sctp_ndelay;
8707c478bd9Sstevel@tonic-gate 		sctp->sctp_events = psctp->sctp_events;
8717c478bd9Sstevel@tonic-gate 	} else {
8727c478bd9Sstevel@tonic-gate 		/*
8737c478bd9Sstevel@tonic-gate 		 * Set to system defaults
8747c478bd9Sstevel@tonic-gate 		 */
875f4b3ec61Sdh 		sctp->sctp_cookie_lifetime =
876f4b3ec61Sdh 		    MSEC_TO_TICK(sctps->sctps_cookie_life);
877bd670b35SErik Nordmark 		connp->conn_sndlowat = sctps->sctps_xmit_lowat;
878bd670b35SErik Nordmark 		connp->conn_sndbuf = sctps->sctps_xmit_hiwat;
879bd670b35SErik Nordmark 		connp->conn_rcvbuf = sctps->sctps_recv_hiwat;
880bd670b35SErik Nordmark 
881f4b3ec61Sdh 		sctp->sctp_cwnd_max = sctps->sctps_cwnd_max_;
882bd670b35SErik Nordmark 		sctp->sctp_rwnd = connp->conn_rcvbuf;
883*a215d4ebSKacheong Poon 		sctp->sctp_arwnd = connp->conn_rcvbuf;
8847d546a59Svi 		sctp->sctp_pd_point = sctp->sctp_rwnd;
885f4b3ec61Sdh 		sctp->sctp_rto_max = MSEC_TO_TICK(sctps->sctps_rto_maxg);
886fd7b5aedSGeorge Shepherd 		sctp->sctp_rto_max_init = sctp->sctp_rto_max;
887f4b3ec61Sdh 		sctp->sctp_rto_min = MSEC_TO_TICK(sctps->sctps_rto_ming);
888f4b3ec61Sdh 		sctp->sctp_rto_initial = MSEC_TO_TICK(
889f4b3ec61Sdh 		    sctps->sctps_rto_initialg);
890f4b3ec61Sdh 		sctp->sctp_pa_max_rxt = sctps->sctps_pa_max_retr;
891f4b3ec61Sdh 		sctp->sctp_pp_max_rxt = sctps->sctps_pp_max_retr;
892f4b3ec61Sdh 		sctp->sctp_max_init_rxt = sctps->sctps_max_init_retr;
893f4b3ec61Sdh 
894f4b3ec61Sdh 		sctp->sctp_num_istr = sctps->sctps_max_in_streams;
895f4b3ec61Sdh 		sctp->sctp_num_ostr = sctps->sctps_initial_out_streams;
896f4b3ec61Sdh 
897f4b3ec61Sdh 		sctp->sctp_hb_interval =
898f4b3ec61Sdh 		    MSEC_TO_TICK(sctps->sctps_heartbeat_interval);
899bd670b35SErik Nordmark 
900bd670b35SErik Nordmark 		if (connp->conn_family == AF_INET)
901bd670b35SErik Nordmark 			connp->conn_default_ttl = sctps->sctps_ipv4_ttl;
902bd670b35SErik Nordmark 		else
903bd670b35SErik Nordmark 			connp->conn_default_ttl = sctps->sctps_ipv6_hoplimit;
904bd670b35SErik Nordmark 
905bd670b35SErik Nordmark 		connp->conn_xmit_ipp.ipp_unicast_hops =
906bd670b35SErik Nordmark 		    connp->conn_default_ttl;
907bd670b35SErik Nordmark 
908bd670b35SErik Nordmark 		/*
909bd670b35SErik Nordmark 		 * Initialize the header template
910bd670b35SErik Nordmark 		 */
911bd670b35SErik Nordmark 		if ((err = sctp_build_hdrs(sctp, sleep)) != 0) {
912bd670b35SErik Nordmark 			goto failure;
913bd670b35SErik Nordmark 		}
9147c478bd9Sstevel@tonic-gate 	}
915bd670b35SErik Nordmark 
9167c478bd9Sstevel@tonic-gate 	sctp->sctp_understands_asconf = B_TRUE;
9177c478bd9Sstevel@tonic-gate 	sctp->sctp_understands_addip = B_TRUE;
9187c478bd9Sstevel@tonic-gate 	sctp->sctp_prsctp_aware = B_FALSE;
9197c478bd9Sstevel@tonic-gate 
9207c478bd9Sstevel@tonic-gate 	sctp->sctp_connp->conn_ref = 1;
9217c478bd9Sstevel@tonic-gate 
9227c478bd9Sstevel@tonic-gate 	sctp->sctp_prsctpdrop = 0;
9237c478bd9Sstevel@tonic-gate 	sctp->sctp_msgcount = 0;
9247c478bd9Sstevel@tonic-gate 
9257c478bd9Sstevel@tonic-gate 	return (0);
926121e5416Skcpoon 
927121e5416Skcpoon failure:
928bd670b35SErik Nordmark 	sctp_headers_free(sctp);
929121e5416Skcpoon 	return (err);
9307c478bd9Sstevel@tonic-gate }
9317c478bd9Sstevel@tonic-gate 
9327c478bd9Sstevel@tonic-gate /*
9337c478bd9Sstevel@tonic-gate  * Extracts the init tag from an INIT chunk and checks if it matches
9347c478bd9Sstevel@tonic-gate  * the sctp's verification tag. Returns 0 if it doesn't match, 1 if
9357c478bd9Sstevel@tonic-gate  * it does.
9367c478bd9Sstevel@tonic-gate  */
9377c478bd9Sstevel@tonic-gate static boolean_t
9387c478bd9Sstevel@tonic-gate sctp_icmp_verf(sctp_t *sctp, sctp_hdr_t *sh, mblk_t *mp)
9397c478bd9Sstevel@tonic-gate {
9407c478bd9Sstevel@tonic-gate 	sctp_chunk_hdr_t *sch;
9417c478bd9Sstevel@tonic-gate 	uint32_t verf, *vp;
9427c478bd9Sstevel@tonic-gate 
9437c478bd9Sstevel@tonic-gate 	sch = (sctp_chunk_hdr_t *)(sh + 1);
9447c478bd9Sstevel@tonic-gate 	vp = (uint32_t *)(sch + 1);
9457c478bd9Sstevel@tonic-gate 
9467c478bd9Sstevel@tonic-gate 	/* Need at least the data chunk hdr and the first 4 bytes of INIT */
9477c478bd9Sstevel@tonic-gate 	if ((unsigned char *)(vp + 1) > mp->b_wptr) {
9487c478bd9Sstevel@tonic-gate 		return (B_FALSE);
9497c478bd9Sstevel@tonic-gate 	}
9507c478bd9Sstevel@tonic-gate 
9517c478bd9Sstevel@tonic-gate 	bcopy(vp, &verf, sizeof (verf));
9527c478bd9Sstevel@tonic-gate 
9537c478bd9Sstevel@tonic-gate 	if (verf == sctp->sctp_lvtag) {
9547c478bd9Sstevel@tonic-gate 		return (B_TRUE);
9557c478bd9Sstevel@tonic-gate 	}
9567c478bd9Sstevel@tonic-gate 	return (B_FALSE);
9577c478bd9Sstevel@tonic-gate }
9587c478bd9Sstevel@tonic-gate 
959bd670b35SErik Nordmark /*
960bd670b35SErik Nordmark  * Update the SCTP state according to change of PMTU.
961bd670b35SErik Nordmark  *
962bd670b35SErik Nordmark  * Path MTU might have changed by either increase or decrease, so need to
963bd670b35SErik Nordmark  * adjust the MSS based on the value of ixa_pmtu.
964bd670b35SErik Nordmark  */
965bd670b35SErik Nordmark static void
966bd670b35SErik Nordmark sctp_update_pmtu(sctp_t *sctp, sctp_faddr_t *fp, boolean_t decrease_only)
967bd670b35SErik Nordmark {
968bd670b35SErik Nordmark 	uint32_t	pmtu;
969bd670b35SErik Nordmark 	int32_t		mss;
9706be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 	ip_xmit_attr_t	*ixa = fp->sf_ixa;
971bd670b35SErik Nordmark 
972bd670b35SErik Nordmark 	if (sctp->sctp_state < SCTPS_ESTABLISHED)
973bd670b35SErik Nordmark 		return;
974bd670b35SErik Nordmark 
975bd670b35SErik Nordmark 	/*
976bd670b35SErik Nordmark 	 * Always call ip_get_pmtu() to make sure that IP has updated
977bd670b35SErik Nordmark 	 * ixa_flags properly.
978bd670b35SErik Nordmark 	 */
979bd670b35SErik Nordmark 	pmtu = ip_get_pmtu(ixa);
980bd670b35SErik Nordmark 
981bd670b35SErik Nordmark 	/*
982bd670b35SErik Nordmark 	 * Calculate the MSS by decreasing the PMTU by sctp_hdr_len and
983bd670b35SErik Nordmark 	 * IPsec overhead if applied. Make sure to use the most recent
984bd670b35SErik Nordmark 	 * IPsec information.
985bd670b35SErik Nordmark 	 */
986bd670b35SErik Nordmark 	mss = pmtu - conn_ipsec_length(sctp->sctp_connp);
987bd670b35SErik Nordmark 	if (ixa->ixa_flags & IXAF_IS_IPV4)
988bd670b35SErik Nordmark 		mss -= sctp->sctp_hdr_len;
989bd670b35SErik Nordmark 	else
990bd670b35SErik Nordmark 		mss -= sctp->sctp_hdr6_len;
991bd670b35SErik Nordmark 
992bd670b35SErik Nordmark 	/*
993bd670b35SErik Nordmark 	 * Nothing to change, so just return.
994bd670b35SErik Nordmark 	 */
9956be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 	if (mss == fp->sf_pmss)
996bd670b35SErik Nordmark 		return;
997bd670b35SErik Nordmark 
998bd670b35SErik Nordmark 	/*
999bd670b35SErik Nordmark 	 * Currently, for ICMP errors, only PMTU decrease is handled.
1000bd670b35SErik Nordmark 	 */
10016be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 	if (mss > fp->sf_pmss && decrease_only)
1002bd670b35SErik Nordmark 		return;
1003bd670b35SErik Nordmark 
1004bd670b35SErik Nordmark #ifdef DEBUG
1005bd670b35SErik Nordmark 	(void) printf("sctp_update_pmtu mss from %d to %d\n",
10066be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 	    fp->sf_pmss, mss);
1007bd670b35SErik Nordmark #endif
10086be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 	DTRACE_PROBE2(sctp_update_pmtu, int32_t, fp->sf_pmss, uint32_t, mss);
1009bd670b35SErik Nordmark 
1010bd670b35SErik Nordmark 	/*
1011bd670b35SErik Nordmark 	 * Update ixa_fragsize and ixa_pmtu.
1012bd670b35SErik Nordmark 	 */
1013bd670b35SErik Nordmark 	ixa->ixa_fragsize = ixa->ixa_pmtu = pmtu;
1014bd670b35SErik Nordmark 
1015bd670b35SErik Nordmark 	/*
1016bd670b35SErik Nordmark 	 * Make sure that sfa_pmss is a multiple of
1017bd670b35SErik Nordmark 	 * SCTP_ALIGN.
1018bd670b35SErik Nordmark 	 */
10196be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 	fp->sf_pmss = mss & ~(SCTP_ALIGN - 1);
10206be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 	fp->sf_pmtu_discovered = 1;
1021bd670b35SErik Nordmark 
1022bd670b35SErik Nordmark #ifdef notyet
1023bd670b35SErik Nordmark 	if (mss < sctp->sctp_sctps->sctps_mss_min)
1024bd670b35SErik Nordmark 		ixa->ixa_flags |= IXAF_PMTU_TOO_SMALL;
1025bd670b35SErik Nordmark #endif
1026bd670b35SErik Nordmark 	if (ixa->ixa_flags & IXAF_PMTU_TOO_SMALL)
1027bd670b35SErik Nordmark 		ixa->ixa_flags &= ~(IXAF_DONTFRAG | IXAF_PMTU_IPV4_DF);
1028bd670b35SErik Nordmark 
1029bd670b35SErik Nordmark 	/*
1030bd670b35SErik Nordmark 	 * If below the min size then ip_get_pmtu cleared IXAF_PMTU_IPV4_DF.
1031bd670b35SErik Nordmark 	 * Make sure to clear IXAF_DONTFRAG, which is used by IP to decide
1032bd670b35SErik Nordmark 	 * whether to fragment the packet.
1033bd670b35SErik Nordmark 	 */
1034bd670b35SErik Nordmark 	if (ixa->ixa_flags & IXAF_IS_IPV4) {
1035bd670b35SErik Nordmark 		if (!(ixa->ixa_flags & IXAF_PMTU_IPV4_DF)) {
10366be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 			fp->sf_df = B_FALSE;
1037bd670b35SErik Nordmark 			if (fp == sctp->sctp_current) {
1038bd670b35SErik Nordmark 				sctp->sctp_ipha->
1039bd670b35SErik Nordmark 				    ipha_fragment_offset_and_flags = 0;
1040bd670b35SErik Nordmark 			}
1041bd670b35SErik Nordmark 		}
1042bd670b35SErik Nordmark 	}
1043bd670b35SErik Nordmark }
1044bd670b35SErik Nordmark 
1045bd670b35SErik Nordmark /*
1046bd670b35SErik Nordmark  * Notify function registered with ip_xmit_attr_t. It's called in the context
1047bd670b35SErik Nordmark  * of conn_ip_output so it's safe to update the SCTP state.
1048bd670b35SErik Nordmark  * Currently only used for pmtu changes.
1049bd670b35SErik Nordmark  */
1050bd670b35SErik Nordmark /* ARGSUSED1 */
1051bd670b35SErik Nordmark static void
1052bd670b35SErik Nordmark sctp_notify(void *arg, ip_xmit_attr_t *ixa, ixa_notify_type_t ntype,
1053bd670b35SErik Nordmark     ixa_notify_arg_t narg)
1054bd670b35SErik Nordmark {
1055bd670b35SErik Nordmark 	sctp_t		*sctp = (sctp_t *)arg;
1056bd670b35SErik Nordmark 	sctp_faddr_t	*fp;
1057bd670b35SErik Nordmark 
1058bd670b35SErik Nordmark 	switch (ntype) {
1059bd670b35SErik Nordmark 	case IXAN_PMTU:
1060bd670b35SErik Nordmark 		/* Find the faddr based on the ip_xmit_attr_t pointer */
10616be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 		for (fp = sctp->sctp_faddrs; fp != NULL; fp = fp->sf_next) {
10626be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 			if (fp->sf_ixa == ixa)
1063bd670b35SErik Nordmark 				break;
1064bd670b35SErik Nordmark 		}
1065bd670b35SErik Nordmark 		if (fp != NULL)
1066bd670b35SErik Nordmark 			sctp_update_pmtu(sctp, fp, B_FALSE);
1067bd670b35SErik Nordmark 		break;
1068bd670b35SErik Nordmark 	default:
1069bd670b35SErik Nordmark 		break;
1070bd670b35SErik Nordmark 	}
1071bd670b35SErik Nordmark }
1072bd670b35SErik Nordmark 
10737c478bd9Sstevel@tonic-gate /*
10747c478bd9Sstevel@tonic-gate  * sctp_icmp_error is called by sctp_input() to process ICMP error messages
1075bd670b35SErik Nordmark  * passed up by IP.  We need to find a sctp_t
10767c478bd9Sstevel@tonic-gate  * that corresponds to the returned datagram.  Passes the message back in on
10777c478bd9Sstevel@tonic-gate  * the correct queue once it has located the connection.
10787c478bd9Sstevel@tonic-gate  * Assumes that IP has pulled up everything up to and including
10797c478bd9Sstevel@tonic-gate  * the ICMP header.
10807c478bd9Sstevel@tonic-gate  */
10817c478bd9Sstevel@tonic-gate void
10827c478bd9Sstevel@tonic-gate sctp_icmp_error(sctp_t *sctp, mblk_t *mp)
10837c478bd9Sstevel@tonic-gate {
10847c478bd9Sstevel@tonic-gate 	icmph_t *icmph;
10857c478bd9Sstevel@tonic-gate 	ipha_t	*ipha;
10867c478bd9Sstevel@tonic-gate 	int	iph_hdr_length;
10877c478bd9Sstevel@tonic-gate 	sctp_hdr_t *sctph;
10887c478bd9Sstevel@tonic-gate 	in6_addr_t dst;
10897c478bd9Sstevel@tonic-gate 	sctp_faddr_t *fp;
1090f4b3ec61Sdh 	sctp_stack_t	*sctps = sctp->sctp_sctps;
10917c478bd9Sstevel@tonic-gate 
109245916cd2Sjpk 	dprint(1, ("sctp_icmp_error: sctp=%p, mp=%p\n", (void *)sctp,
109345916cd2Sjpk 	    (void *)mp));
10947c478bd9Sstevel@tonic-gate 
10957c478bd9Sstevel@tonic-gate 	ipha = (ipha_t *)mp->b_rptr;
10967c478bd9Sstevel@tonic-gate 	if (IPH_HDR_VERSION(ipha) != IPV4_VERSION) {
10977c478bd9Sstevel@tonic-gate 		ASSERT(IPH_HDR_VERSION(ipha) == IPV6_VERSION);
1098bd670b35SErik Nordmark 		sctp_icmp_error_ipv6(sctp, mp);
10997c478bd9Sstevel@tonic-gate 		return;
11007c478bd9Sstevel@tonic-gate 	}
11017c478bd9Sstevel@tonic-gate 
1102f0c3911fSGeorge Shepherd 	/* account for the ip hdr from the icmp message */
11037c478bd9Sstevel@tonic-gate 	iph_hdr_length = IPH_HDR_LENGTH(ipha);
11047c478bd9Sstevel@tonic-gate 	icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length];
1105f0c3911fSGeorge Shepherd 	/* now the ip hdr of message resulting in this icmp */
11067c478bd9Sstevel@tonic-gate 	ipha = (ipha_t *)&icmph[1];
11077c478bd9Sstevel@tonic-gate 	iph_hdr_length = IPH_HDR_LENGTH(ipha);
11087c478bd9Sstevel@tonic-gate 	sctph = (sctp_hdr_t *)((char *)ipha + iph_hdr_length);
1109f0c3911fSGeorge Shepherd 	/* first_mp must expose the full sctp header. */
11107c478bd9Sstevel@tonic-gate 	if ((uchar_t *)(sctph + 1) >= mp->b_wptr) {
11117c478bd9Sstevel@tonic-gate 		/* not enough data for SCTP header */
1112bd670b35SErik Nordmark 		freemsg(mp);
11137c478bd9Sstevel@tonic-gate 		return;
11147c478bd9Sstevel@tonic-gate 	}
11157c478bd9Sstevel@tonic-gate 
11167c478bd9Sstevel@tonic-gate 	switch (icmph->icmph_type) {
11177c478bd9Sstevel@tonic-gate 	case ICMP_DEST_UNREACHABLE:
11187c478bd9Sstevel@tonic-gate 		switch (icmph->icmph_code) {
11197c478bd9Sstevel@tonic-gate 		case ICMP_FRAGMENTATION_NEEDED:
11207c478bd9Sstevel@tonic-gate 			/*
11217c478bd9Sstevel@tonic-gate 			 * Reduce the MSS based on the new MTU.  This will
11227c478bd9Sstevel@tonic-gate 			 * eliminate any fragmentation locally.
11237c478bd9Sstevel@tonic-gate 			 * N.B.  There may well be some funny side-effects on
11247c478bd9Sstevel@tonic-gate 			 * the local send policy and the remote receive policy.
11257c478bd9Sstevel@tonic-gate 			 * Pending further research, we provide
11267c478bd9Sstevel@tonic-gate 			 * sctp_ignore_path_mtu just in case this proves
11277c478bd9Sstevel@tonic-gate 			 * disastrous somewhere.
11287c478bd9Sstevel@tonic-gate 			 *
11297c478bd9Sstevel@tonic-gate 			 * After updating the MSS, retransmit part of the
11307c478bd9Sstevel@tonic-gate 			 * dropped segment using the new mss by calling
11317c478bd9Sstevel@tonic-gate 			 * sctp_wput_slow().  Need to adjust all those
11327c478bd9Sstevel@tonic-gate 			 * params to make sure sctp_wput_slow() work properly.
11337c478bd9Sstevel@tonic-gate 			 */
1134f4b3ec61Sdh 			if (sctps->sctps_ignore_path_mtu)
11357c478bd9Sstevel@tonic-gate 				break;
11367c478bd9Sstevel@tonic-gate 
11377c478bd9Sstevel@tonic-gate 			/* find the offending faddr */
11387c478bd9Sstevel@tonic-gate 			IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, &dst);
11397c478bd9Sstevel@tonic-gate 			fp = sctp_lookup_faddr(sctp, &dst);
11407c478bd9Sstevel@tonic-gate 			if (fp == NULL) {
11417c478bd9Sstevel@tonic-gate 				break;
11427c478bd9Sstevel@tonic-gate 			}
1143bd670b35SErik Nordmark 			sctp_update_pmtu(sctp, fp, B_TRUE);
1144f0c3911fSGeorge Shepherd 			/*
1145f0c3911fSGeorge Shepherd 			 * It is possible, even likely that a fast retransmit
1146f0c3911fSGeorge Shepherd 			 * attempt has been dropped by ip as a result of this
1147f0c3911fSGeorge Shepherd 			 * error, retransmission bundles as much as possible.
1148f0c3911fSGeorge Shepherd 			 * A retransmit here prevents significant delays waiting
1149f0c3911fSGeorge Shepherd 			 * on the timer. Analogous to behaviour of TCP after
1150f0c3911fSGeorge Shepherd 			 * ICMP too big.
1151f0c3911fSGeorge Shepherd 			 */
1152f0c3911fSGeorge Shepherd 			sctp_rexmit(sctp, fp);
11537c478bd9Sstevel@tonic-gate 			break;
11547c478bd9Sstevel@tonic-gate 		case ICMP_PORT_UNREACHABLE:
11557c478bd9Sstevel@tonic-gate 		case ICMP_PROTOCOL_UNREACHABLE:
11567c478bd9Sstevel@tonic-gate 			switch (sctp->sctp_state) {
11577c478bd9Sstevel@tonic-gate 			case SCTPS_COOKIE_WAIT:
11587c478bd9Sstevel@tonic-gate 			case SCTPS_COOKIE_ECHOED:
11597c478bd9Sstevel@tonic-gate 				/* make sure the verification tag matches */
11607c478bd9Sstevel@tonic-gate 				if (!sctp_icmp_verf(sctp, sctph, mp)) {
11617c478bd9Sstevel@tonic-gate 					break;
11627c478bd9Sstevel@tonic-gate 				}
11635dd46ab5SKacheong Poon 				SCTPS_BUMP_MIB(sctps, sctpAborted);
1164c9da23f8Skcpoon 				sctp_assoc_event(sctp, SCTP_CANT_STR_ASSOC, 0,
1165c9da23f8Skcpoon 				    NULL);
11667c478bd9Sstevel@tonic-gate 				sctp_clean_death(sctp, ECONNREFUSED);
11677c478bd9Sstevel@tonic-gate 				break;
11687c478bd9Sstevel@tonic-gate 			}
11697c478bd9Sstevel@tonic-gate 			break;
11707c478bd9Sstevel@tonic-gate 		case ICMP_HOST_UNREACHABLE:
11717c478bd9Sstevel@tonic-gate 		case ICMP_NET_UNREACHABLE:
11727c478bd9Sstevel@tonic-gate 			/* Record the error in case we finally time out. */
11737c478bd9Sstevel@tonic-gate 			sctp->sctp_client_errno = (icmph->icmph_code ==
11747c478bd9Sstevel@tonic-gate 			    ICMP_HOST_UNREACHABLE) ? EHOSTUNREACH : ENETUNREACH;
11757c478bd9Sstevel@tonic-gate 			break;
11767c478bd9Sstevel@tonic-gate 		default:
11777c478bd9Sstevel@tonic-gate 			break;
11787c478bd9Sstevel@tonic-gate 		}
11797c478bd9Sstevel@tonic-gate 		break;
11807c478bd9Sstevel@tonic-gate 	case ICMP_SOURCE_QUENCH: {
11817c478bd9Sstevel@tonic-gate 		/* Reduce the sending rate as if we got a retransmit timeout */
11827c478bd9Sstevel@tonic-gate 		break;
11837c478bd9Sstevel@tonic-gate 	}
11847c478bd9Sstevel@tonic-gate 	}
1185bd670b35SErik Nordmark 	freemsg(mp);
11867c478bd9Sstevel@tonic-gate }
11877c478bd9Sstevel@tonic-gate 
11887c478bd9Sstevel@tonic-gate /*
11897c478bd9Sstevel@tonic-gate  * sctp_icmp_error_ipv6() is called by sctp_icmp_error() to process ICMPv6
11907c478bd9Sstevel@tonic-gate  * error messages passed up by IP.
11917c478bd9Sstevel@tonic-gate  * Assumes that IP has pulled up all the extension headers as well
11927c478bd9Sstevel@tonic-gate  * as the ICMPv6 header.
11937c478bd9Sstevel@tonic-gate  */
11947c478bd9Sstevel@tonic-gate static void
11957c478bd9Sstevel@tonic-gate sctp_icmp_error_ipv6(sctp_t *sctp, mblk_t *mp)
11967c478bd9Sstevel@tonic-gate {
11977c478bd9Sstevel@tonic-gate 	icmp6_t *icmp6;
11987c478bd9Sstevel@tonic-gate 	ip6_t	*ip6h;
11997c478bd9Sstevel@tonic-gate 	uint16_t	iph_hdr_length;
12007c478bd9Sstevel@tonic-gate 	sctp_hdr_t *sctpha;
12017c478bd9Sstevel@tonic-gate 	uint8_t	*nexthdrp;
12027c478bd9Sstevel@tonic-gate 	sctp_faddr_t *fp;
1203f4b3ec61Sdh 	sctp_stack_t	*sctps = sctp->sctp_sctps;
12047c478bd9Sstevel@tonic-gate 
12057c478bd9Sstevel@tonic-gate 	ip6h = (ip6_t *)mp->b_rptr;
12067c478bd9Sstevel@tonic-gate 	iph_hdr_length = (ip6h->ip6_nxt != IPPROTO_SCTP) ?
12077c478bd9Sstevel@tonic-gate 	    ip_hdr_length_v6(mp, ip6h) : IPV6_HDR_LEN;
12087c478bd9Sstevel@tonic-gate 
12097c478bd9Sstevel@tonic-gate 	icmp6 = (icmp6_t *)&mp->b_rptr[iph_hdr_length];
12107c478bd9Sstevel@tonic-gate 	ip6h = (ip6_t *)&icmp6[1];
12117c478bd9Sstevel@tonic-gate 	if (!ip_hdr_length_nexthdr_v6(mp, ip6h, &iph_hdr_length, &nexthdrp)) {
12127c478bd9Sstevel@tonic-gate 		freemsg(mp);
12137c478bd9Sstevel@tonic-gate 		return;
12147c478bd9Sstevel@tonic-gate 	}
12157c478bd9Sstevel@tonic-gate 	ASSERT(*nexthdrp == IPPROTO_SCTP);
12167c478bd9Sstevel@tonic-gate 
12177c478bd9Sstevel@tonic-gate 	/* XXX need ifindex to find connection */
12187c478bd9Sstevel@tonic-gate 	sctpha = (sctp_hdr_t *)((char *)ip6h + iph_hdr_length);
12197c478bd9Sstevel@tonic-gate 	if ((uchar_t *)sctpha >= mp->b_wptr) {
12207c478bd9Sstevel@tonic-gate 		/* not enough data for SCTP header */
12217c478bd9Sstevel@tonic-gate 		freemsg(mp);
12227c478bd9Sstevel@tonic-gate 		return;
12237c478bd9Sstevel@tonic-gate 	}
12247c478bd9Sstevel@tonic-gate 	switch (icmp6->icmp6_type) {
12257c478bd9Sstevel@tonic-gate 	case ICMP6_PACKET_TOO_BIG:
12267c478bd9Sstevel@tonic-gate 		/*
12277c478bd9Sstevel@tonic-gate 		 * Reduce the MSS based on the new MTU.  This will
12287c478bd9Sstevel@tonic-gate 		 * eliminate any fragmentation locally.
12297c478bd9Sstevel@tonic-gate 		 * N.B.  There may well be some funny side-effects on
12307c478bd9Sstevel@tonic-gate 		 * the local send policy and the remote receive policy.
12317c478bd9Sstevel@tonic-gate 		 * Pending further research, we provide
12327c478bd9Sstevel@tonic-gate 		 * sctp_ignore_path_mtu just in case this proves
12337c478bd9Sstevel@tonic-gate 		 * disastrous somewhere.
12347c478bd9Sstevel@tonic-gate 		 *
12357c478bd9Sstevel@tonic-gate 		 * After updating the MSS, retransmit part of the
12367c478bd9Sstevel@tonic-gate 		 * dropped segment using the new mss by calling
12377c478bd9Sstevel@tonic-gate 		 * sctp_wput_slow().  Need to adjust all those
12387c478bd9Sstevel@tonic-gate 		 * params to make sure sctp_wput_slow() work properly.
12397c478bd9Sstevel@tonic-gate 		 */
1240f4b3ec61Sdh 		if (sctps->sctps_ignore_path_mtu)
12417c478bd9Sstevel@tonic-gate 			break;
12427c478bd9Sstevel@tonic-gate 
12437c478bd9Sstevel@tonic-gate 		/* find the offending faddr */
12447c478bd9Sstevel@tonic-gate 		fp = sctp_lookup_faddr(sctp, &ip6h->ip6_dst);
12457c478bd9Sstevel@tonic-gate 		if (fp == NULL) {
12467c478bd9Sstevel@tonic-gate 			break;
12477c478bd9Sstevel@tonic-gate 		}
12487c478bd9Sstevel@tonic-gate 
1249bd670b35SErik Nordmark 		sctp_update_pmtu(sctp, fp, B_TRUE);
1250bd670b35SErik Nordmark 		/*
1251bd670b35SErik Nordmark 		 * It is possible, even likely that a fast retransmit
1252bd670b35SErik Nordmark 		 * attempt has been dropped by ip as a result of this
1253bd670b35SErik Nordmark 		 * error, retransmission bundles as much as possible.
1254bd670b35SErik Nordmark 		 * A retransmit here prevents significant delays waiting
1255bd670b35SErik Nordmark 		 * on the timer. Analogous to behaviour of TCP after
1256bd670b35SErik Nordmark 		 * ICMP too big.
1257bd670b35SErik Nordmark 		 */
1258bd670b35SErik Nordmark 		sctp_rexmit(sctp, fp);
12597c478bd9Sstevel@tonic-gate 		break;
12607c478bd9Sstevel@tonic-gate 
12617c478bd9Sstevel@tonic-gate 	case ICMP6_DST_UNREACH:
12627c478bd9Sstevel@tonic-gate 		switch (icmp6->icmp6_code) {
12637c478bd9Sstevel@tonic-gate 		case ICMP6_DST_UNREACH_NOPORT:
12647c478bd9Sstevel@tonic-gate 			/* make sure the verification tag matches */
12657c478bd9Sstevel@tonic-gate 			if (!sctp_icmp_verf(sctp, sctpha, mp)) {
12667c478bd9Sstevel@tonic-gate 				break;
12677c478bd9Sstevel@tonic-gate 			}
12687c478bd9Sstevel@tonic-gate 			if (sctp->sctp_state == SCTPS_COOKIE_WAIT ||
12697c478bd9Sstevel@tonic-gate 			    sctp->sctp_state == SCTPS_COOKIE_ECHOED) {
12705dd46ab5SKacheong Poon 				SCTPS_BUMP_MIB(sctps, sctpAborted);
1271c9da23f8Skcpoon 				sctp_assoc_event(sctp, SCTP_CANT_STR_ASSOC, 0,
1272c9da23f8Skcpoon 				    NULL);
12737c478bd9Sstevel@tonic-gate 				sctp_clean_death(sctp, ECONNREFUSED);
12747c478bd9Sstevel@tonic-gate 			}
12757c478bd9Sstevel@tonic-gate 			break;
12767c478bd9Sstevel@tonic-gate 
12777c478bd9Sstevel@tonic-gate 		case ICMP6_DST_UNREACH_ADMIN:
12787c478bd9Sstevel@tonic-gate 		case ICMP6_DST_UNREACH_NOROUTE:
12797c478bd9Sstevel@tonic-gate 		case ICMP6_DST_UNREACH_NOTNEIGHBOR:
12807c478bd9Sstevel@tonic-gate 		case ICMP6_DST_UNREACH_ADDR:
12817c478bd9Sstevel@tonic-gate 			/* Record the error in case we finally time out. */
12827c478bd9Sstevel@tonic-gate 			sctp->sctp_client_errno = EHOSTUNREACH;
12837c478bd9Sstevel@tonic-gate 			break;
12847c478bd9Sstevel@tonic-gate 		default:
12857c478bd9Sstevel@tonic-gate 			break;
12867c478bd9Sstevel@tonic-gate 		}
12877c478bd9Sstevel@tonic-gate 		break;
12887c478bd9Sstevel@tonic-gate 
12897c478bd9Sstevel@tonic-gate 	case ICMP6_PARAM_PROB:
12907c478bd9Sstevel@tonic-gate 		/* If this corresponds to an ICMP_PROTOCOL_UNREACHABLE */
12917c478bd9Sstevel@tonic-gate 		if (icmp6->icmp6_code == ICMP6_PARAMPROB_NEXTHEADER &&
12927c478bd9Sstevel@tonic-gate 		    (uchar_t *)ip6h + icmp6->icmp6_pptr ==
12937c478bd9Sstevel@tonic-gate 		    (uchar_t *)nexthdrp) {
12947c478bd9Sstevel@tonic-gate 			/* make sure the verification tag matches */
12957c478bd9Sstevel@tonic-gate 			if (!sctp_icmp_verf(sctp, sctpha, mp)) {
12967c478bd9Sstevel@tonic-gate 				break;
12977c478bd9Sstevel@tonic-gate 			}
12987c478bd9Sstevel@tonic-gate 			if (sctp->sctp_state == SCTPS_COOKIE_WAIT) {
12995dd46ab5SKacheong Poon 				SCTPS_BUMP_MIB(sctps, sctpAborted);
1300c9da23f8Skcpoon 				sctp_assoc_event(sctp, SCTP_CANT_STR_ASSOC, 0,
1301c9da23f8Skcpoon 				    NULL);
13027c478bd9Sstevel@tonic-gate 				sctp_clean_death(sctp, ECONNREFUSED);
13037c478bd9Sstevel@tonic-gate 			}
13047c478bd9Sstevel@tonic-gate 			break;
13057c478bd9Sstevel@tonic-gate 		}
13067c478bd9Sstevel@tonic-gate 		break;
13077c478bd9Sstevel@tonic-gate 
13087c478bd9Sstevel@tonic-gate 	case ICMP6_TIME_EXCEEDED:
13097c478bd9Sstevel@tonic-gate 	default:
13107c478bd9Sstevel@tonic-gate 		break;
13117c478bd9Sstevel@tonic-gate 	}
13127c478bd9Sstevel@tonic-gate 	freemsg(mp);
13137c478bd9Sstevel@tonic-gate }
13147c478bd9Sstevel@tonic-gate 
13157c478bd9Sstevel@tonic-gate /*
13167c478bd9Sstevel@tonic-gate  * Called by sockfs to create a new sctp instance.
13177c478bd9Sstevel@tonic-gate  *
13187c478bd9Sstevel@tonic-gate  * If parent pointer is passed in, inherit settings from it.
13197c478bd9Sstevel@tonic-gate  */
13207c478bd9Sstevel@tonic-gate sctp_t *
1321bd670b35SErik Nordmark sctp_create(void *ulpd, sctp_t *parent, int family, int type, int flags,
13220f1702c5SYu Xiangning     sock_upcalls_t *upcalls, sctp_sockbuf_limits_t *sbl,
13237c478bd9Sstevel@tonic-gate     cred_t *credp)
13247c478bd9Sstevel@tonic-gate {
13257c478bd9Sstevel@tonic-gate 	sctp_t		*sctp, *psctp;
1326bd670b35SErik Nordmark 	conn_t		*connp;
13277c478bd9Sstevel@tonic-gate 	mblk_t		*ack_mp, *hb_mp;
13287c478bd9Sstevel@tonic-gate 	int		sleep = flags & SCTP_CAN_BLOCK ? KM_SLEEP : KM_NOSLEEP;
1329f4b3ec61Sdh 	zoneid_t	zoneid;
1330f4b3ec61Sdh 	sctp_stack_t	*sctps;
13317c478bd9Sstevel@tonic-gate 
13327c478bd9Sstevel@tonic-gate 	/* User must supply a credential. */
13337c478bd9Sstevel@tonic-gate 	if (credp == NULL)
13347c478bd9Sstevel@tonic-gate 		return (NULL);
13357c478bd9Sstevel@tonic-gate 
13367c478bd9Sstevel@tonic-gate 	psctp = (sctp_t *)parent;
1337f4b3ec61Sdh 	if (psctp != NULL) {
1338f4b3ec61Sdh 		sctps = psctp->sctp_sctps;
1339f4b3ec61Sdh 		/* Increase here to have common decrease at end */
1340f4b3ec61Sdh 		netstack_hold(sctps->sctps_netstack);
13415dd46ab5SKacheong Poon 		ASSERT(sctps->sctps_recvq_tq_list_cur_sz > 0);
1342f4b3ec61Sdh 	} else {
1343f4b3ec61Sdh 		netstack_t *ns;
1344f4b3ec61Sdh 
1345f4b3ec61Sdh 		ns = netstack_find_by_cred(credp);
1346f4b3ec61Sdh 		sctps = ns->netstack_sctp;
13475dd46ab5SKacheong Poon 		/*
13485dd46ab5SKacheong Poon 		 * Check if the receive queue taskq for this sctp_stack_t has
13495dd46ab5SKacheong Poon 		 * been set up.
13505dd46ab5SKacheong Poon 		 */
13515dd46ab5SKacheong Poon 		if (sctps->sctps_recvq_tq_list_cur_sz == 0)
13525dd46ab5SKacheong Poon 			sctp_rq_tq_init(sctps);
13537c478bd9Sstevel@tonic-gate 
1354f4b3ec61Sdh 		/*
1355f4b3ec61Sdh 		 * For exclusive stacks we set the zoneid to zero
1356f4b3ec61Sdh 		 * to make SCTP operate as if in the global zone.
1357f4b3ec61Sdh 		 */
1358f4b3ec61Sdh 		if (sctps->sctps_netstack->netstack_stackid !=
1359f4b3ec61Sdh 		    GLOBAL_NETSTACKID)
1360f4b3ec61Sdh 			zoneid = GLOBAL_ZONEID;
1361f4b3ec61Sdh 		else
1362f4b3ec61Sdh 			zoneid = crgetzoneid(credp);
1363f4b3ec61Sdh 	}
1364bd670b35SErik Nordmark 	if ((connp = ipcl_conn_create(IPCL_SCTPCONN, sleep,
1365b34b8d1aSkcpoon 	    sctps->sctps_netstack)) == NULL) {
1366f4b3ec61Sdh 		netstack_rele(sctps->sctps_netstack);
1367f4b3ec61Sdh 		SCTP_KSTAT(sctps, sctp_conn_create);
1368f4b3ec61Sdh 		return (NULL);
1369f4b3ec61Sdh 	}
1370f4b3ec61Sdh 	/*
1371f4b3ec61Sdh 	 * ipcl_conn_create did a netstack_hold. Undo the hold that was
1372f4b3ec61Sdh 	 * done at top of sctp_create.
1373f4b3ec61Sdh 	 */
1374f4b3ec61Sdh 	netstack_rele(sctps->sctps_netstack);
1375bd670b35SErik Nordmark 	sctp = CONN2SCTP(connp);
1376f4b3ec61Sdh 	sctp->sctp_sctps = sctps;
13777c478bd9Sstevel@tonic-gate 
1378121e5416Skcpoon 	if ((ack_mp = sctp_timer_alloc(sctp, sctp_ack_timer, sleep)) == NULL ||
1379121e5416Skcpoon 	    (hb_mp = sctp_timer_alloc(sctp, sctp_heartbeat_timer,
1380121e5416Skcpoon 	    sleep)) == NULL) {
13817c478bd9Sstevel@tonic-gate 		if (ack_mp != NULL)
13827c478bd9Sstevel@tonic-gate 			freeb(ack_mp);
1383bd670b35SErik Nordmark 		sctp_conn_clear(connp);
1384f4b3ec61Sdh 		sctp->sctp_sctps = NULL;
1385bd670b35SErik Nordmark 		kmem_cache_free(sctp_conn_cache, connp);
13867c478bd9Sstevel@tonic-gate 		return (NULL);
13877c478bd9Sstevel@tonic-gate 	}
13887c478bd9Sstevel@tonic-gate 
13897c478bd9Sstevel@tonic-gate 	sctp->sctp_ack_mp = ack_mp;
13907c478bd9Sstevel@tonic-gate 	sctp->sctp_heartbeat_mp = hb_mp;
13917c478bd9Sstevel@tonic-gate 
1392bd670b35SErik Nordmark 	/*
1393bd670b35SErik Nordmark 	 * Have conn_ip_output drop packets should our outer source
1394bd670b35SErik Nordmark 	 * go invalid, and tell us about mtu changes.
1395bd670b35SErik Nordmark 	 */
1396bd670b35SErik Nordmark 	connp->conn_ixa->ixa_flags |= IXAF_SET_ULP_CKSUM | IXAF_VERIFY_SOURCE |
1397bd670b35SErik Nordmark 	    IXAF_VERIFY_PMTU;
1398bd670b35SErik Nordmark 	connp->conn_family = family;
1399bd670b35SErik Nordmark 	connp->conn_so_type = type;
14007c478bd9Sstevel@tonic-gate 
14017c478bd9Sstevel@tonic-gate 	if (sctp_init_values(sctp, psctp, sleep) != 0) {
14027c478bd9Sstevel@tonic-gate 		freeb(ack_mp);
14037c478bd9Sstevel@tonic-gate 		freeb(hb_mp);
1404bd670b35SErik Nordmark 		sctp_conn_clear(connp);
1405f4b3ec61Sdh 		sctp->sctp_sctps = NULL;
1406bd670b35SErik Nordmark 		kmem_cache_free(sctp_conn_cache, connp);
14077c478bd9Sstevel@tonic-gate 		return (NULL);
14087c478bd9Sstevel@tonic-gate 	}
14097c478bd9Sstevel@tonic-gate 	sctp->sctp_cansleep = ((flags & SCTP_CAN_BLOCK) == SCTP_CAN_BLOCK);
14107c478bd9Sstevel@tonic-gate 
1411f4b3ec61Sdh 	sctp->sctp_mss = sctps->sctps_initial_mtu - ((family == AF_INET6) ?
1412b34b8d1aSkcpoon 	    sctp->sctp_hdr6_len : sctp->sctp_hdr_len);
14137c478bd9Sstevel@tonic-gate 
14147c478bd9Sstevel@tonic-gate 	if (psctp != NULL) {
1415bd670b35SErik Nordmark 		conn_t	*pconnp = psctp->sctp_connp;
1416bd670b35SErik Nordmark 
14177c478bd9Sstevel@tonic-gate 		RUN_SCTP(psctp);
14187c478bd9Sstevel@tonic-gate 		/*
14197c478bd9Sstevel@tonic-gate 		 * Inherit local address list, local port. Parent is either
14207c478bd9Sstevel@tonic-gate 		 * in SCTPS_BOUND, or SCTPS_LISTEN state.
14217c478bd9Sstevel@tonic-gate 		 */
14227c478bd9Sstevel@tonic-gate 		ASSERT((psctp->sctp_state == SCTPS_BOUND) ||
14237c478bd9Sstevel@tonic-gate 		    (psctp->sctp_state == SCTPS_LISTEN));
14247c478bd9Sstevel@tonic-gate 		if (sctp_dup_saddrs(psctp, sctp, sleep)) {
14257c478bd9Sstevel@tonic-gate 			WAKE_SCTP(psctp);
14267c478bd9Sstevel@tonic-gate 			freeb(ack_mp);
14277c478bd9Sstevel@tonic-gate 			freeb(hb_mp);
14287c478bd9Sstevel@tonic-gate 			sctp_headers_free(sctp);
1429bd670b35SErik Nordmark 			sctp_conn_clear(connp);
1430f4b3ec61Sdh 			sctp->sctp_sctps = NULL;
1431bd670b35SErik Nordmark 			kmem_cache_free(sctp_conn_cache, connp);
14327c478bd9Sstevel@tonic-gate 			return (NULL);
14337c478bd9Sstevel@tonic-gate 		}
14347c478bd9Sstevel@tonic-gate 
14357c478bd9Sstevel@tonic-gate 		/*
14367c478bd9Sstevel@tonic-gate 		 * If the parent is specified, it'll be immediatelly
14377c478bd9Sstevel@tonic-gate 		 * followed by sctp_connect(). So don't add this guy to
14387c478bd9Sstevel@tonic-gate 		 * bind hash.
14397c478bd9Sstevel@tonic-gate 		 */
1440bd670b35SErik Nordmark 		connp->conn_lport = pconnp->conn_lport;
14417c478bd9Sstevel@tonic-gate 		sctp->sctp_state = SCTPS_BOUND;
14427c478bd9Sstevel@tonic-gate 		WAKE_SCTP(psctp);
14437c478bd9Sstevel@tonic-gate 	} else {
1444bd670b35SErik Nordmark 		ASSERT(connp->conn_cred == NULL);
1445bd670b35SErik Nordmark 		connp->conn_zoneid = zoneid;
1446bd670b35SErik Nordmark 		/*
1447bd670b35SErik Nordmark 		 * conn_allzones can not be set this early, hence
1448bd670b35SErik Nordmark 		 * no IPCL_ZONEID
1449bd670b35SErik Nordmark 		 */
1450bd670b35SErik Nordmark 		connp->conn_ixa->ixa_zoneid = zoneid;
1451d3d50737SRafael Vanoni 		connp->conn_open_time = ddi_get_lbolt64();
1452bd670b35SErik Nordmark 		connp->conn_cred = credp;
1453bd670b35SErik Nordmark 		crhold(credp);
1454bd670b35SErik Nordmark 		connp->conn_cpid = curproc->p_pid;
1455de8c4a14SErik Nordmark 
1456bd670b35SErik Nordmark 		/*
1457bd670b35SErik Nordmark 		 * If the caller has the process-wide flag set, then default to
1458bd670b35SErik Nordmark 		 * MAC exempt mode.  This allows read-down to unlabeled hosts.
1459bd670b35SErik Nordmark 		 */
1460bd670b35SErik Nordmark 		if (getpflags(NET_MAC_AWARE, credp) != 0)
1461bd670b35SErik Nordmark 			connp->conn_mac_mode = CONN_MAC_AWARE;
14627c478bd9Sstevel@tonic-gate 
1463bd670b35SErik Nordmark 		connp->conn_zone_is_global =
1464bd670b35SErik Nordmark 		    (crgetzoneid(credp) == GLOBAL_ZONEID);
1465bd670b35SErik Nordmark 	}
146645916cd2Sjpk 
14677c478bd9Sstevel@tonic-gate 	/* Initialize SCTP instance values,  our verf tag must never be 0 */
14687c478bd9Sstevel@tonic-gate 	(void) random_get_pseudo_bytes((uint8_t *)&sctp->sctp_lvtag,
14697c478bd9Sstevel@tonic-gate 	    sizeof (sctp->sctp_lvtag));
14707c478bd9Sstevel@tonic-gate 	if (sctp->sctp_lvtag == 0)
14717c478bd9Sstevel@tonic-gate 		sctp->sctp_lvtag = (uint32_t)gethrtime();
14727c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_lvtag != 0);
14737c478bd9Sstevel@tonic-gate 
14747c478bd9Sstevel@tonic-gate 	sctp->sctp_ltsn = sctp->sctp_lvtag + 1;
14757c478bd9Sstevel@tonic-gate 	sctp->sctp_lcsn = sctp->sctp_ltsn;
14767c478bd9Sstevel@tonic-gate 	sctp->sctp_recovery_tsn = sctp->sctp_lastack_rxd = sctp->sctp_ltsn - 1;
14777c478bd9Sstevel@tonic-gate 	sctp->sctp_adv_pap = sctp->sctp_lastack_rxd;
14787c478bd9Sstevel@tonic-gate 
14797c478bd9Sstevel@tonic-gate 	/* Information required by upper layer */
1480bd670b35SErik Nordmark 	ASSERT(ulpd != NULL);
1481bd670b35SErik Nordmark 	sctp->sctp_ulpd = ulpd;
1482bd670b35SErik Nordmark 
1483bd670b35SErik Nordmark 	ASSERT(upcalls != NULL);
1484bd670b35SErik Nordmark 	sctp->sctp_upcalls = upcalls;
1485bd670b35SErik Nordmark 	ASSERT(sbl != NULL);
1486bd670b35SErik Nordmark 	/* Fill in the socket buffer limits for sctpsockfs */
1487bd670b35SErik Nordmark 	sbl->sbl_txlowat = connp->conn_sndlowat;
1488bd670b35SErik Nordmark 	sbl->sbl_txbuf = connp->conn_sndbuf;
1489bd670b35SErik Nordmark 	sbl->sbl_rxbuf = sctp->sctp_rwnd;
1490bd670b35SErik Nordmark 	sbl->sbl_rxlowat = SCTP_RECV_LOWATER;
14917c478bd9Sstevel@tonic-gate 
14927c478bd9Sstevel@tonic-gate 	/* Insert this in the global list. */
1493f4b3ec61Sdh 	SCTP_LINK(sctp, sctps);
14947c478bd9Sstevel@tonic-gate 
14957c478bd9Sstevel@tonic-gate 	return (sctp);
14967c478bd9Sstevel@tonic-gate }
14977c478bd9Sstevel@tonic-gate 
1498f4b3ec61Sdh /* Run at module load time */
1499f4b3ec61Sdh void
1500f4b3ec61Sdh sctp_ddi_g_init(void)
1501f4b3ec61Sdh {
15027c478bd9Sstevel@tonic-gate 	/* Create sctp_t/conn_t cache */
15037c478bd9Sstevel@tonic-gate 	sctp_conn_cache_init();
15047c478bd9Sstevel@tonic-gate 
15057c478bd9Sstevel@tonic-gate 	/* Create the faddr cache */
15067c478bd9Sstevel@tonic-gate 	sctp_faddr_init();
15077c478bd9Sstevel@tonic-gate 
15087c478bd9Sstevel@tonic-gate 	/* Create the sets cache */
15097c478bd9Sstevel@tonic-gate 	sctp_sets_init();
15107c478bd9Sstevel@tonic-gate 
15117c478bd9Sstevel@tonic-gate 	/* Create the PR-SCTP sets cache */
15127c478bd9Sstevel@tonic-gate 	sctp_ftsn_sets_init();
15137c478bd9Sstevel@tonic-gate 
1514f4b3ec61Sdh 	/* Initialize tables used for CRC calculation */
1515f4b3ec61Sdh 	sctp_crc32_init();
1516f4b3ec61Sdh 
1517f4b3ec61Sdh 	/*
1518f4b3ec61Sdh 	 * We want to be informed each time a stack is created or
1519f4b3ec61Sdh 	 * destroyed in the kernel, so we can maintain the
1520f4b3ec61Sdh 	 * set of sctp_stack_t's.
1521f4b3ec61Sdh 	 */
1522bd670b35SErik Nordmark 	netstack_register(NS_SCTP, sctp_stack_init, NULL, sctp_stack_fini);
1523f4b3ec61Sdh }
1524f4b3ec61Sdh 
1525f4b3ec61Sdh static void *
1526f4b3ec61Sdh sctp_stack_init(netstackid_t stackid, netstack_t *ns)
1527f4b3ec61Sdh {
1528f4b3ec61Sdh 	sctp_stack_t	*sctps;
15296e91bba0SGirish Moodalbail 	size_t		arrsz;
15305dd46ab5SKacheong Poon 	int		i;
1531f4b3ec61Sdh 
1532f4b3ec61Sdh 	sctps = kmem_zalloc(sizeof (*sctps), KM_SLEEP);
1533f4b3ec61Sdh 	sctps->sctps_netstack = ns;
1534f4b3ec61Sdh 
1535f4b3ec61Sdh 	/* Initialize locks */
1536f4b3ec61Sdh 	mutex_init(&sctps->sctps_g_lock, NULL, MUTEX_DEFAULT, NULL);
1537f4b3ec61Sdh 	mutex_init(&sctps->sctps_epriv_port_lock, NULL, MUTEX_DEFAULT, NULL);
1538f4b3ec61Sdh 	sctps->sctps_g_num_epriv_ports = SCTP_NUM_EPRIV_PORTS;
15396e91bba0SGirish Moodalbail 	sctps->sctps_g_epriv_ports[0] = ULP_DEF_EPRIV_PORT1;
15406e91bba0SGirish Moodalbail 	sctps->sctps_g_epriv_ports[1] = ULP_DEF_EPRIV_PORT2;
1541f4b3ec61Sdh 
1542f4b3ec61Sdh 	/* Initialize SCTP hash arrays. */
1543f4b3ec61Sdh 	sctp_hash_init(sctps);
1544f4b3ec61Sdh 
15456e91bba0SGirish Moodalbail 	arrsz = sctp_propinfo_count * sizeof (mod_prop_info_t);
15466e91bba0SGirish Moodalbail 	sctps->sctps_propinfo_tbl = (mod_prop_info_t *)kmem_alloc(arrsz,
15476e91bba0SGirish Moodalbail 	    KM_SLEEP);
15486e91bba0SGirish Moodalbail 	bcopy(sctp_propinfo_tbl, sctps->sctps_propinfo_tbl, arrsz);
1549f4b3ec61Sdh 
15507c478bd9Sstevel@tonic-gate 	/* saddr init */
1551f4b3ec61Sdh 	sctp_saddr_init(sctps);
15527c478bd9Sstevel@tonic-gate 
15537c478bd9Sstevel@tonic-gate 	/* Global SCTP PCB list. */
1554f4b3ec61Sdh 	list_create(&sctps->sctps_g_list, sizeof (sctp_t),
15557c478bd9Sstevel@tonic-gate 	    offsetof(sctp_t, sctp_list));
15567c478bd9Sstevel@tonic-gate 
15575dd46ab5SKacheong Poon 	/* Initialize SCTP kstats. */
1558f4b3ec61Sdh 	sctps->sctps_mibkp = sctp_kstat_init(stackid);
15595dd46ab5SKacheong Poon 	sctps->sctps_kstat = sctp_kstat2_init(stackid);
15605dd46ab5SKacheong Poon 
15615dd46ab5SKacheong Poon 	mutex_init(&sctps->sctps_reclaim_lock, NULL, MUTEX_DEFAULT, NULL);
15625dd46ab5SKacheong Poon 	sctps->sctps_reclaim = B_FALSE;
15635dd46ab5SKacheong Poon 	sctps->sctps_reclaim_tid = 0;
15645dd46ab5SKacheong Poon 	sctps->sctps_reclaim_period = sctps->sctps_rto_maxg;
15655dd46ab5SKacheong Poon 
15665dd46ab5SKacheong Poon 	/* Allocate the per netstack stats */
15675dd46ab5SKacheong Poon 	mutex_enter(&cpu_lock);
15685dd46ab5SKacheong Poon 	sctps->sctps_sc_cnt = MAX(ncpus, boot_ncpus);
15695dd46ab5SKacheong Poon 	mutex_exit(&cpu_lock);
15705dd46ab5SKacheong Poon 	sctps->sctps_sc = kmem_zalloc(max_ncpus  * sizeof (sctp_stats_cpu_t *),
15715dd46ab5SKacheong Poon 	    KM_SLEEP);
15725dd46ab5SKacheong Poon 	for (i = 0; i < sctps->sctps_sc_cnt; i++) {
15735dd46ab5SKacheong Poon 		sctps->sctps_sc[i] = kmem_zalloc(sizeof (sctp_stats_cpu_t),
15745dd46ab5SKacheong Poon 		    KM_SLEEP);
15755dd46ab5SKacheong Poon 	}
15765dd46ab5SKacheong Poon 
15775dd46ab5SKacheong Poon 	mutex_init(&sctps->sctps_listener_conf_lock, NULL, MUTEX_DEFAULT, NULL);
15785dd46ab5SKacheong Poon 	list_create(&sctps->sctps_listener_conf, sizeof (sctp_listener_t),
15795dd46ab5SKacheong Poon 	    offsetof(sctp_listener_t, sl_link));
1580f4b3ec61Sdh 
1581f4b3ec61Sdh 	return (sctps);
15827c478bd9Sstevel@tonic-gate }
15837c478bd9Sstevel@tonic-gate 
1584f4b3ec61Sdh /*
1585f4b3ec61Sdh  * Called when the module is about to be unloaded.
1586f4b3ec61Sdh  */
15877c478bd9Sstevel@tonic-gate void
1588f4b3ec61Sdh sctp_ddi_g_destroy(void)
15897c478bd9Sstevel@tonic-gate {
15907c478bd9Sstevel@tonic-gate 	/* Destroy sctp_t/conn_t caches */
15917c478bd9Sstevel@tonic-gate 	sctp_conn_cache_fini();
15927c478bd9Sstevel@tonic-gate 
15937c478bd9Sstevel@tonic-gate 	/* Destroy the faddr cache */
15947c478bd9Sstevel@tonic-gate 	sctp_faddr_fini();
15957c478bd9Sstevel@tonic-gate 
15967c478bd9Sstevel@tonic-gate 	/* Destroy the sets cache */
15977c478bd9Sstevel@tonic-gate 	sctp_sets_fini();
15987c478bd9Sstevel@tonic-gate 
15997c478bd9Sstevel@tonic-gate 	/* Destroy the PR-SCTP sets cache */
16007c478bd9Sstevel@tonic-gate 	sctp_ftsn_sets_fini();
16017c478bd9Sstevel@tonic-gate 
1602f4b3ec61Sdh 	netstack_unregister(NS_SCTP);
1603f4b3ec61Sdh }
1604f4b3ec61Sdh 
1605f4b3ec61Sdh /*
1606f4b3ec61Sdh  * Free the SCTP stack instance.
1607f4b3ec61Sdh  */
1608f4b3ec61Sdh static void
1609f4b3ec61Sdh sctp_stack_fini(netstackid_t stackid, void *arg)
1610f4b3ec61Sdh {
1611f4b3ec61Sdh 	sctp_stack_t *sctps = (sctp_stack_t *)arg;
16125dd46ab5SKacheong Poon 	int i;
16135dd46ab5SKacheong Poon 
16145dd46ab5SKacheong Poon 	/*
16155dd46ab5SKacheong Poon 	 * Set sctps_reclaim to false tells sctp_reclaim_timer() not to restart
16165dd46ab5SKacheong Poon 	 * the timer.
16175dd46ab5SKacheong Poon 	 */
16185dd46ab5SKacheong Poon 	mutex_enter(&sctps->sctps_reclaim_lock);
16195dd46ab5SKacheong Poon 	sctps->sctps_reclaim = B_FALSE;
16205dd46ab5SKacheong Poon 	mutex_exit(&sctps->sctps_reclaim_lock);
16215dd46ab5SKacheong Poon 	if (sctps->sctps_reclaim_tid != 0)
16225dd46ab5SKacheong Poon 		(void) untimeout(sctps->sctps_reclaim_tid);
16235dd46ab5SKacheong Poon 	mutex_destroy(&sctps->sctps_reclaim_lock);
16245dd46ab5SKacheong Poon 
16255dd46ab5SKacheong Poon 	sctp_listener_conf_cleanup(sctps);
1626f4b3ec61Sdh 
16276e91bba0SGirish Moodalbail 	kmem_free(sctps->sctps_propinfo_tbl,
16286e91bba0SGirish Moodalbail 	    sctp_propinfo_count * sizeof (mod_prop_info_t));
16296e91bba0SGirish Moodalbail 	sctps->sctps_propinfo_tbl = NULL;
1630f4b3ec61Sdh 
16317c478bd9Sstevel@tonic-gate 	/* Destroy the recvq taskqs. */
1632f4b3ec61Sdh 	sctp_rq_tq_fini(sctps);
16337c478bd9Sstevel@tonic-gate 
16347c478bd9Sstevel@tonic-gate 	/* Destroy saddr  */
1635f4b3ec61Sdh 	sctp_saddr_fini(sctps);
16367c478bd9Sstevel@tonic-gate 
16377c478bd9Sstevel@tonic-gate 	/* Global SCTP PCB list. */
1638f4b3ec61Sdh 	list_destroy(&sctps->sctps_g_list);
16397c478bd9Sstevel@tonic-gate 
16407c478bd9Sstevel@tonic-gate 	/* Destroy SCTP hash arrays. */
1641f4b3ec61Sdh 	sctp_hash_destroy(sctps);
1642f4b3ec61Sdh 
1643f4b3ec61Sdh 	/* Destroy SCTP kernel stats. */
16445dd46ab5SKacheong Poon 	for (i = 0; i < sctps->sctps_sc_cnt; i++)
16455dd46ab5SKacheong Poon 		kmem_free(sctps->sctps_sc[i], sizeof (sctp_stats_cpu_t));
16465dd46ab5SKacheong Poon 	kmem_free(sctps->sctps_sc, max_ncpus * sizeof (sctp_stats_cpu_t *));
1647f4b3ec61Sdh 
1648f4b3ec61Sdh 	sctp_kstat_fini(stackid, sctps->sctps_mibkp);
1649f4b3ec61Sdh 	sctps->sctps_mibkp = NULL;
16505dd46ab5SKacheong Poon 	sctp_kstat2_fini(stackid, sctps->sctps_kstat);
16515dd46ab5SKacheong Poon 	sctps->sctps_kstat = NULL;
1652f4b3ec61Sdh 
1653f4b3ec61Sdh 	mutex_destroy(&sctps->sctps_g_lock);
1654f4b3ec61Sdh 	mutex_destroy(&sctps->sctps_epriv_port_lock);
16557c478bd9Sstevel@tonic-gate 
1656f4b3ec61Sdh 	kmem_free(sctps, sizeof (*sctps));
16577c478bd9Sstevel@tonic-gate }
16587c478bd9Sstevel@tonic-gate 
16597c478bd9Sstevel@tonic-gate static void
1660f4b3ec61Sdh sctp_rq_tq_init(sctp_stack_t *sctps)
16617c478bd9Sstevel@tonic-gate {
16625dd46ab5SKacheong Poon 	char tq_name[TASKQ_NAMELEN];
16635dd46ab5SKacheong Poon 	int thrs;
16645dd46ab5SKacheong Poon 	int max_tasks;
16655dd46ab5SKacheong Poon 
1666*a215d4ebSKacheong Poon 	mutex_enter(&sctps->sctps_g_lock);
1667*a215d4ebSKacheong Poon 	/* Someone may have beaten us in creating the taskqs. */
1668*a215d4ebSKacheong Poon 	if (sctps->sctps_recvq_tq_list_cur_sz > 0) {
1669*a215d4ebSKacheong Poon 		mutex_exit(&sctps->sctps_g_lock);
1670*a215d4ebSKacheong Poon 		return;
1671*a215d4ebSKacheong Poon 	}
1672*a215d4ebSKacheong Poon 
16735dd46ab5SKacheong Poon 	thrs = MIN(sctp_recvq_tq_thr_max, MAX(sctp_recvq_tq_thr_min,
16745dd46ab5SKacheong Poon 	    MAX(ncpus, boot_ncpus)));
16755dd46ab5SKacheong Poon 	/*
16765dd46ab5SKacheong Poon 	 * Make sure that the maximum number of tasks is at least thrice as
16775dd46ab5SKacheong Poon 	 * large as the number of threads.
16785dd46ab5SKacheong Poon 	 */
16795dd46ab5SKacheong Poon 	max_tasks = MAX(sctp_recvq_tq_task_min, thrs) * 3;
16805dd46ab5SKacheong Poon 
16815dd46ab5SKacheong Poon 	/*
16825dd46ab5SKacheong Poon 	 * This helps differentiate the default taskqs in different IP stacks.
16835dd46ab5SKacheong Poon 	 */
16845dd46ab5SKacheong Poon 	(void) snprintf(tq_name, sizeof (tq_name), "sctp_def_rq_taskq_%d",
16855dd46ab5SKacheong Poon 	    sctps->sctps_netstack->netstack_stackid);
16865dd46ab5SKacheong Poon 
16875dd46ab5SKacheong Poon 	sctps->sctps_recvq_tq_list_max_sz = sctp_recvq_tq_list_max;
1688f4b3ec61Sdh 	sctps->sctps_recvq_tq_list_cur_sz = 1;
16895dd46ab5SKacheong Poon 
16907c478bd9Sstevel@tonic-gate 	/*
16917c478bd9Sstevel@tonic-gate 	 * Initialize the recvq_tq_list and create the first recvq taskq.
16927c478bd9Sstevel@tonic-gate 	 * What to do if it fails?
16937c478bd9Sstevel@tonic-gate 	 */
1694f4b3ec61Sdh 	sctps->sctps_recvq_tq_list =
1695f4b3ec61Sdh 	    kmem_zalloc(sctps->sctps_recvq_tq_list_max_sz * sizeof (taskq_t *),
1696b34b8d1aSkcpoon 	    KM_SLEEP);
16975dd46ab5SKacheong Poon 	sctps->sctps_recvq_tq_list[0] = taskq_create(tq_name, thrs,
16985dd46ab5SKacheong Poon 	    minclsyspri, sctp_recvq_tq_task_min, max_tasks, TASKQ_PREPOPULATE);
1699f4b3ec61Sdh 	mutex_init(&sctps->sctps_rq_tq_lock, NULL, MUTEX_DEFAULT, NULL);
1700*a215d4ebSKacheong Poon 
1701*a215d4ebSKacheong Poon 	mutex_exit(&sctps->sctps_g_lock);
17027c478bd9Sstevel@tonic-gate }
17037c478bd9Sstevel@tonic-gate 
17047c478bd9Sstevel@tonic-gate static void
1705f4b3ec61Sdh sctp_rq_tq_fini(sctp_stack_t *sctps)
17067c478bd9Sstevel@tonic-gate {
17077c478bd9Sstevel@tonic-gate 	int i;
17087c478bd9Sstevel@tonic-gate 
17095dd46ab5SKacheong Poon 	if (sctps->sctps_recvq_tq_list_cur_sz == 0)
17105dd46ab5SKacheong Poon 		return;
17115dd46ab5SKacheong Poon 
1712f4b3ec61Sdh 	for (i = 0; i < sctps->sctps_recvq_tq_list_cur_sz; i++) {
1713f4b3ec61Sdh 		ASSERT(sctps->sctps_recvq_tq_list[i] != NULL);
1714f4b3ec61Sdh 		taskq_destroy(sctps->sctps_recvq_tq_list[i]);
17157c478bd9Sstevel@tonic-gate 	}
1716f4b3ec61Sdh 	kmem_free(sctps->sctps_recvq_tq_list,
1717f4b3ec61Sdh 	    sctps->sctps_recvq_tq_list_max_sz * sizeof (taskq_t *));
1718f4b3ec61Sdh 	sctps->sctps_recvq_tq_list = NULL;
17197c478bd9Sstevel@tonic-gate }
17207c478bd9Sstevel@tonic-gate 
17217c478bd9Sstevel@tonic-gate /* Add another taskq for a new ill. */
17227c478bd9Sstevel@tonic-gate void
1723f4b3ec61Sdh sctp_inc_taskq(sctp_stack_t *sctps)
17247c478bd9Sstevel@tonic-gate {
17257c478bd9Sstevel@tonic-gate 	taskq_t *tq;
17267c478bd9Sstevel@tonic-gate 	char tq_name[TASKQ_NAMELEN];
17275dd46ab5SKacheong Poon 	int thrs;
17285dd46ab5SKacheong Poon 	int max_tasks;
17295dd46ab5SKacheong Poon 
17305dd46ab5SKacheong Poon 	thrs = MIN(sctp_recvq_tq_thr_max, MAX(sctp_recvq_tq_thr_min,
17315dd46ab5SKacheong Poon 	    MAX(ncpus, boot_ncpus)));
17325dd46ab5SKacheong Poon 	/*
17335dd46ab5SKacheong Poon 	 * Make sure that the maximum number of tasks is at least thrice as
17345dd46ab5SKacheong Poon 	 * large as the number of threads.
17355dd46ab5SKacheong Poon 	 */
17365dd46ab5SKacheong Poon 	max_tasks = MAX(sctp_recvq_tq_task_min, thrs) * 3;
17377c478bd9Sstevel@tonic-gate 
1738f4b3ec61Sdh 	mutex_enter(&sctps->sctps_rq_tq_lock);
1739f4b3ec61Sdh 	if (sctps->sctps_recvq_tq_list_cur_sz + 1 >
1740f4b3ec61Sdh 	    sctps->sctps_recvq_tq_list_max_sz) {
1741f4b3ec61Sdh 		mutex_exit(&sctps->sctps_rq_tq_lock);
17427c478bd9Sstevel@tonic-gate 		cmn_err(CE_NOTE, "Cannot create more SCTP recvq taskq");
17437c478bd9Sstevel@tonic-gate 		return;
17447c478bd9Sstevel@tonic-gate 	}
17457c478bd9Sstevel@tonic-gate 
17465dd46ab5SKacheong Poon 	(void) snprintf(tq_name, sizeof (tq_name), "sctp_rq_taskq_%d_%u",
17475dd46ab5SKacheong Poon 	    sctps->sctps_netstack->netstack_stackid,
1748f4b3ec61Sdh 	    sctps->sctps_recvq_tq_list_cur_sz);
17495dd46ab5SKacheong Poon 	tq = taskq_create(tq_name, thrs, minclsyspri, sctp_recvq_tq_task_min,
17505dd46ab5SKacheong Poon 	    max_tasks, TASKQ_PREPOPULATE);
17517c478bd9Sstevel@tonic-gate 	if (tq == NULL) {
1752f4b3ec61Sdh 		mutex_exit(&sctps->sctps_rq_tq_lock);
17537c478bd9Sstevel@tonic-gate 		cmn_err(CE_NOTE, "SCTP recvq taskq creation failed");
17547c478bd9Sstevel@tonic-gate 		return;
17557c478bd9Sstevel@tonic-gate 	}
1756f4b3ec61Sdh 	ASSERT(sctps->sctps_recvq_tq_list[
1757b34b8d1aSkcpoon 	    sctps->sctps_recvq_tq_list_cur_sz] == NULL);
1758f4b3ec61Sdh 	sctps->sctps_recvq_tq_list[sctps->sctps_recvq_tq_list_cur_sz] = tq;
1759f4b3ec61Sdh 	atomic_add_32(&sctps->sctps_recvq_tq_list_cur_sz, 1);
1760f4b3ec61Sdh 	mutex_exit(&sctps->sctps_rq_tq_lock);
17617c478bd9Sstevel@tonic-gate }
17627c478bd9Sstevel@tonic-gate 
17637c478bd9Sstevel@tonic-gate #ifdef DEBUG
17647c478bd9Sstevel@tonic-gate uint32_t recvq_loop_cnt = 0;
17657c478bd9Sstevel@tonic-gate uint32_t recvq_call = 0;
17667c478bd9Sstevel@tonic-gate #endif
17677c478bd9Sstevel@tonic-gate 
17687c478bd9Sstevel@tonic-gate /*
17697c478bd9Sstevel@tonic-gate  * Find the next recvq_tq to use.  This routine will go thru all the
17707c478bd9Sstevel@tonic-gate  * taskqs until it can dispatch a job for the sctp.  If this fails,
17717c478bd9Sstevel@tonic-gate  * it will create a new taskq and try it.
17727c478bd9Sstevel@tonic-gate  */
17737c478bd9Sstevel@tonic-gate static boolean_t
17747c478bd9Sstevel@tonic-gate sctp_find_next_tq(sctp_t *sctp)
17757c478bd9Sstevel@tonic-gate {
17767c478bd9Sstevel@tonic-gate 	int next_tq, try;
17777c478bd9Sstevel@tonic-gate 	taskq_t *tq;
1778f4b3ec61Sdh 	sctp_stack_t	*sctps = sctp->sctp_sctps;
17797c478bd9Sstevel@tonic-gate 
17807c478bd9Sstevel@tonic-gate 	/*
17817c478bd9Sstevel@tonic-gate 	 * Note that since we don't hold a lock on sctp_rq_tq_lock for
17827c478bd9Sstevel@tonic-gate 	 * performance reason, recvq_ta_list_cur_sz can be changed during
17837c478bd9Sstevel@tonic-gate 	 * this loop.  The problem this will create is that the loop may
17847c478bd9Sstevel@tonic-gate 	 * not have tried all the recvq_tq.  This should be OK.
17857c478bd9Sstevel@tonic-gate 	 */
1786f4b3ec61Sdh 	next_tq = atomic_add_32_nv(&sctps->sctps_recvq_tq_list_cur, 1) %
1787f4b3ec61Sdh 	    sctps->sctps_recvq_tq_list_cur_sz;
1788f4b3ec61Sdh 	for (try = 0; try < sctps->sctps_recvq_tq_list_cur_sz; try++) {
1789f4b3ec61Sdh 		tq = sctps->sctps_recvq_tq_list[next_tq];
17907c478bd9Sstevel@tonic-gate 		if (taskq_dispatch(tq, sctp_process_recvq, sctp,
17917c478bd9Sstevel@tonic-gate 		    TQ_NOSLEEP) != NULL) {
17927c478bd9Sstevel@tonic-gate 			sctp->sctp_recvq_tq = tq;
17937c478bd9Sstevel@tonic-gate 			return (B_TRUE);
17947c478bd9Sstevel@tonic-gate 		}
1795f4b3ec61Sdh 		next_tq = (next_tq + 1) % sctps->sctps_recvq_tq_list_cur_sz;
17967c478bd9Sstevel@tonic-gate 	}
17977c478bd9Sstevel@tonic-gate 
17987c478bd9Sstevel@tonic-gate 	/*
17997c478bd9Sstevel@tonic-gate 	 * Create one more taskq and try it.  Note that sctp_inc_taskq()
18007c478bd9Sstevel@tonic-gate 	 * may not have created another taskq if the number of recvq
18017c478bd9Sstevel@tonic-gate 	 * taskqs is at the maximum.  We are probably in a pretty bad
18027c478bd9Sstevel@tonic-gate 	 * shape if this actually happens...
18037c478bd9Sstevel@tonic-gate 	 */
1804f4b3ec61Sdh 	sctp_inc_taskq(sctps);
1805f4b3ec61Sdh 	tq = sctps->sctps_recvq_tq_list[sctps->sctps_recvq_tq_list_cur_sz - 1];
18067c478bd9Sstevel@tonic-gate 	if (taskq_dispatch(tq, sctp_process_recvq, sctp, TQ_NOSLEEP) != NULL) {
18077c478bd9Sstevel@tonic-gate 		sctp->sctp_recvq_tq = tq;
18087c478bd9Sstevel@tonic-gate 		return (B_TRUE);
18097c478bd9Sstevel@tonic-gate 	}
1810f4b3ec61Sdh 	SCTP_KSTAT(sctps, sctp_find_next_tq);
18117c478bd9Sstevel@tonic-gate 	return (B_FALSE);
18127c478bd9Sstevel@tonic-gate }
18137c478bd9Sstevel@tonic-gate 
18147c478bd9Sstevel@tonic-gate /*
18157c478bd9Sstevel@tonic-gate  * To add a message to the recvq.  Note that the sctp_timer_fire()
18167c478bd9Sstevel@tonic-gate  * routine also uses this function to add the timer message to the
18177c478bd9Sstevel@tonic-gate  * receive queue for later processing.  And it should be the only
18187c478bd9Sstevel@tonic-gate  * caller of sctp_add_recvq() which sets the try_harder argument
18197c478bd9Sstevel@tonic-gate  * to B_TRUE.
18207c478bd9Sstevel@tonic-gate  *
18217c478bd9Sstevel@tonic-gate  * If the try_harder argument is B_TRUE, this routine sctp_find_next_tq()
18227c478bd9Sstevel@tonic-gate  * will try very hard to dispatch the task.  Refer to the comment
18237c478bd9Sstevel@tonic-gate  * for that routine on how it does that.
1824bd670b35SErik Nordmark  *
1825bd670b35SErik Nordmark  * On failure the message has been freed i.e., this routine always consumes the
1826bd670b35SErik Nordmark  * message. It bumps ipIfStatsInDiscards and and uses ip_drop_input to drop.
18277c478bd9Sstevel@tonic-gate  */
1828bd670b35SErik Nordmark void
1829bd670b35SErik Nordmark sctp_add_recvq(sctp_t *sctp, mblk_t *mp, boolean_t caller_hold_lock,
1830bd670b35SErik Nordmark     ip_recv_attr_t *ira)
18317c478bd9Sstevel@tonic-gate {
1832bd670b35SErik Nordmark 	mblk_t	*attrmp;
1833bd670b35SErik Nordmark 	ip_stack_t	*ipst = sctp->sctp_sctps->sctps_netstack->netstack_ip;
1834bd670b35SErik Nordmark 
1835bd670b35SErik Nordmark 	ASSERT(ira->ira_ill == NULL);
1836bd670b35SErik Nordmark 
18377c478bd9Sstevel@tonic-gate 	if (!caller_hold_lock)
18387c478bd9Sstevel@tonic-gate 		mutex_enter(&sctp->sctp_recvq_lock);
18397c478bd9Sstevel@tonic-gate 
18407c478bd9Sstevel@tonic-gate 	/* If the taskq dispatch has not been scheduled, do it now. */
18417c478bd9Sstevel@tonic-gate 	if (sctp->sctp_recvq_tq == NULL) {
18427c478bd9Sstevel@tonic-gate 		ASSERT(sctp->sctp_recvq == NULL);
18437c478bd9Sstevel@tonic-gate 		if (!sctp_find_next_tq(sctp)) {
18447c478bd9Sstevel@tonic-gate 			if (!caller_hold_lock)
18457c478bd9Sstevel@tonic-gate 				mutex_exit(&sctp->sctp_recvq_lock);
1846bd670b35SErik Nordmark 			BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsInDiscards);
1847bd670b35SErik Nordmark 			ip_drop_input("ipIfStatsInDiscards", mp, NULL);
1848bd670b35SErik Nordmark 			freemsg(mp);
1849bd670b35SErik Nordmark 			return;
18507c478bd9Sstevel@tonic-gate 		}
18517c478bd9Sstevel@tonic-gate 		/* Make sure the sctp_t will not go away. */
18527c478bd9Sstevel@tonic-gate 		SCTP_REFHOLD(sctp);
18537c478bd9Sstevel@tonic-gate 	}
18547c478bd9Sstevel@tonic-gate 
1855bd670b35SErik Nordmark 	attrmp = ip_recv_attr_to_mblk(ira);
1856bd670b35SErik Nordmark 	if (attrmp == NULL) {
1857bd670b35SErik Nordmark 		if (!caller_hold_lock)
1858bd670b35SErik Nordmark 			mutex_exit(&sctp->sctp_recvq_lock);
1859bd670b35SErik Nordmark 		BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsInDiscards);
1860bd670b35SErik Nordmark 		ip_drop_input("ipIfStatsInDiscards", mp, NULL);
1861bd670b35SErik Nordmark 		freemsg(mp);
1862bd670b35SErik Nordmark 		return;
1863bd670b35SErik Nordmark 	}
1864bd670b35SErik Nordmark 	ASSERT(attrmp->b_cont == NULL);
1865bd670b35SErik Nordmark 	attrmp->b_cont = mp;
1866bd670b35SErik Nordmark 	mp = attrmp;
1867bd670b35SErik Nordmark 
18687c478bd9Sstevel@tonic-gate 	if (sctp->sctp_recvq == NULL) {
18697c478bd9Sstevel@tonic-gate 		sctp->sctp_recvq = mp;
18707c478bd9Sstevel@tonic-gate 		sctp->sctp_recvq_tail = mp;
18717c478bd9Sstevel@tonic-gate 	} else {
18727c478bd9Sstevel@tonic-gate 		sctp->sctp_recvq_tail->b_next = mp;
18737c478bd9Sstevel@tonic-gate 		sctp->sctp_recvq_tail = mp;
18747c478bd9Sstevel@tonic-gate 	}
18757c478bd9Sstevel@tonic-gate 
18767c478bd9Sstevel@tonic-gate 	if (!caller_hold_lock)
18777c478bd9Sstevel@tonic-gate 		mutex_exit(&sctp->sctp_recvq_lock);
18787c478bd9Sstevel@tonic-gate }
18797c478bd9Sstevel@tonic-gate 
18807c478bd9Sstevel@tonic-gate static void
18817c478bd9Sstevel@tonic-gate sctp_process_recvq(void *arg)
18827c478bd9Sstevel@tonic-gate {
18837c478bd9Sstevel@tonic-gate 	sctp_t		*sctp = (sctp_t *)arg;
18847c478bd9Sstevel@tonic-gate 	mblk_t		*mp;
18857c478bd9Sstevel@tonic-gate #ifdef DEBUG
18867c478bd9Sstevel@tonic-gate 	uint32_t	loop_cnt = 0;
18877c478bd9Sstevel@tonic-gate #endif
1888bd670b35SErik Nordmark 	ip_recv_attr_t	iras;
18897c478bd9Sstevel@tonic-gate 
18907c478bd9Sstevel@tonic-gate #ifdef	_BIG_ENDIAN
18917c478bd9Sstevel@tonic-gate #define	IPVER(ip6h)	((((uint32_t *)ip6h)[0] >> 28) & 0x7)
18927c478bd9Sstevel@tonic-gate #else
18937c478bd9Sstevel@tonic-gate #define	IPVER(ip6h)	((((uint32_t *)ip6h)[0] >> 4) & 0x7)
18947c478bd9Sstevel@tonic-gate #endif
18957c478bd9Sstevel@tonic-gate 
18967c478bd9Sstevel@tonic-gate 	RUN_SCTP(sctp);
18977c478bd9Sstevel@tonic-gate 	mutex_enter(&sctp->sctp_recvq_lock);
18987c478bd9Sstevel@tonic-gate 
18997c478bd9Sstevel@tonic-gate #ifdef DEBUG
19007c478bd9Sstevel@tonic-gate 	recvq_call++;
19017c478bd9Sstevel@tonic-gate #endif
19027c478bd9Sstevel@tonic-gate 	/*
19037c478bd9Sstevel@tonic-gate 	 * Note that while we are in this loop, other thread can put
19047c478bd9Sstevel@tonic-gate 	 * new packets in the receive queue.  We may be looping for
19057c478bd9Sstevel@tonic-gate 	 * quite a while.
19067c478bd9Sstevel@tonic-gate 	 */
19077c478bd9Sstevel@tonic-gate 	while ((mp = sctp->sctp_recvq) != NULL) {
1908bd670b35SErik Nordmark 		mblk_t *data_mp;
1909bd670b35SErik Nordmark 
19107c478bd9Sstevel@tonic-gate 		sctp->sctp_recvq = mp->b_next;
19117c478bd9Sstevel@tonic-gate 		mutex_exit(&sctp->sctp_recvq_lock);
19127c478bd9Sstevel@tonic-gate 		mp->b_next = NULL;
19137c478bd9Sstevel@tonic-gate #ifdef DEBUG
19147c478bd9Sstevel@tonic-gate 		loop_cnt++;
19157c478bd9Sstevel@tonic-gate #endif
19167c478bd9Sstevel@tonic-gate 		mp->b_prev = NULL;
19177c478bd9Sstevel@tonic-gate 
1918bd670b35SErik Nordmark 		data_mp = mp->b_cont;
1919bd670b35SErik Nordmark 		mp->b_cont = NULL;
1920bd670b35SErik Nordmark 		if (!ip_recv_attr_from_mblk(mp, &iras)) {
1921bd670b35SErik Nordmark 			ip_drop_input("ip_recv_attr_from_mblk", mp, NULL);
1922bd670b35SErik Nordmark 			freemsg(mp);
1923bd670b35SErik Nordmark 			ira_cleanup(&iras, B_TRUE);
1924bd670b35SErik Nordmark 			continue;
1925bd670b35SErik Nordmark 		}
1926bd670b35SErik Nordmark 
1927bd670b35SErik Nordmark 		if (iras.ira_flags & IRAF_ICMP_ERROR)
1928bd670b35SErik Nordmark 			sctp_icmp_error(sctp, data_mp);
1929bd670b35SErik Nordmark 		else
1930bd670b35SErik Nordmark 			sctp_input_data(sctp, data_mp, &iras);
1931bd670b35SErik Nordmark 
1932bd670b35SErik Nordmark 		ira_cleanup(&iras, B_TRUE);
19337c478bd9Sstevel@tonic-gate 		mutex_enter(&sctp->sctp_recvq_lock);
19347c478bd9Sstevel@tonic-gate 	}
19357c478bd9Sstevel@tonic-gate 
19367c478bd9Sstevel@tonic-gate 	sctp->sctp_recvq_tail = NULL;
19377c478bd9Sstevel@tonic-gate 	sctp->sctp_recvq_tq = NULL;
19387c478bd9Sstevel@tonic-gate 
19397c478bd9Sstevel@tonic-gate 	mutex_exit(&sctp->sctp_recvq_lock);
19407c478bd9Sstevel@tonic-gate 
19417c478bd9Sstevel@tonic-gate 	WAKE_SCTP(sctp);
19427c478bd9Sstevel@tonic-gate 
19437c478bd9Sstevel@tonic-gate #ifdef DEBUG
19447c478bd9Sstevel@tonic-gate 	if (loop_cnt > recvq_loop_cnt)
19457c478bd9Sstevel@tonic-gate 		recvq_loop_cnt = loop_cnt;
19467c478bd9Sstevel@tonic-gate #endif
19477c478bd9Sstevel@tonic-gate 	/* Now it can go away. */
19487c478bd9Sstevel@tonic-gate 	SCTP_REFRELE(sctp);
19497c478bd9Sstevel@tonic-gate }
19507c478bd9Sstevel@tonic-gate 
19517c478bd9Sstevel@tonic-gate /* ARGSUSED */
19527c478bd9Sstevel@tonic-gate static int
19537c478bd9Sstevel@tonic-gate sctp_conn_cache_constructor(void *buf, void *cdrarg, int kmflags)
19547c478bd9Sstevel@tonic-gate {
1955bd670b35SErik Nordmark 	conn_t	*connp = (conn_t *)buf;
1956bd670b35SErik Nordmark 	sctp_t	*sctp = (sctp_t *)&connp[1];
195792baa190SGeorge Shepherd 	int	cnt;
19587c478bd9Sstevel@tonic-gate 
1959bd670b35SErik Nordmark 	bzero(connp, sizeof (conn_t));
19607c478bd9Sstevel@tonic-gate 	bzero(buf, (char *)&sctp[1] - (char *)buf);
19617c478bd9Sstevel@tonic-gate 
19627c478bd9Sstevel@tonic-gate 	mutex_init(&sctp->sctp_reflock, NULL, MUTEX_DEFAULT, NULL);
19637c478bd9Sstevel@tonic-gate 	mutex_init(&sctp->sctp_lock, NULL, MUTEX_DEFAULT, NULL);
19647c478bd9Sstevel@tonic-gate 	mutex_init(&sctp->sctp_recvq_lock, NULL, MUTEX_DEFAULT, NULL);
19657c478bd9Sstevel@tonic-gate 	cv_init(&sctp->sctp_cv, NULL, CV_DEFAULT, NULL);
196692baa190SGeorge Shepherd 	for (cnt = 0; cnt < SCTP_IPIF_HASH; cnt++) {
196792baa190SGeorge Shepherd 		rw_init(&sctp->sctp_saddrs[cnt].ipif_hash_lock, NULL,
196892baa190SGeorge Shepherd 		    RW_DEFAULT, NULL);
196992baa190SGeorge Shepherd 	}
19707c478bd9Sstevel@tonic-gate 
1971bd670b35SErik Nordmark 	mutex_init(&connp->conn_lock, NULL, MUTEX_DEFAULT, NULL);
1972bd670b35SErik Nordmark 	cv_init(&connp->conn_cv, NULL, CV_DEFAULT, NULL);
1973bd670b35SErik Nordmark 	connp->conn_flags = IPCL_SCTPCONN;
1974bd670b35SErik Nordmark 	connp->conn_proto = IPPROTO_SCTP;
1975bd670b35SErik Nordmark 	connp->conn_sctp = sctp;
1976bd670b35SErik Nordmark 	sctp->sctp_connp = connp;
1977bd670b35SErik Nordmark 	rw_init(&connp->conn_ilg_lock, NULL, RW_DEFAULT, NULL);
1978bd670b35SErik Nordmark 
1979bd670b35SErik Nordmark 	connp->conn_ixa = kmem_zalloc(sizeof (ip_xmit_attr_t), kmflags);
1980bd670b35SErik Nordmark 	if (connp->conn_ixa == NULL) {
1981bd670b35SErik Nordmark 		return (ENOMEM);
1982bd670b35SErik Nordmark 	}
1983bd670b35SErik Nordmark 	connp->conn_ixa->ixa_refcnt = 1;
1984bd670b35SErik Nordmark 	connp->conn_ixa->ixa_protocol = connp->conn_proto;
1985bd670b35SErik Nordmark 	connp->conn_ixa->ixa_xmit_hint = CONN_TO_XMIT_HINT(connp);
19867c478bd9Sstevel@tonic-gate 	return (0);
19877c478bd9Sstevel@tonic-gate }
19887c478bd9Sstevel@tonic-gate 
19897c478bd9Sstevel@tonic-gate /* ARGSUSED */
19907c478bd9Sstevel@tonic-gate static void
19917c478bd9Sstevel@tonic-gate sctp_conn_cache_destructor(void *buf, void *cdrarg)
19927c478bd9Sstevel@tonic-gate {
1993bd670b35SErik Nordmark 	conn_t	*connp = (conn_t *)buf;
1994bd670b35SErik Nordmark 	sctp_t	*sctp = (sctp_t *)&connp[1];
199592baa190SGeorge Shepherd 	int	cnt;
19967c478bd9Sstevel@tonic-gate 
1997bd670b35SErik Nordmark 	ASSERT(sctp->sctp_connp == connp);
19987c478bd9Sstevel@tonic-gate 	ASSERT(!MUTEX_HELD(&sctp->sctp_lock));
19997c478bd9Sstevel@tonic-gate 	ASSERT(!MUTEX_HELD(&sctp->sctp_reflock));
20007c478bd9Sstevel@tonic-gate 	ASSERT(!MUTEX_HELD(&sctp->sctp_recvq_lock));
20017c478bd9Sstevel@tonic-gate 
20027c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_conn_hash_next == NULL);
20037c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_conn_hash_prev == NULL);
20047c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_listen_hash_next == NULL);
20057c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_listen_hash_prev == NULL);
20067c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_listen_tfp == NULL);
20077c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_conn_tfp == NULL);
20087c478bd9Sstevel@tonic-gate 
20097c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_faddrs == NULL);
20107c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_nsaddrs == 0);
20117c478bd9Sstevel@tonic-gate 
20127c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_ulpd == NULL);
20137c478bd9Sstevel@tonic-gate 
20147c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_lastfaddr == NULL);
20157c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_primary == NULL);
20167c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_current == NULL);
20177c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_lastdata == NULL);
20187c478bd9Sstevel@tonic-gate 
20197c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_xmit_head == NULL);
20207c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_xmit_tail == NULL);
20217c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_xmit_unsent == NULL);
20227c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_xmit_unsent_tail == NULL);
20237c478bd9Sstevel@tonic-gate 
20247c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_ostrcntrs == NULL);
20257c478bd9Sstevel@tonic-gate 
20267c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_sack_info == NULL);
20277c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_ack_mp == NULL);
20287c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_instr == NULL);
20297c478bd9Sstevel@tonic-gate 
20307c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_iphc == NULL);
20317c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_iphc6 == NULL);
20327c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_ipha == NULL);
20337c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_ip6h == NULL);
20347c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_sctph == NULL);
20357c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_sctph6 == NULL);
20367c478bd9Sstevel@tonic-gate 
20377c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_cookie_mp == NULL);
20387c478bd9Sstevel@tonic-gate 
20397c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_refcnt == 0);
20407c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_timer_mp == NULL);
20417c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_connp->conn_ref == 0);
20427c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_heartbeat_mp == NULL);
20437c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_ptpbhn == NULL && sctp->sctp_bind_hash == NULL);
20447c478bd9Sstevel@tonic-gate 
20457c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_shutdown_faddr == NULL);
20467c478bd9Sstevel@tonic-gate 
20477c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_cxmit_list == NULL);
20487c478bd9Sstevel@tonic-gate 
20497c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_recvq == NULL);
20507c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_recvq_tail == NULL);
20517c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_recvq_tq == NULL);
20527c478bd9Sstevel@tonic-gate 
2053121e5416Skcpoon 	/*
2054121e5416Skcpoon 	 * sctp_pad_mp can be NULL if the memory allocation fails
2055121e5416Skcpoon 	 * in sctp_init_values() and the conn_t is freed.
2056121e5416Skcpoon 	 */
2057121e5416Skcpoon 	if (sctp->sctp_pad_mp != NULL) {
2058121e5416Skcpoon 		freeb(sctp->sctp_pad_mp);
2059121e5416Skcpoon 		sctp->sctp_pad_mp = NULL;
2060121e5416Skcpoon 	}
2061121e5416Skcpoon 
20627c478bd9Sstevel@tonic-gate 	mutex_destroy(&sctp->sctp_reflock);
20637c478bd9Sstevel@tonic-gate 	mutex_destroy(&sctp->sctp_lock);
20647c478bd9Sstevel@tonic-gate 	mutex_destroy(&sctp->sctp_recvq_lock);
20657c478bd9Sstevel@tonic-gate 	cv_destroy(&sctp->sctp_cv);
206692baa190SGeorge Shepherd 	for (cnt = 0; cnt < SCTP_IPIF_HASH; cnt++) {
206792baa190SGeorge Shepherd 		rw_destroy(&sctp->sctp_saddrs[cnt].ipif_hash_lock);
206892baa190SGeorge Shepherd 	}
20697c478bd9Sstevel@tonic-gate 
2070bd670b35SErik Nordmark 	mutex_destroy(&connp->conn_lock);
2071bd670b35SErik Nordmark 	cv_destroy(&connp->conn_cv);
2072bd670b35SErik Nordmark 	rw_destroy(&connp->conn_ilg_lock);
2073bd670b35SErik Nordmark 
2074bd670b35SErik Nordmark 	/* Can be NULL if constructor failed */
2075bd670b35SErik Nordmark 	if (connp->conn_ixa != NULL) {
2076bd670b35SErik Nordmark 		ASSERT(connp->conn_ixa->ixa_refcnt == 1);
2077bd670b35SErik Nordmark 		ASSERT(connp->conn_ixa->ixa_ire == NULL);
2078bd670b35SErik Nordmark 		ASSERT(connp->conn_ixa->ixa_nce == NULL);
2079bd670b35SErik Nordmark 		ixa_refrele(connp->conn_ixa);
2080bd670b35SErik Nordmark 	}
20817c478bd9Sstevel@tonic-gate }
20827c478bd9Sstevel@tonic-gate 
20837c478bd9Sstevel@tonic-gate static void
20847c478bd9Sstevel@tonic-gate sctp_conn_cache_init()
20857c478bd9Sstevel@tonic-gate {
20867c478bd9Sstevel@tonic-gate 	sctp_conn_cache = kmem_cache_create("sctp_conn_cache",
20877c478bd9Sstevel@tonic-gate 	    sizeof (sctp_t) + sizeof (conn_t), 0, sctp_conn_cache_constructor,
20885dd46ab5SKacheong Poon 	    sctp_conn_cache_destructor, sctp_conn_reclaim, NULL, NULL, 0);
20897c478bd9Sstevel@tonic-gate }
20907c478bd9Sstevel@tonic-gate 
20917c478bd9Sstevel@tonic-gate static void
20927c478bd9Sstevel@tonic-gate sctp_conn_cache_fini()
20937c478bd9Sstevel@tonic-gate {
20947c478bd9Sstevel@tonic-gate 	kmem_cache_destroy(sctp_conn_cache);
20957c478bd9Sstevel@tonic-gate }
2096121e5416Skcpoon 
2097121e5416Skcpoon void
2098121e5416Skcpoon sctp_conn_init(conn_t *connp)
2099121e5416Skcpoon {
2100bd670b35SErik Nordmark 	ASSERT(connp->conn_flags == IPCL_SCTPCONN);
2101121e5416Skcpoon 	connp->conn_rq = connp->conn_wq = NULL;
2102bd670b35SErik Nordmark 	connp->conn_ixa->ixa_flags |= IXAF_SET_ULP_CKSUM | IXAF_VERIFY_SOURCE |
2103bd670b35SErik Nordmark 	    IXAF_VERIFY_PMTU;
2104bd670b35SErik Nordmark 
2105bd670b35SErik Nordmark 	ASSERT(connp->conn_proto == IPPROTO_SCTP);
2106bd670b35SErik Nordmark 	ASSERT(connp->conn_ixa->ixa_protocol == connp->conn_proto);
2107121e5416Skcpoon 	connp->conn_state_flags |= CONN_INCIPIENT;
2108bd670b35SErik Nordmark 
2109bd670b35SErik Nordmark 	ASSERT(connp->conn_sctp != NULL);
2110bd670b35SErik Nordmark 
2111bd670b35SErik Nordmark 	/*
2112bd670b35SErik Nordmark 	 * Register sctp_notify to listen to capability changes detected by IP.
2113bd670b35SErik Nordmark 	 * This upcall is made in the context of the call to conn_ip_output
2114bd670b35SErik Nordmark 	 * thus it holds whatever locks sctp holds across conn_ip_output.
2115bd670b35SErik Nordmark 	 */
2116bd670b35SErik Nordmark 	connp->conn_ixa->ixa_notify = sctp_notify;
2117bd670b35SErik Nordmark 	connp->conn_ixa->ixa_notify_cookie = connp->conn_sctp;
2118121e5416Skcpoon }
2119121e5416Skcpoon 
2120121e5416Skcpoon static void
2121121e5416Skcpoon sctp_conn_clear(conn_t *connp)
2122121e5416Skcpoon {
2123121e5416Skcpoon 	/* Clean up conn_t stuff */
2124bd670b35SErik Nordmark 	if (connp->conn_latch != NULL) {
2125bd670b35SErik Nordmark 		IPLATCH_REFRELE(connp->conn_latch);
2126bd670b35SErik Nordmark 		connp->conn_latch = NULL;
2127bd670b35SErik Nordmark 	}
2128bd670b35SErik Nordmark 	if (connp->conn_latch_in_policy != NULL) {
2129bd670b35SErik Nordmark 		IPPOL_REFRELE(connp->conn_latch_in_policy);
2130bd670b35SErik Nordmark 		connp->conn_latch_in_policy = NULL;
2131bd670b35SErik Nordmark 	}
2132bd670b35SErik Nordmark 	if (connp->conn_latch_in_action != NULL) {
2133bd670b35SErik Nordmark 		IPACT_REFRELE(connp->conn_latch_in_action);
2134bd670b35SErik Nordmark 		connp->conn_latch_in_action = NULL;
2135bd670b35SErik Nordmark 	}
2136bd670b35SErik Nordmark 	if (connp->conn_policy != NULL) {
2137121e5416Skcpoon 		IPPH_REFRELE(connp->conn_policy, connp->conn_netstack);
2138bd670b35SErik Nordmark 		connp->conn_policy = NULL;
2139bd670b35SErik Nordmark 	}
2140bd670b35SErik Nordmark 	if (connp->conn_ipsec_opt_mp != NULL) {
2141121e5416Skcpoon 		freemsg(connp->conn_ipsec_opt_mp);
2142bd670b35SErik Nordmark 		connp->conn_ipsec_opt_mp = NULL;
2143bd670b35SErik Nordmark 	}
2144121e5416Skcpoon 	netstack_rele(connp->conn_netstack);
2145bd670b35SErik Nordmark 	connp->conn_netstack = NULL;
2146bd670b35SErik Nordmark 
2147bd670b35SErik Nordmark 	/* Leave conn_ixa and other constructed fields in place */
2148bd670b35SErik Nordmark 	ipcl_conn_cleanup(connp);
2149121e5416Skcpoon }
2150