xref: /illumos-gate/usr/src/uts/common/inet/sctp/sctp.c (revision bd670b35)
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 /*
23de8c4a14SErik Nordmark  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #include <sys/types.h>
287c478bd9Sstevel@tonic-gate #include <sys/stream.h>
297c478bd9Sstevel@tonic-gate #include <sys/strsubr.h>
307c478bd9Sstevel@tonic-gate #include <sys/stropts.h>
317c478bd9Sstevel@tonic-gate #include <sys/strsun.h>
327c478bd9Sstevel@tonic-gate #define	_SUN_TPI_VERSION 2
337c478bd9Sstevel@tonic-gate #include <sys/tihdr.h>
347c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
357c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
367c478bd9Sstevel@tonic-gate #include <sys/xti_inet.h>
377c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
387c478bd9Sstevel@tonic-gate #include <sys/debug.h>
397c478bd9Sstevel@tonic-gate #include <sys/vtrace.h>
407c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
417c478bd9Sstevel@tonic-gate #include <sys/cpuvar.h>
427c478bd9Sstevel@tonic-gate #include <sys/random.h>
4345916cd2Sjpk #include <sys/priv.h>
44f4b3ec61Sdh #include <sys/sunldi.h>
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate #include <sys/errno.h>
477c478bd9Sstevel@tonic-gate #include <sys/signal.h>
487c478bd9Sstevel@tonic-gate #include <sys/socket.h>
497c478bd9Sstevel@tonic-gate #include <sys/isa_defs.h>
507c478bd9Sstevel@tonic-gate #include <netinet/in.h>
517c478bd9Sstevel@tonic-gate #include <netinet/tcp.h>
527c478bd9Sstevel@tonic-gate #include <netinet/ip6.h>
537c478bd9Sstevel@tonic-gate #include <netinet/icmp6.h>
547c478bd9Sstevel@tonic-gate #include <netinet/sctp.h>
557c478bd9Sstevel@tonic-gate #include <net/if.h>
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate #include <inet/common.h>
587c478bd9Sstevel@tonic-gate #include <inet/ip.h>
59*bd670b35SErik Nordmark #include <inet/ip_if.h>
60*bd670b35SErik Nordmark #include <inet/ip_ire.h>
617c478bd9Sstevel@tonic-gate #include <inet/ip6.h>
627c478bd9Sstevel@tonic-gate #include <inet/mi.h>
637c478bd9Sstevel@tonic-gate #include <inet/mib2.h>
64f4b3ec61Sdh #include <inet/kstatcom.h>
657c478bd9Sstevel@tonic-gate #include <inet/nd.h>
667c478bd9Sstevel@tonic-gate #include <inet/optcom.h>
677c478bd9Sstevel@tonic-gate #include <inet/ipclassifier.h>
687c478bd9Sstevel@tonic-gate #include <inet/ipsec_impl.h>
697c478bd9Sstevel@tonic-gate #include <inet/sctp_ip.h>
707c478bd9Sstevel@tonic-gate #include <inet/sctp_crc32.h>
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate #include "sctp_impl.h"
737c478bd9Sstevel@tonic-gate #include "sctp_addr.h"
741d8c4025Svi #include "sctp_asconf.h"
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate int sctpdebug;
777c478bd9Sstevel@tonic-gate sin6_t	sctp_sin6_null;	/* Zero address for quick clears */
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate static void	sctp_closei_local(sctp_t *sctp);
807c478bd9Sstevel@tonic-gate static int	sctp_init_values(sctp_t *, sctp_t *, int);
817c478bd9Sstevel@tonic-gate static void	sctp_icmp_error_ipv6(sctp_t *sctp, mblk_t *mp);
827c478bd9Sstevel@tonic-gate static void	sctp_process_recvq(void *);
83f4b3ec61Sdh static void	sctp_rq_tq_init(sctp_stack_t *);
84f4b3ec61Sdh static void	sctp_rq_tq_fini(sctp_stack_t *);
857c478bd9Sstevel@tonic-gate static void	sctp_conn_cache_init();
867c478bd9Sstevel@tonic-gate static void	sctp_conn_cache_fini();
877c478bd9Sstevel@tonic-gate static int	sctp_conn_cache_constructor();
887c478bd9Sstevel@tonic-gate static void	sctp_conn_cache_destructor();
89121e5416Skcpoon static void	sctp_conn_clear(conn_t *);
90*bd670b35SErik Nordmark static void	sctp_notify(void *, ip_xmit_attr_t *, ixa_notify_type_t,
91*bd670b35SErik Nordmark     ixa_notify_arg_t);
92f4b3ec61Sdh 
93f4b3ec61Sdh static void	*sctp_stack_init(netstackid_t stackid, netstack_t *ns);
94f4b3ec61Sdh static void	sctp_stack_fini(netstackid_t stackid, void *arg);
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate /*
977c478bd9Sstevel@tonic-gate  * SCTP receive queue taskq
987c478bd9Sstevel@tonic-gate  *
997c478bd9Sstevel@tonic-gate  * At SCTP initialization time, a default taskq is created for
1007c478bd9Sstevel@tonic-gate  * servicing packets received when the interrupt thread cannot
1017c478bd9Sstevel@tonic-gate  * get a hold on the sctp_t.  The number of taskq can be increased in
1027c478bd9Sstevel@tonic-gate  * sctp_find_next_tq() when an existing taskq cannot be dispatched.
1037c478bd9Sstevel@tonic-gate  * The taskqs are never removed.  But the max number of taskq which
1047c478bd9Sstevel@tonic-gate  * can be created is controlled by sctp_recvq_tq_list_max_sz.  Note
1057c478bd9Sstevel@tonic-gate  * that SCTP recvq taskq is not tied to any specific CPU or ill.
1067c478bd9Sstevel@tonic-gate  *
1077c478bd9Sstevel@tonic-gate  * Those taskqs are stored in an array recvq_tq_list.  And they are
1087c478bd9Sstevel@tonic-gate  * used in a round robin fashion.  The current taskq being used is
1097c478bd9Sstevel@tonic-gate  * determined by recvq_tq_list_cur.
1107c478bd9Sstevel@tonic-gate  */
1117c478bd9Sstevel@tonic-gate 
112f4b3ec61Sdh /* /etc/system variables */
1137c478bd9Sstevel@tonic-gate /* The minimum number of threads for each taskq. */
1147c478bd9Sstevel@tonic-gate int		sctp_recvq_tq_thr_min = 4;
1157c478bd9Sstevel@tonic-gate /* The maximum number of threads for each taskq. */
1167c478bd9Sstevel@tonic-gate int		sctp_recvq_tq_thr_max = 16;
1177c478bd9Sstevel@tonic-gate /* The minimum number of tasks for each taskq. */
1187c478bd9Sstevel@tonic-gate int		sctp_recvq_tq_task_min = 5;
1197c478bd9Sstevel@tonic-gate /* The maxiimum number of tasks for each taskq. */
1207c478bd9Sstevel@tonic-gate int		sctp_recvq_tq_task_max = 50;
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate /*  sctp_t/conn_t kmem cache */
1237c478bd9Sstevel@tonic-gate struct kmem_cache	*sctp_conn_cache;
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate #define	SCTP_CONDEMNED(sctp)				\
1267c478bd9Sstevel@tonic-gate 	mutex_enter(&(sctp)->sctp_reflock);		\
1277c478bd9Sstevel@tonic-gate 	((sctp)->sctp_condemned = B_TRUE);		\
1285dddb8baSkcpoon 	mutex_exit(&(sctp)->sctp_reflock);
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate /* Link/unlink a sctp_t to/from the global list. */
131f4b3ec61Sdh #define	SCTP_LINK(sctp, sctps)				\
132f4b3ec61Sdh 	mutex_enter(&(sctps)->sctps_g_lock);		\
133f4b3ec61Sdh 	list_insert_tail(&sctps->sctps_g_list, (sctp));	\
134f4b3ec61Sdh 	mutex_exit(&(sctps)->sctps_g_lock);
1357c478bd9Sstevel@tonic-gate 
136f4b3ec61Sdh #define	SCTP_UNLINK(sctp, sctps)				\
137f4b3ec61Sdh 	mutex_enter(&(sctps)->sctps_g_lock);		\
1387c478bd9Sstevel@tonic-gate 	ASSERT((sctp)->sctp_condemned);			\
139f4b3ec61Sdh 	list_remove(&(sctps)->sctps_g_list, (sctp));	\
140f4b3ec61Sdh 	mutex_exit(&(sctps)->sctps_g_lock);
1417c478bd9Sstevel@tonic-gate 
1421d8c4025Svi /*
1431d8c4025Svi  * Hooks for Sun Cluster. On non-clustered nodes these will remain NULL.
1441d8c4025Svi  * PSARC/2005/602.
1451d8c4025Svi  */
1461d8c4025Svi void (*cl_sctp_listen)(sa_family_t, uchar_t *, uint_t, in_port_t) = NULL;
1471d8c4025Svi void (*cl_sctp_unlisten)(sa_family_t, uchar_t *, uint_t, in_port_t) = NULL;
1481d8c4025Svi void (*cl_sctp_connect)(sa_family_t, uchar_t *, uint_t, in_port_t,
1491d8c4025Svi     uchar_t *, uint_t, in_port_t, boolean_t, cl_sctp_handle_t) = NULL;
1501d8c4025Svi void (*cl_sctp_disconnect)(sa_family_t, cl_sctp_handle_t) = NULL;
1511d8c4025Svi void (*cl_sctp_assoc_change)(sa_family_t, uchar_t *, size_t, uint_t,
1521d8c4025Svi     uchar_t *, size_t, uint_t, int, cl_sctp_handle_t) = NULL;
1531d8c4025Svi void (*cl_sctp_check_addrs)(sa_family_t, in_port_t, uchar_t **, size_t,
1541d8c4025Svi     uint_t *, boolean_t) = NULL;
1557c478bd9Sstevel@tonic-gate /*
1567c478bd9Sstevel@tonic-gate  * Return the version number of the SCTP kernel interface.
1577c478bd9Sstevel@tonic-gate  */
1587c478bd9Sstevel@tonic-gate int
1597c478bd9Sstevel@tonic-gate sctp_itf_ver(int cl_ver)
1607c478bd9Sstevel@tonic-gate {
1617c478bd9Sstevel@tonic-gate 	if (cl_ver != SCTP_ITF_VER)
1627c478bd9Sstevel@tonic-gate 		return (-1);
1637c478bd9Sstevel@tonic-gate 	return (SCTP_ITF_VER);
1647c478bd9Sstevel@tonic-gate }
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate /*
1677c478bd9Sstevel@tonic-gate  * Called when we need a new sctp instantiation but don't really have a
1687c478bd9Sstevel@tonic-gate  * new q to hang it off of. Copy the priv flag from the passed in structure.
1697c478bd9Sstevel@tonic-gate  */
1707c478bd9Sstevel@tonic-gate sctp_t *
1717c478bd9Sstevel@tonic-gate sctp_create_eager(sctp_t *psctp)
1727c478bd9Sstevel@tonic-gate {
1737c478bd9Sstevel@tonic-gate 	sctp_t	*sctp;
1747c478bd9Sstevel@tonic-gate 	mblk_t	*ack_mp, *hb_mp;
175*bd670b35SErik Nordmark 	conn_t	*connp;
176*bd670b35SErik Nordmark 	cred_t	*credp;
177f4b3ec61Sdh 	sctp_stack_t	*sctps = psctp->sctp_sctps;
1787c478bd9Sstevel@tonic-gate 
179f4b3ec61Sdh 	if ((connp = ipcl_conn_create(IPCL_SCTPCONN, KM_NOSLEEP,
180b34b8d1aSkcpoon 	    sctps->sctps_netstack)) == NULL) {
1817c478bd9Sstevel@tonic-gate 		return (NULL);
182f4b3ec61Sdh 	}
18345916cd2Sjpk 
1847c478bd9Sstevel@tonic-gate 	sctp = CONN2SCTP(connp);
185f4b3ec61Sdh 	sctp->sctp_sctps = sctps;
1867c478bd9Sstevel@tonic-gate 
187121e5416Skcpoon 	if ((ack_mp = sctp_timer_alloc(sctp, sctp_ack_timer,
188121e5416Skcpoon 	    KM_NOSLEEP)) == NULL ||
189121e5416Skcpoon 	    (hb_mp = sctp_timer_alloc(sctp, sctp_heartbeat_timer,
190121e5416Skcpoon 	    KM_NOSLEEP)) == NULL) {
1917c478bd9Sstevel@tonic-gate 		if (ack_mp != NULL)
1927c478bd9Sstevel@tonic-gate 			freeb(ack_mp);
193121e5416Skcpoon 		sctp_conn_clear(connp);
194f4b3ec61Sdh 		sctp->sctp_sctps = NULL;
1957c478bd9Sstevel@tonic-gate 		kmem_cache_free(sctp_conn_cache, connp);
1967c478bd9Sstevel@tonic-gate 		return (NULL);
1977c478bd9Sstevel@tonic-gate 	}
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate 	sctp->sctp_ack_mp = ack_mp;
2007c478bd9Sstevel@tonic-gate 	sctp->sctp_heartbeat_mp = hb_mp;
2017c478bd9Sstevel@tonic-gate 
2027c478bd9Sstevel@tonic-gate 	if (sctp_init_values(sctp, psctp, KM_NOSLEEP) != 0) {
2037c478bd9Sstevel@tonic-gate 		freeb(ack_mp);
2047c478bd9Sstevel@tonic-gate 		freeb(hb_mp);
205121e5416Skcpoon 		sctp_conn_clear(connp);
206f4b3ec61Sdh 		sctp->sctp_sctps = NULL;
2077c478bd9Sstevel@tonic-gate 		kmem_cache_free(sctp_conn_cache, connp);
2087c478bd9Sstevel@tonic-gate 		return (NULL);
2097c478bd9Sstevel@tonic-gate 	}
21045916cd2Sjpk 
211*bd670b35SErik Nordmark 	if ((credp = psctp->sctp_connp->conn_cred) != NULL) {
21245916cd2Sjpk 		connp->conn_cred = credp;
21345916cd2Sjpk 		crhold(credp);
2147c478bd9Sstevel@tonic-gate 	}
2155d0bc3edSsommerfe 
2167c478bd9Sstevel@tonic-gate 	sctp->sctp_mss = psctp->sctp_mss;
2177c478bd9Sstevel@tonic-gate 	sctp->sctp_detached = B_TRUE;
2187c478bd9Sstevel@tonic-gate 	/*
2197c478bd9Sstevel@tonic-gate 	 * Link to the global as soon as possible so that this sctp_t
2207c478bd9Sstevel@tonic-gate 	 * can be found.
2217c478bd9Sstevel@tonic-gate 	 */
222f4b3ec61Sdh 	SCTP_LINK(sctp, sctps);
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate 	return (sctp);
2257c478bd9Sstevel@tonic-gate }
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate /*
2287c478bd9Sstevel@tonic-gate  * We are dying for some reason.  Try to do it gracefully.
2297c478bd9Sstevel@tonic-gate  */
2307c478bd9Sstevel@tonic-gate void
2317c478bd9Sstevel@tonic-gate sctp_clean_death(sctp_t *sctp, int err)
2327c478bd9Sstevel@tonic-gate {
2337c478bd9Sstevel@tonic-gate 	ASSERT(sctp != NULL);
2347c478bd9Sstevel@tonic-gate 
23545916cd2Sjpk 	dprint(3, ("sctp_clean_death %p, state %d\n", (void *)sctp,
23645916cd2Sjpk 	    sctp->sctp_state));
2377c478bd9Sstevel@tonic-gate 
2387c478bd9Sstevel@tonic-gate 	sctp->sctp_client_errno = err;
2397c478bd9Sstevel@tonic-gate 	/*
2407c478bd9Sstevel@tonic-gate 	 * Check to see if we need to notify upper layer.
2417c478bd9Sstevel@tonic-gate 	 */
2427c478bd9Sstevel@tonic-gate 	if ((sctp->sctp_state >= SCTPS_COOKIE_WAIT) &&
2437c478bd9Sstevel@tonic-gate 	    !SCTP_IS_DETACHED(sctp)) {
2447c478bd9Sstevel@tonic-gate 		if (sctp->sctp_xmit_head || sctp->sctp_xmit_unsent) {
2457c478bd9Sstevel@tonic-gate 			sctp_regift_xmitlist(sctp);
2467c478bd9Sstevel@tonic-gate 		}
2470f1702c5SYu Xiangning 		if (sctp->sctp_ulp_disconnected(sctp->sctp_ulpd, 0, err)) {
2487c478bd9Sstevel@tonic-gate 			/*
2497c478bd9Sstevel@tonic-gate 			 * Socket is gone, detach.
2507c478bd9Sstevel@tonic-gate 			 */
2517c478bd9Sstevel@tonic-gate 			sctp->sctp_detached = B_TRUE;
2527c478bd9Sstevel@tonic-gate 			sctp->sctp_ulpd = NULL;
2530f1702c5SYu Xiangning 			sctp->sctp_upcalls = NULL;
2547c478bd9Sstevel@tonic-gate 		}
2557c478bd9Sstevel@tonic-gate 	}
2567c478bd9Sstevel@tonic-gate 
2577c478bd9Sstevel@tonic-gate 	/* Remove this sctp from all hashes. */
2587c478bd9Sstevel@tonic-gate 	sctp_closei_local(sctp);
2597c478bd9Sstevel@tonic-gate 
2607c478bd9Sstevel@tonic-gate 	/*
2617c478bd9Sstevel@tonic-gate 	 * If the sctp_t is detached, we need to finish freeing up
2627c478bd9Sstevel@tonic-gate 	 * the resources.  At this point, ip_fanout_sctp() should have
2637c478bd9Sstevel@tonic-gate 	 * a hold on this sctp_t.  Some thread doing snmp stuff can
2647c478bd9Sstevel@tonic-gate 	 * have a hold.  And a taskq can also have a hold waiting to
2657c478bd9Sstevel@tonic-gate 	 * work.  sctp_unlink() the sctp_t from the global list so
2667c478bd9Sstevel@tonic-gate 	 * that no new thread can find it.  Then do a SCTP_REFRELE().
2677c478bd9Sstevel@tonic-gate 	 * The sctp_t will be freed after all those threads are done.
2687c478bd9Sstevel@tonic-gate 	 */
2697c478bd9Sstevel@tonic-gate 	if (SCTP_IS_DETACHED(sctp)) {
2707c478bd9Sstevel@tonic-gate 		SCTP_CONDEMNED(sctp);
2717c478bd9Sstevel@tonic-gate 		SCTP_REFRELE(sctp);
2727c478bd9Sstevel@tonic-gate 	}
2737c478bd9Sstevel@tonic-gate }
2747c478bd9Sstevel@tonic-gate 
2757c478bd9Sstevel@tonic-gate /*
2767c478bd9Sstevel@tonic-gate  * Called by upper layer when it wants to close this association.
2777c478bd9Sstevel@tonic-gate  * Depending on the state of this assoication, we need to do
2787c478bd9Sstevel@tonic-gate  * different things.
2797c478bd9Sstevel@tonic-gate  *
2807c478bd9Sstevel@tonic-gate  * If the state is below COOKIE_ECHOED or it is COOKIE_ECHOED but with
2817c478bd9Sstevel@tonic-gate  * no sent data, just remove this sctp from all the hashes.  This
2827c478bd9Sstevel@tonic-gate  * makes sure that all packets from the other end will go to the default
2837c478bd9Sstevel@tonic-gate  * sctp handling.  The upper layer will then do a sctp_close() to clean
2847c478bd9Sstevel@tonic-gate  * up.
2857c478bd9Sstevel@tonic-gate  *
2867c478bd9Sstevel@tonic-gate  * Otherwise, check and see if SO_LINGER is set.  If it is set, check
2877c478bd9Sstevel@tonic-gate  * the value.  If the value is 0, consider this an abortive close.  Send
2887c478bd9Sstevel@tonic-gate  * an ABORT message and kill the associatiion.
2897c478bd9Sstevel@tonic-gate  *
2907c478bd9Sstevel@tonic-gate  */
2917c478bd9Sstevel@tonic-gate int
2927c478bd9Sstevel@tonic-gate sctp_disconnect(sctp_t *sctp)
2937c478bd9Sstevel@tonic-gate {
294*bd670b35SErik Nordmark 	int		error = 0;
295*bd670b35SErik Nordmark 	conn_t		*connp = sctp->sctp_connp;
2967c478bd9Sstevel@tonic-gate 
29745916cd2Sjpk 	dprint(3, ("sctp_disconnect %p, state %d\n", (void *)sctp,
29845916cd2Sjpk 	    sctp->sctp_state));
2997c478bd9Sstevel@tonic-gate 
3007c478bd9Sstevel@tonic-gate 	RUN_SCTP(sctp);
3017c478bd9Sstevel@tonic-gate 
3027c478bd9Sstevel@tonic-gate 	switch (sctp->sctp_state) {
3037c478bd9Sstevel@tonic-gate 	case SCTPS_IDLE:
3047c478bd9Sstevel@tonic-gate 	case SCTPS_BOUND:
3057c478bd9Sstevel@tonic-gate 	case SCTPS_LISTEN:
3067c478bd9Sstevel@tonic-gate 		break;
3077c478bd9Sstevel@tonic-gate 	case SCTPS_COOKIE_WAIT:
3087c478bd9Sstevel@tonic-gate 	case SCTPS_COOKIE_ECHOED:
3097c478bd9Sstevel@tonic-gate 		/*
3107c478bd9Sstevel@tonic-gate 		 * Close during the connect 3-way handshake
3117c478bd9Sstevel@tonic-gate 		 * but here there may or may not be pending data
3127c478bd9Sstevel@tonic-gate 		 * already on queue. Process almost same as in
3137c478bd9Sstevel@tonic-gate 		 * the ESTABLISHED state.
3147c478bd9Sstevel@tonic-gate 		 */
3157c478bd9Sstevel@tonic-gate 		if (sctp->sctp_xmit_head == NULL &&
3167c478bd9Sstevel@tonic-gate 		    sctp->sctp_xmit_unsent == NULL) {
3177c478bd9Sstevel@tonic-gate 			break;
3187c478bd9Sstevel@tonic-gate 		}
3197c478bd9Sstevel@tonic-gate 		/* FALLTHRU */
3207c478bd9Sstevel@tonic-gate 	default:
3217c478bd9Sstevel@tonic-gate 		/*
322e6f13f86SKacheong Poon 		 * If SO_LINGER has set a zero linger time, terminate the
323e6f13f86SKacheong Poon 		 * association and send an ABORT.
3247c478bd9Sstevel@tonic-gate 		 */
325*bd670b35SErik Nordmark 		if (connp->conn_linger && connp->conn_lingertime == 0) {
326e6f13f86SKacheong Poon 			sctp_user_abort(sctp, NULL);
327e6f13f86SKacheong Poon 			WAKE_SCTP(sctp);
328e6f13f86SKacheong Poon 			return (error);
3297c478bd9Sstevel@tonic-gate 		}
3307c478bd9Sstevel@tonic-gate 
3311d8c4025Svi 		/*
332e6f13f86SKacheong Poon 		 * In there is unread data, send an ABORT and terminate the
333e6f13f86SKacheong Poon 		 * association.
3341d8c4025Svi 		 */
3351d8c4025Svi 		if (sctp->sctp_rxqueued > 0 || sctp->sctp_irwnd >
3361d8c4025Svi 		    sctp->sctp_rwnd) {
337e6f13f86SKacheong Poon 			sctp_user_abort(sctp, NULL);
338e6f13f86SKacheong Poon 			WAKE_SCTP(sctp);
339e6f13f86SKacheong Poon 			return (error);
3401d8c4025Svi 		}
3417c478bd9Sstevel@tonic-gate 		/*
3427c478bd9Sstevel@tonic-gate 		 * Transmit the shutdown before detaching the sctp_t.
3437c478bd9Sstevel@tonic-gate 		 * After sctp_detach returns this queue/perimeter
3447c478bd9Sstevel@tonic-gate 		 * no longer owns the sctp_t thus others can modify it.
3457c478bd9Sstevel@tonic-gate 		 */
3467c478bd9Sstevel@tonic-gate 		sctp_send_shutdown(sctp, 0);
3477c478bd9Sstevel@tonic-gate 
3487c478bd9Sstevel@tonic-gate 		/* Pass gathered wisdom to IP for keeping */
349*bd670b35SErik Nordmark 		sctp_update_dce(sctp);
3507c478bd9Sstevel@tonic-gate 
3517c478bd9Sstevel@tonic-gate 		/*
3527c478bd9Sstevel@tonic-gate 		 * If lingering on close then wait until the shutdown
3537c478bd9Sstevel@tonic-gate 		 * is complete, or the SO_LINGER time passes, or an
3547c478bd9Sstevel@tonic-gate 		 * ABORT is sent/received.  Note that sctp_disconnect()
3557c478bd9Sstevel@tonic-gate 		 * can be called more than once.  Make sure that only
3567c478bd9Sstevel@tonic-gate 		 * one thread waits.
3577c478bd9Sstevel@tonic-gate 		 */
358*bd670b35SErik Nordmark 		if (connp->conn_linger && connp->conn_lingertime > 0 &&
3597c478bd9Sstevel@tonic-gate 		    sctp->sctp_state >= SCTPS_ESTABLISHED &&
3607c478bd9Sstevel@tonic-gate 		    !sctp->sctp_lingering) {
3617c478bd9Sstevel@tonic-gate 			clock_t stoptime;	/* in ticks */
3627c478bd9Sstevel@tonic-gate 			clock_t ret;
3637c478bd9Sstevel@tonic-gate 
3647c478bd9Sstevel@tonic-gate 			sctp->sctp_lingering = 1;
3657c478bd9Sstevel@tonic-gate 			sctp->sctp_client_errno = 0;
366*bd670b35SErik Nordmark 			stoptime = lbolt + connp->conn_lingertime * hz;
3677c478bd9Sstevel@tonic-gate 
3687c478bd9Sstevel@tonic-gate 			mutex_enter(&sctp->sctp_lock);
3697c478bd9Sstevel@tonic-gate 			sctp->sctp_running = B_FALSE;
3707c478bd9Sstevel@tonic-gate 			while (sctp->sctp_state >= SCTPS_ESTABLISHED &&
3717c478bd9Sstevel@tonic-gate 			    sctp->sctp_client_errno == 0) {
3727c478bd9Sstevel@tonic-gate 				cv_broadcast(&sctp->sctp_cv);
3737c478bd9Sstevel@tonic-gate 				ret = cv_timedwait_sig(&sctp->sctp_cv,
3747c478bd9Sstevel@tonic-gate 				    &sctp->sctp_lock, stoptime);
3757c478bd9Sstevel@tonic-gate 				if (ret < 0) {
3767c478bd9Sstevel@tonic-gate 					/* Stoptime has reached. */
3777c478bd9Sstevel@tonic-gate 					sctp->sctp_client_errno = EWOULDBLOCK;
3787c478bd9Sstevel@tonic-gate 					break;
3797c478bd9Sstevel@tonic-gate 				} else if (ret == 0) {
3807c478bd9Sstevel@tonic-gate 					/* Got a signal. */
3817c478bd9Sstevel@tonic-gate 					break;
3827c478bd9Sstevel@tonic-gate 				}
3837c478bd9Sstevel@tonic-gate 			}
3847c478bd9Sstevel@tonic-gate 			error = sctp->sctp_client_errno;
3857c478bd9Sstevel@tonic-gate 			sctp->sctp_client_errno = 0;
3867c478bd9Sstevel@tonic-gate 			mutex_exit(&sctp->sctp_lock);
3877c478bd9Sstevel@tonic-gate 		}
3887c478bd9Sstevel@tonic-gate 
3897c478bd9Sstevel@tonic-gate 		WAKE_SCTP(sctp);
3907c478bd9Sstevel@tonic-gate 		return (error);
3917c478bd9Sstevel@tonic-gate 	}
3927c478bd9Sstevel@tonic-gate 
3937c478bd9Sstevel@tonic-gate 
3947c478bd9Sstevel@tonic-gate 	/* Remove this sctp from all hashes so nobody can find it. */
3957c478bd9Sstevel@tonic-gate 	sctp_closei_local(sctp);
3967c478bd9Sstevel@tonic-gate 	WAKE_SCTP(sctp);
3977c478bd9Sstevel@tonic-gate 	return (error);
3987c478bd9Sstevel@tonic-gate }
3997c478bd9Sstevel@tonic-gate 
4007c478bd9Sstevel@tonic-gate void
4017c478bd9Sstevel@tonic-gate sctp_close(sctp_t *sctp)
4027c478bd9Sstevel@tonic-gate {
40345916cd2Sjpk 	dprint(3, ("sctp_close %p, state %d\n", (void *)sctp,
40445916cd2Sjpk 	    sctp->sctp_state));
4057c478bd9Sstevel@tonic-gate 
4067c478bd9Sstevel@tonic-gate 	RUN_SCTP(sctp);
4077c478bd9Sstevel@tonic-gate 	sctp->sctp_detached = 1;
4087c478bd9Sstevel@tonic-gate 	sctp->sctp_ulpd = NULL;
4090f1702c5SYu Xiangning 	sctp->sctp_upcalls = NULL;
4107c478bd9Sstevel@tonic-gate 	bzero(&sctp->sctp_events, sizeof (sctp->sctp_events));
4117c478bd9Sstevel@tonic-gate 
4127c478bd9Sstevel@tonic-gate 	/* If the graceful shutdown has not been completed, just return. */
4137c478bd9Sstevel@tonic-gate 	if (sctp->sctp_state != SCTPS_IDLE) {
4147c478bd9Sstevel@tonic-gate 		WAKE_SCTP(sctp);
4157c478bd9Sstevel@tonic-gate 		return;
4167c478bd9Sstevel@tonic-gate 	}
4177c478bd9Sstevel@tonic-gate 
4187c478bd9Sstevel@tonic-gate 	/*
4197c478bd9Sstevel@tonic-gate 	 * Since sctp_t is in SCTPS_IDLE state, so the only thread which
4207c478bd9Sstevel@tonic-gate 	 * can have a hold on the sctp_t is doing snmp stuff.  Just do
4217c478bd9Sstevel@tonic-gate 	 * a SCTP_REFRELE() here after the SCTP_UNLINK().  It will
4227c478bd9Sstevel@tonic-gate 	 * be freed when the other thread is done.
4237c478bd9Sstevel@tonic-gate 	 */
4247c478bd9Sstevel@tonic-gate 	SCTP_CONDEMNED(sctp);
4257c478bd9Sstevel@tonic-gate 	WAKE_SCTP(sctp);
4267c478bd9Sstevel@tonic-gate 	SCTP_REFRELE(sctp);
4277c478bd9Sstevel@tonic-gate }
4287c478bd9Sstevel@tonic-gate 
4297c478bd9Sstevel@tonic-gate /*
4307c478bd9Sstevel@tonic-gate  * Unlink from global list and do the eager close.
4317c478bd9Sstevel@tonic-gate  * Remove the refhold implicit in being on the global list.
4327c478bd9Sstevel@tonic-gate  */
4337c478bd9Sstevel@tonic-gate void
4347c478bd9Sstevel@tonic-gate sctp_close_eager(sctp_t *sctp)
4357c478bd9Sstevel@tonic-gate {
4367c478bd9Sstevel@tonic-gate 	SCTP_CONDEMNED(sctp);
4377c478bd9Sstevel@tonic-gate 	sctp_closei_local(sctp);
4387c478bd9Sstevel@tonic-gate 	SCTP_REFRELE(sctp);
4397c478bd9Sstevel@tonic-gate }
4407c478bd9Sstevel@tonic-gate 
4417c478bd9Sstevel@tonic-gate /*
4427c478bd9Sstevel@tonic-gate  * The sctp_t is going away. Remove it from all lists and set it
4437c478bd9Sstevel@tonic-gate  * to SCTPS_IDLE. The caller has to remove it from the
4447c478bd9Sstevel@tonic-gate  * global list. The freeing up of memory is deferred until
4457c478bd9Sstevel@tonic-gate  * sctp_free(). This is needed since a thread in sctp_input() might have
4467c478bd9Sstevel@tonic-gate  * done a SCTP_REFHOLD on this structure before it was removed from the
4477c478bd9Sstevel@tonic-gate  * hashes.
4487c478bd9Sstevel@tonic-gate  */
4497c478bd9Sstevel@tonic-gate static void
4507c478bd9Sstevel@tonic-gate sctp_closei_local(sctp_t *sctp)
4517c478bd9Sstevel@tonic-gate {
4527c478bd9Sstevel@tonic-gate 	mblk_t	*mp;
4537c478bd9Sstevel@tonic-gate 	conn_t	*connp = sctp->sctp_connp;
4547c478bd9Sstevel@tonic-gate 
455b34b8d1aSkcpoon 	/* Sanity check, don't do the same thing twice.  */
456b34b8d1aSkcpoon 	if (connp->conn_state_flags & CONN_CLOSING) {
457b34b8d1aSkcpoon 		ASSERT(sctp->sctp_state == SCTPS_IDLE);
458b34b8d1aSkcpoon 		return;
459b34b8d1aSkcpoon 	}
460b34b8d1aSkcpoon 
4617c478bd9Sstevel@tonic-gate 	/* Stop and free the timers */
4627c478bd9Sstevel@tonic-gate 	sctp_free_faddr_timers(sctp);
4637c478bd9Sstevel@tonic-gate 	if ((mp = sctp->sctp_heartbeat_mp) != NULL) {
4647c478bd9Sstevel@tonic-gate 		sctp_timer_free(mp);
4657c478bd9Sstevel@tonic-gate 		sctp->sctp_heartbeat_mp = NULL;
4667c478bd9Sstevel@tonic-gate 	}
4677c478bd9Sstevel@tonic-gate 	if ((mp = sctp->sctp_ack_mp) != NULL) {
4687c478bd9Sstevel@tonic-gate 		sctp_timer_free(mp);
4697c478bd9Sstevel@tonic-gate 		sctp->sctp_ack_mp = NULL;
4707c478bd9Sstevel@tonic-gate 	}
4717c478bd9Sstevel@tonic-gate 
4727c478bd9Sstevel@tonic-gate 	/* Set the CONN_CLOSING flag so that IP will not cache IRE again. */
4737c478bd9Sstevel@tonic-gate 	mutex_enter(&connp->conn_lock);
4747c478bd9Sstevel@tonic-gate 	connp->conn_state_flags |= CONN_CLOSING;
4757c478bd9Sstevel@tonic-gate 	mutex_exit(&connp->conn_lock);
4767c478bd9Sstevel@tonic-gate 
4777c478bd9Sstevel@tonic-gate 	/* Remove from all hashes. */
4787c478bd9Sstevel@tonic-gate 	sctp_bind_hash_remove(sctp);
4797c478bd9Sstevel@tonic-gate 	sctp_conn_hash_remove(sctp);
4807c478bd9Sstevel@tonic-gate 	sctp_listen_hash_remove(sctp);
4817c478bd9Sstevel@tonic-gate 	sctp->sctp_state = SCTPS_IDLE;
4827c478bd9Sstevel@tonic-gate 
4837c478bd9Sstevel@tonic-gate 	/*
4847c478bd9Sstevel@tonic-gate 	 * Clean up the recvq as much as possible.  All those packets
4857c478bd9Sstevel@tonic-gate 	 * will be silently dropped as this sctp_t is now in idle state.
4867c478bd9Sstevel@tonic-gate 	 */
4877c478bd9Sstevel@tonic-gate 	mutex_enter(&sctp->sctp_recvq_lock);
4887c478bd9Sstevel@tonic-gate 	while ((mp = sctp->sctp_recvq) != NULL) {
4897c478bd9Sstevel@tonic-gate 		sctp->sctp_recvq = mp->b_next;
4907c478bd9Sstevel@tonic-gate 		mp->b_next = NULL;
491*bd670b35SErik Nordmark 
492*bd670b35SErik Nordmark 		if (ip_recv_attr_is_mblk(mp))
493*bd670b35SErik Nordmark 			mp = ip_recv_attr_free_mblk(mp);
494*bd670b35SErik Nordmark 
4957c478bd9Sstevel@tonic-gate 		freemsg(mp);
4967c478bd9Sstevel@tonic-gate 	}
4977c478bd9Sstevel@tonic-gate 	mutex_exit(&sctp->sctp_recvq_lock);
4987c478bd9Sstevel@tonic-gate }
4997c478bd9Sstevel@tonic-gate 
5007c478bd9Sstevel@tonic-gate /*
5017c478bd9Sstevel@tonic-gate  * Free memory associated with the sctp/ip header template.
5027c478bd9Sstevel@tonic-gate  */
5037c478bd9Sstevel@tonic-gate static void
5047c478bd9Sstevel@tonic-gate sctp_headers_free(sctp_t *sctp)
5057c478bd9Sstevel@tonic-gate {
5067c478bd9Sstevel@tonic-gate 	if (sctp->sctp_iphc != NULL) {
5077c478bd9Sstevel@tonic-gate 		kmem_free(sctp->sctp_iphc, sctp->sctp_iphc_len);
5087c478bd9Sstevel@tonic-gate 		sctp->sctp_iphc = NULL;
5097c478bd9Sstevel@tonic-gate 		sctp->sctp_ipha = NULL;
5107c478bd9Sstevel@tonic-gate 		sctp->sctp_hdr_len = 0;
5117c478bd9Sstevel@tonic-gate 		sctp->sctp_ip_hdr_len = 0;
5127c478bd9Sstevel@tonic-gate 		sctp->sctp_iphc_len = 0;
5137c478bd9Sstevel@tonic-gate 		sctp->sctp_sctph = NULL;
5147c478bd9Sstevel@tonic-gate 		sctp->sctp_hdr_len = 0;
5157c478bd9Sstevel@tonic-gate 	}
5167c478bd9Sstevel@tonic-gate 	if (sctp->sctp_iphc6 != NULL) {
5177c478bd9Sstevel@tonic-gate 		kmem_free(sctp->sctp_iphc6, sctp->sctp_iphc6_len);
5187c478bd9Sstevel@tonic-gate 		sctp->sctp_iphc6 = NULL;
5197c478bd9Sstevel@tonic-gate 		sctp->sctp_ip6h = NULL;
5207c478bd9Sstevel@tonic-gate 		sctp->sctp_hdr6_len = 0;
5217c478bd9Sstevel@tonic-gate 		sctp->sctp_ip_hdr6_len = 0;
5227c478bd9Sstevel@tonic-gate 		sctp->sctp_iphc6_len = 0;
5237c478bd9Sstevel@tonic-gate 		sctp->sctp_sctph6 = NULL;
5247c478bd9Sstevel@tonic-gate 		sctp->sctp_hdr6_len = 0;
5257c478bd9Sstevel@tonic-gate 	}
5267c478bd9Sstevel@tonic-gate }
5277c478bd9Sstevel@tonic-gate 
5287c478bd9Sstevel@tonic-gate static void
5297c478bd9Sstevel@tonic-gate sctp_free_xmit_data(sctp_t *sctp)
5307c478bd9Sstevel@tonic-gate {
5317c478bd9Sstevel@tonic-gate 	mblk_t	*ump = NULL;
5327c478bd9Sstevel@tonic-gate 	mblk_t	*nump;
5337c478bd9Sstevel@tonic-gate 	mblk_t	*mp;
5347c478bd9Sstevel@tonic-gate 	mblk_t	*nmp;
5357c478bd9Sstevel@tonic-gate 
5367c478bd9Sstevel@tonic-gate 	sctp->sctp_xmit_unacked = NULL;
5377c478bd9Sstevel@tonic-gate 	ump = sctp->sctp_xmit_head;
5387c478bd9Sstevel@tonic-gate 	sctp->sctp_xmit_tail = sctp->sctp_xmit_head = NULL;
5397c478bd9Sstevel@tonic-gate free_unsent:
5407c478bd9Sstevel@tonic-gate 	for (; ump != NULL; ump = nump) {
5417c478bd9Sstevel@tonic-gate 		for (mp = ump->b_cont; mp != NULL; mp = nmp) {
5427c478bd9Sstevel@tonic-gate 			nmp = mp->b_next;
5437c478bd9Sstevel@tonic-gate 			mp->b_next = NULL;
5447c478bd9Sstevel@tonic-gate 			mp->b_prev = NULL;
5457c478bd9Sstevel@tonic-gate 			freemsg(mp);
5467c478bd9Sstevel@tonic-gate 		}
5477c478bd9Sstevel@tonic-gate 		ASSERT(DB_REF(ump) == 1);
5487c478bd9Sstevel@tonic-gate 		nump = ump->b_next;
5497c478bd9Sstevel@tonic-gate 		ump->b_next = NULL;
5507c478bd9Sstevel@tonic-gate 		ump->b_prev = NULL;
5517c478bd9Sstevel@tonic-gate 		ump->b_cont = NULL;
5527c478bd9Sstevel@tonic-gate 		freeb(ump);
5537c478bd9Sstevel@tonic-gate 	}
5547c478bd9Sstevel@tonic-gate 	if ((ump = sctp->sctp_xmit_unsent) == NULL) {
5557c478bd9Sstevel@tonic-gate 		ASSERT(sctp->sctp_xmit_unsent_tail == NULL);
5567c478bd9Sstevel@tonic-gate 		return;
5577c478bd9Sstevel@tonic-gate 	}
5587c478bd9Sstevel@tonic-gate 	sctp->sctp_xmit_unsent = sctp->sctp_xmit_unsent_tail = NULL;
5597c478bd9Sstevel@tonic-gate 	goto free_unsent;
5607c478bd9Sstevel@tonic-gate }
5617c478bd9Sstevel@tonic-gate 
5627c478bd9Sstevel@tonic-gate /*
5637c478bd9Sstevel@tonic-gate  * Cleanup all the messages in the stream queue and the reassembly lists.
5647c478bd9Sstevel@tonic-gate  * If 'free' is true, then delete the streams as well.
5657c478bd9Sstevel@tonic-gate  */
5667c478bd9Sstevel@tonic-gate void
5677c478bd9Sstevel@tonic-gate sctp_instream_cleanup(sctp_t *sctp, boolean_t free)
5687c478bd9Sstevel@tonic-gate {
5697c478bd9Sstevel@tonic-gate 	int	i;
5707c478bd9Sstevel@tonic-gate 	mblk_t	*mp;
5717c478bd9Sstevel@tonic-gate 	mblk_t	*mp1;
5727c478bd9Sstevel@tonic-gate 
5737c478bd9Sstevel@tonic-gate 	if (sctp->sctp_instr != NULL) {
5747c478bd9Sstevel@tonic-gate 		/* walk thru and flush out anything remaining in the Q */
5757c478bd9Sstevel@tonic-gate 		for (i = 0; i < sctp->sctp_num_istr; i++) {
5767c478bd9Sstevel@tonic-gate 			mp = sctp->sctp_instr[i].istr_msgs;
5777c478bd9Sstevel@tonic-gate 			while (mp != NULL) {
5787c478bd9Sstevel@tonic-gate 				mp1 = mp->b_next;
5797c478bd9Sstevel@tonic-gate 				mp->b_next = mp->b_prev = NULL;
5807c478bd9Sstevel@tonic-gate 				freemsg(mp);
5817c478bd9Sstevel@tonic-gate 				mp = mp1;
5827c478bd9Sstevel@tonic-gate 			}
5837c478bd9Sstevel@tonic-gate 			sctp->sctp_instr[i].istr_msgs = NULL;
584a0263d1cSnh 			sctp->sctp_instr[i].istr_nmsgs = 0;
5857c478bd9Sstevel@tonic-gate 			sctp_free_reass((sctp->sctp_instr) + i);
5867c478bd9Sstevel@tonic-gate 			sctp->sctp_instr[i].nextseq = 0;
5877c478bd9Sstevel@tonic-gate 		}
5887c478bd9Sstevel@tonic-gate 		if (free) {
5897c478bd9Sstevel@tonic-gate 			kmem_free(sctp->sctp_instr,
5907c478bd9Sstevel@tonic-gate 			    sizeof (*sctp->sctp_instr) * sctp->sctp_num_istr);
5917c478bd9Sstevel@tonic-gate 			sctp->sctp_instr = NULL;
5927c478bd9Sstevel@tonic-gate 			sctp->sctp_num_istr = 0;
5937c478bd9Sstevel@tonic-gate 		}
5947c478bd9Sstevel@tonic-gate 	}
5957c478bd9Sstevel@tonic-gate 	/* un-ordered fragments */
5967c478bd9Sstevel@tonic-gate 	if (sctp->sctp_uo_frags != NULL) {
5977c478bd9Sstevel@tonic-gate 		for (mp = sctp->sctp_uo_frags; mp != NULL; mp = mp1) {
5987c478bd9Sstevel@tonic-gate 			mp1 = mp->b_next;
5997c478bd9Sstevel@tonic-gate 			mp->b_next = mp->b_prev = NULL;
6007c478bd9Sstevel@tonic-gate 			freemsg(mp);
6017c478bd9Sstevel@tonic-gate 		}
6027c478bd9Sstevel@tonic-gate 	}
6037c478bd9Sstevel@tonic-gate }
6047c478bd9Sstevel@tonic-gate 
6057c478bd9Sstevel@tonic-gate /*
6067c478bd9Sstevel@tonic-gate  * Last reference to the sctp_t is gone. Free all memory associated with it.
6077c478bd9Sstevel@tonic-gate  * Called from SCTP_REFRELE. Called inline in sctp_close()
6087c478bd9Sstevel@tonic-gate  */
6097c478bd9Sstevel@tonic-gate void
6107c478bd9Sstevel@tonic-gate sctp_free(conn_t *connp)
6117c478bd9Sstevel@tonic-gate {
6127c478bd9Sstevel@tonic-gate 	sctp_t *sctp = CONN2SCTP(connp);
6137c478bd9Sstevel@tonic-gate 	int		cnt;
614f4b3ec61Sdh 	sctp_stack_t	*sctps = sctp->sctp_sctps;
6157c478bd9Sstevel@tonic-gate 
616f4b3ec61Sdh 	ASSERT(sctps != NULL);
6177c478bd9Sstevel@tonic-gate 	/* Unlink it from the global list */
618f4b3ec61Sdh 	SCTP_UNLINK(sctp, sctps);
6197c478bd9Sstevel@tonic-gate 
6207c478bd9Sstevel@tonic-gate 	ASSERT(connp->conn_ref == 0);
621*bd670b35SErik Nordmark 	ASSERT(connp->conn_proto == IPPROTO_SCTP);
6227c478bd9Sstevel@tonic-gate 	ASSERT(!MUTEX_HELD(&sctp->sctp_reflock));
6237c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_refcnt == 0);
6247c478bd9Sstevel@tonic-gate 
6257c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_ptpbhn == NULL && sctp->sctp_bind_hash == NULL);
6267c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_conn_hash_next == NULL &&
6277c478bd9Sstevel@tonic-gate 	    sctp->sctp_conn_hash_prev == NULL);
6287c478bd9Sstevel@tonic-gate 
6297c478bd9Sstevel@tonic-gate 
6307c478bd9Sstevel@tonic-gate 	/* Free up all the resources. */
6317c478bd9Sstevel@tonic-gate 
6327c478bd9Sstevel@tonic-gate 	/* blow away sctp stream management */
6337c478bd9Sstevel@tonic-gate 	if (sctp->sctp_ostrcntrs != NULL) {
6347c478bd9Sstevel@tonic-gate 		kmem_free(sctp->sctp_ostrcntrs,
6357c478bd9Sstevel@tonic-gate 		    sizeof (uint16_t) * sctp->sctp_num_ostr);
6367c478bd9Sstevel@tonic-gate 		sctp->sctp_ostrcntrs = NULL;
6377c478bd9Sstevel@tonic-gate 	}
6387c478bd9Sstevel@tonic-gate 	sctp_instream_cleanup(sctp, B_TRUE);
6397c478bd9Sstevel@tonic-gate 
6407c478bd9Sstevel@tonic-gate 	/* Remove all data transfer resources. */
6417c478bd9Sstevel@tonic-gate 	sctp->sctp_istr_nmsgs = 0;
6427c478bd9Sstevel@tonic-gate 	sctp->sctp_rxqueued = 0;
6437c478bd9Sstevel@tonic-gate 	sctp_free_xmit_data(sctp);
6447c478bd9Sstevel@tonic-gate 	sctp->sctp_unacked = 0;
6457c478bd9Sstevel@tonic-gate 	sctp->sctp_unsent = 0;
6461d8c4025Svi 	if (sctp->sctp_cxmit_list != NULL)
6471d8c4025Svi 		sctp_asconf_free_cxmit(sctp, NULL);
6481d8c4025Svi 
6497c478bd9Sstevel@tonic-gate 	sctp->sctp_lastdata = NULL;
6507c478bd9Sstevel@tonic-gate 
6517c478bd9Sstevel@tonic-gate 	/* Clear out default xmit settings */
6527c478bd9Sstevel@tonic-gate 	sctp->sctp_def_stream = 0;
6537c478bd9Sstevel@tonic-gate 	sctp->sctp_def_flags = 0;
6547c478bd9Sstevel@tonic-gate 	sctp->sctp_def_ppid = 0;
6557c478bd9Sstevel@tonic-gate 	sctp->sctp_def_context = 0;
6567c478bd9Sstevel@tonic-gate 	sctp->sctp_def_timetolive = 0;
6577c478bd9Sstevel@tonic-gate 
6587c478bd9Sstevel@tonic-gate 	if (sctp->sctp_sack_info != NULL) {
6597c478bd9Sstevel@tonic-gate 		sctp_free_set(sctp->sctp_sack_info);
6607c478bd9Sstevel@tonic-gate 		sctp->sctp_sack_info = NULL;
6617c478bd9Sstevel@tonic-gate 	}
6627c478bd9Sstevel@tonic-gate 	sctp->sctp_sack_gaps = 0;
6637c478bd9Sstevel@tonic-gate 
6647c478bd9Sstevel@tonic-gate 	if (sctp->sctp_cookie_mp != NULL) {
6657c478bd9Sstevel@tonic-gate 		freemsg(sctp->sctp_cookie_mp);
6667c478bd9Sstevel@tonic-gate 		sctp->sctp_cookie_mp = NULL;
6677c478bd9Sstevel@tonic-gate 	}
6687c478bd9Sstevel@tonic-gate 
6697c478bd9Sstevel@tonic-gate 	/* Remove all the address resources. */
6707c478bd9Sstevel@tonic-gate 	sctp_zap_addrs(sctp);
6717c478bd9Sstevel@tonic-gate 	for (cnt = 0; cnt < SCTP_IPIF_HASH; cnt++) {
6727c478bd9Sstevel@tonic-gate 		ASSERT(sctp->sctp_saddrs[cnt].ipif_count == 0);
6737c478bd9Sstevel@tonic-gate 		list_destroy(&sctp->sctp_saddrs[cnt].sctp_ipif_list);
6747c478bd9Sstevel@tonic-gate 	}
6757c478bd9Sstevel@tonic-gate 
6767c478bd9Sstevel@tonic-gate 	if (sctp->sctp_hopopts != NULL) {
6777c478bd9Sstevel@tonic-gate 		mi_free(sctp->sctp_hopopts);
6787c478bd9Sstevel@tonic-gate 		sctp->sctp_hopopts = NULL;
6797c478bd9Sstevel@tonic-gate 		sctp->sctp_hopoptslen = 0;
6807c478bd9Sstevel@tonic-gate 	}
6817c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_hopoptslen == 0);
6827c478bd9Sstevel@tonic-gate 	if (sctp->sctp_dstopts != NULL) {
6837c478bd9Sstevel@tonic-gate 		mi_free(sctp->sctp_dstopts);
6847c478bd9Sstevel@tonic-gate 		sctp->sctp_dstopts = NULL;
6857c478bd9Sstevel@tonic-gate 		sctp->sctp_dstoptslen = 0;
6867c478bd9Sstevel@tonic-gate 	}
6877c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_dstoptslen == 0);
688*bd670b35SErik Nordmark 	if (sctp->sctp_rthdrdstopts != NULL) {
689*bd670b35SErik Nordmark 		mi_free(sctp->sctp_rthdrdstopts);
690*bd670b35SErik Nordmark 		sctp->sctp_rthdrdstopts = NULL;
691*bd670b35SErik Nordmark 		sctp->sctp_rthdrdstoptslen = 0;
6927c478bd9Sstevel@tonic-gate 	}
693*bd670b35SErik Nordmark 	ASSERT(sctp->sctp_rthdrdstoptslen == 0);
6947c478bd9Sstevel@tonic-gate 	if (sctp->sctp_rthdr != NULL) {
6957c478bd9Sstevel@tonic-gate 		mi_free(sctp->sctp_rthdr);
6967c478bd9Sstevel@tonic-gate 		sctp->sctp_rthdr = NULL;
6977c478bd9Sstevel@tonic-gate 		sctp->sctp_rthdrlen = 0;
6987c478bd9Sstevel@tonic-gate 	}
6997c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_rthdrlen == 0);
7007c478bd9Sstevel@tonic-gate 	sctp_headers_free(sctp);
7017c478bd9Sstevel@tonic-gate 
7027c478bd9Sstevel@tonic-gate 	sctp->sctp_shutdown_faddr = NULL;
7037c478bd9Sstevel@tonic-gate 
7047f093707Skcpoon 	if (sctp->sctp_err_chunks != NULL) {
7057f093707Skcpoon 		freemsg(sctp->sctp_err_chunks);
7067f093707Skcpoon 		sctp->sctp_err_chunks = NULL;
7077f093707Skcpoon 		sctp->sctp_err_len = 0;
7087f093707Skcpoon 	}
7097f093707Skcpoon 
7107c478bd9Sstevel@tonic-gate 	/* Clear all the bitfields. */
7117c478bd9Sstevel@tonic-gate 	bzero(&sctp->sctp_bits, sizeof (sctp->sctp_bits));
7127c478bd9Sstevel@tonic-gate 
7137c478bd9Sstevel@tonic-gate 	/* It is time to update the global statistics. */
714f4b3ec61Sdh 	UPDATE_MIB(&sctps->sctps_mib, sctpOutSCTPPkts, sctp->sctp_opkts);
715f4b3ec61Sdh 	UPDATE_MIB(&sctps->sctps_mib, sctpOutCtrlChunks, sctp->sctp_obchunks);
716f4b3ec61Sdh 	UPDATE_MIB(&sctps->sctps_mib, sctpOutOrderChunks, sctp->sctp_odchunks);
717f4b3ec61Sdh 	UPDATE_MIB(&sctps->sctps_mib,
718f4b3ec61Sdh 	    sctpOutUnorderChunks, sctp->sctp_oudchunks);
719f4b3ec61Sdh 	UPDATE_MIB(&sctps->sctps_mib, sctpRetransChunks, sctp->sctp_rxtchunks);
720f4b3ec61Sdh 	UPDATE_MIB(&sctps->sctps_mib, sctpInSCTPPkts, sctp->sctp_ipkts);
721f4b3ec61Sdh 	UPDATE_MIB(&sctps->sctps_mib, sctpInCtrlChunks, sctp->sctp_ibchunks);
722f4b3ec61Sdh 	UPDATE_MIB(&sctps->sctps_mib, sctpInOrderChunks, sctp->sctp_idchunks);
723f4b3ec61Sdh 	UPDATE_MIB(&sctps->sctps_mib,
724f4b3ec61Sdh 	    sctpInUnorderChunks, sctp->sctp_iudchunks);
725f4b3ec61Sdh 	UPDATE_MIB(&sctps->sctps_mib, sctpFragUsrMsgs, sctp->sctp_fragdmsgs);
726f4b3ec61Sdh 	UPDATE_MIB(&sctps->sctps_mib, sctpReasmUsrMsgs, sctp->sctp_reassmsgs);
7277c478bd9Sstevel@tonic-gate 	sctp->sctp_opkts = 0;
7287c478bd9Sstevel@tonic-gate 	sctp->sctp_obchunks = 0;
7297c478bd9Sstevel@tonic-gate 	sctp->sctp_odchunks = 0;
7307c478bd9Sstevel@tonic-gate 	sctp->sctp_oudchunks = 0;
7317c478bd9Sstevel@tonic-gate 	sctp->sctp_rxtchunks = 0;
7327c478bd9Sstevel@tonic-gate 	sctp->sctp_ipkts = 0;
7337c478bd9Sstevel@tonic-gate 	sctp->sctp_ibchunks = 0;
7347c478bd9Sstevel@tonic-gate 	sctp->sctp_idchunks = 0;
7357c478bd9Sstevel@tonic-gate 	sctp->sctp_iudchunks = 0;
7367c478bd9Sstevel@tonic-gate 	sctp->sctp_fragdmsgs = 0;
7377c478bd9Sstevel@tonic-gate 	sctp->sctp_reassmsgs = 0;
73835e12f9cSGeorge Shepherd 	sctp->sctp_outseqtsns = 0;
73935e12f9cSGeorge Shepherd 	sctp->sctp_osacks = 0;
74035e12f9cSGeorge Shepherd 	sctp->sctp_isacks = 0;
74135e12f9cSGeorge Shepherd 	sctp->sctp_idupchunks = 0;
74235e12f9cSGeorge Shepherd 	sctp->sctp_gapcnt = 0;
74335e12f9cSGeorge Shepherd 	sctp->sctp_cum_obchunks = 0;
74435e12f9cSGeorge Shepherd 	sctp->sctp_cum_odchunks = 0;
74535e12f9cSGeorge Shepherd 	sctp->sctp_cum_oudchunks = 0;
74635e12f9cSGeorge Shepherd 	sctp->sctp_cum_rxtchunks = 0;
74735e12f9cSGeorge Shepherd 	sctp->sctp_cum_ibchunks = 0;
74835e12f9cSGeorge Shepherd 	sctp->sctp_cum_idchunks = 0;
74935e12f9cSGeorge Shepherd 	sctp->sctp_cum_iudchunks = 0;
7507c478bd9Sstevel@tonic-gate 
7517c478bd9Sstevel@tonic-gate 	sctp->sctp_autoclose = 0;
752558fbd03Skcpoon 	sctp->sctp_tx_adaptation_code = 0;
7537c478bd9Sstevel@tonic-gate 
7547f99e6fcSkp 	sctp->sctp_v6label_len = 0;
7557f99e6fcSkp 	sctp->sctp_v4label_len = 0;
7567f99e6fcSkp 
757f4b3ec61Sdh 	sctp->sctp_sctps = NULL;
758f4b3ec61Sdh 
759121e5416Skcpoon 	sctp_conn_clear(connp);
7607c478bd9Sstevel@tonic-gate 	kmem_cache_free(sctp_conn_cache, connp);
7617c478bd9Sstevel@tonic-gate }
7627c478bd9Sstevel@tonic-gate 
7637c478bd9Sstevel@tonic-gate /* Diagnostic routine used to return a string associated with the sctp state. */
7647c478bd9Sstevel@tonic-gate char *
7657c478bd9Sstevel@tonic-gate sctp_display(sctp_t *sctp, char *sup_buf)
7667c478bd9Sstevel@tonic-gate {
7677c478bd9Sstevel@tonic-gate 	char	*buf;
7687c478bd9Sstevel@tonic-gate 	char	buf1[30];
7697c478bd9Sstevel@tonic-gate 	static char	priv_buf[INET6_ADDRSTRLEN * 2 + 80];
7707c478bd9Sstevel@tonic-gate 	char	*cp;
771*bd670b35SErik Nordmark 	conn_t	*connp;
7727c478bd9Sstevel@tonic-gate 
7737c478bd9Sstevel@tonic-gate 	if (sctp == NULL)
7747c478bd9Sstevel@tonic-gate 		return ("NULL_SCTP");
7757c478bd9Sstevel@tonic-gate 
776*bd670b35SErik Nordmark 	connp = sctp->sctp_connp;
7777c478bd9Sstevel@tonic-gate 	buf = (sup_buf != NULL) ? sup_buf : priv_buf;
7787c478bd9Sstevel@tonic-gate 
7797c478bd9Sstevel@tonic-gate 	switch (sctp->sctp_state) {
7807c478bd9Sstevel@tonic-gate 	case SCTPS_IDLE:
7817c478bd9Sstevel@tonic-gate 		cp = "SCTP_IDLE";
7827c478bd9Sstevel@tonic-gate 		break;
7837c478bd9Sstevel@tonic-gate 	case SCTPS_BOUND:
7847c478bd9Sstevel@tonic-gate 		cp = "SCTP_BOUND";
7857c478bd9Sstevel@tonic-gate 		break;
7867c478bd9Sstevel@tonic-gate 	case SCTPS_LISTEN:
7877c478bd9Sstevel@tonic-gate 		cp = "SCTP_LISTEN";
7887c478bd9Sstevel@tonic-gate 		break;
7897c478bd9Sstevel@tonic-gate 	case SCTPS_COOKIE_WAIT:
7907c478bd9Sstevel@tonic-gate 		cp = "SCTP_COOKIE_WAIT";
7917c478bd9Sstevel@tonic-gate 		break;
7927c478bd9Sstevel@tonic-gate 	case SCTPS_COOKIE_ECHOED:
7937c478bd9Sstevel@tonic-gate 		cp = "SCTP_COOKIE_ECHOED";
7947c478bd9Sstevel@tonic-gate 		break;
7957c478bd9Sstevel@tonic-gate 	case SCTPS_ESTABLISHED:
7967c478bd9Sstevel@tonic-gate 		cp = "SCTP_ESTABLISHED";
7977c478bd9Sstevel@tonic-gate 		break;
7987c478bd9Sstevel@tonic-gate 	case SCTPS_SHUTDOWN_PENDING:
7997c478bd9Sstevel@tonic-gate 		cp = "SCTP_SHUTDOWN_PENDING";
8007c478bd9Sstevel@tonic-gate 		break;
8017c478bd9Sstevel@tonic-gate 	case SCTPS_SHUTDOWN_SENT:
8027c478bd9Sstevel@tonic-gate 		cp = "SCTPS_SHUTDOWN_SENT";
8037c478bd9Sstevel@tonic-gate 		break;
8047c478bd9Sstevel@tonic-gate 	case SCTPS_SHUTDOWN_RECEIVED:
8057c478bd9Sstevel@tonic-gate 		cp = "SCTPS_SHUTDOWN_RECEIVED";
8067c478bd9Sstevel@tonic-gate 		break;
8077c478bd9Sstevel@tonic-gate 	case SCTPS_SHUTDOWN_ACK_SENT:
8087c478bd9Sstevel@tonic-gate 		cp = "SCTPS_SHUTDOWN_ACK_SENT";
8097c478bd9Sstevel@tonic-gate 		break;
8107c478bd9Sstevel@tonic-gate 	default:
8117c478bd9Sstevel@tonic-gate 		(void) mi_sprintf(buf1, "SCTPUnkState(%d)", sctp->sctp_state);
8127c478bd9Sstevel@tonic-gate 		cp = buf1;
8137c478bd9Sstevel@tonic-gate 		break;
8147c478bd9Sstevel@tonic-gate 	}
8157c478bd9Sstevel@tonic-gate 	(void) mi_sprintf(buf, "[%u, %u] %s",
816*bd670b35SErik Nordmark 	    ntohs(connp->conn_lport), ntohs(connp->conn_fport), cp);
8177c478bd9Sstevel@tonic-gate 
8187c478bd9Sstevel@tonic-gate 	return (buf);
8197c478bd9Sstevel@tonic-gate }
8207c478bd9Sstevel@tonic-gate 
8217c478bd9Sstevel@tonic-gate /*
8227c478bd9Sstevel@tonic-gate  * Initialize protocol control block. If a parent exists, inherit
8237c478bd9Sstevel@tonic-gate  * all values set through setsockopt().
8247c478bd9Sstevel@tonic-gate  */
8257c478bd9Sstevel@tonic-gate static int
8267c478bd9Sstevel@tonic-gate sctp_init_values(sctp_t *sctp, sctp_t *psctp, int sleep)
8277c478bd9Sstevel@tonic-gate {
8287c478bd9Sstevel@tonic-gate 	int	err;
8297c478bd9Sstevel@tonic-gate 	int	cnt;
830f4b3ec61Sdh 	sctp_stack_t	*sctps = sctp->sctp_sctps;
831*bd670b35SErik Nordmark 	conn_t 	*connp;
8327c478bd9Sstevel@tonic-gate 
833*bd670b35SErik Nordmark 	connp = sctp->sctp_connp;
8347c478bd9Sstevel@tonic-gate 
8357c478bd9Sstevel@tonic-gate 	sctp->sctp_nsaddrs = 0;
8367c478bd9Sstevel@tonic-gate 	for (cnt = 0; cnt < SCTP_IPIF_HASH; cnt++) {
8377c478bd9Sstevel@tonic-gate 		sctp->sctp_saddrs[cnt].ipif_count = 0;
8387c478bd9Sstevel@tonic-gate 		list_create(&sctp->sctp_saddrs[cnt].sctp_ipif_list,
8397c478bd9Sstevel@tonic-gate 		    sizeof (sctp_saddr_ipif_t), offsetof(sctp_saddr_ipif_t,
8407c478bd9Sstevel@tonic-gate 		    saddr_ipif));
8417c478bd9Sstevel@tonic-gate 	}
842*bd670b35SErik Nordmark 	connp->conn_ports = 0;
8437c478bd9Sstevel@tonic-gate 	sctp->sctp_running = B_FALSE;
8447c478bd9Sstevel@tonic-gate 	sctp->sctp_state = SCTPS_IDLE;
8457c478bd9Sstevel@tonic-gate 
8467c478bd9Sstevel@tonic-gate 	sctp->sctp_refcnt = 1;
8477c478bd9Sstevel@tonic-gate 
8487c478bd9Sstevel@tonic-gate 	sctp->sctp_strikes = 0;
8497c478bd9Sstevel@tonic-gate 
8507c478bd9Sstevel@tonic-gate 	sctp->sctp_last_mtu_probe = lbolt64;
851f4b3ec61Sdh 	sctp->sctp_mtu_probe_intvl = sctps->sctps_mtu_probe_interval;
8527c478bd9Sstevel@tonic-gate 
8537c478bd9Sstevel@tonic-gate 	sctp->sctp_sack_gaps = 0;
8547c478bd9Sstevel@tonic-gate 	sctp->sctp_sack_toggle = 2;
8557c478bd9Sstevel@tonic-gate 
856121e5416Skcpoon 	/* Only need to do the allocation if there is no "cached" one. */
857121e5416Skcpoon 	if (sctp->sctp_pad_mp == NULL) {
858121e5416Skcpoon 		if (sleep == KM_SLEEP) {
859121e5416Skcpoon 			sctp->sctp_pad_mp = allocb_wait(SCTP_ALIGN, BPRI_MED,
860121e5416Skcpoon 			    STR_NOSIG, NULL);
861121e5416Skcpoon 		} else {
862121e5416Skcpoon 			sctp->sctp_pad_mp = allocb(SCTP_ALIGN, BPRI_MED);
863121e5416Skcpoon 			if (sctp->sctp_pad_mp == NULL)
864121e5416Skcpoon 				return (ENOMEM);
865121e5416Skcpoon 		}
866121e5416Skcpoon 		bzero(sctp->sctp_pad_mp->b_rptr, SCTP_ALIGN);
867121e5416Skcpoon 	}
868121e5416Skcpoon 
8697c478bd9Sstevel@tonic-gate 	if (psctp != NULL) {
8707c478bd9Sstevel@tonic-gate 		/*
8717c478bd9Sstevel@tonic-gate 		 * Inherit from parent
872*bd670b35SErik Nordmark 		 *
873*bd670b35SErik Nordmark 		 * Start by inheriting from the conn_t, including conn_ixa and
874*bd670b35SErik Nordmark 		 * conn_xmit_ipp.
8757c478bd9Sstevel@tonic-gate 		 */
876*bd670b35SErik Nordmark 		err = conn_inherit_parent(psctp->sctp_connp, connp);
877*bd670b35SErik Nordmark 		if (err != 0)
878121e5416Skcpoon 			goto failure;
8797c478bd9Sstevel@tonic-gate 
8807c478bd9Sstevel@tonic-gate 		sctp->sctp_cookie_lifetime = psctp->sctp_cookie_lifetime;
881*bd670b35SErik Nordmark 
8827c478bd9Sstevel@tonic-gate 		sctp->sctp_cwnd_max = psctp->sctp_cwnd_max;
8837c478bd9Sstevel@tonic-gate 		sctp->sctp_rwnd = psctp->sctp_rwnd;
8841d8c4025Svi 		sctp->sctp_irwnd = psctp->sctp_rwnd;
8857d546a59Svi 		sctp->sctp_pd_point = psctp->sctp_pd_point;
8867c478bd9Sstevel@tonic-gate 		sctp->sctp_rto_max = psctp->sctp_rto_max;
8877c478bd9Sstevel@tonic-gate 		sctp->sctp_init_rto_max = psctp->sctp_init_rto_max;
8887c478bd9Sstevel@tonic-gate 		sctp->sctp_rto_min = psctp->sctp_rto_min;
8897c478bd9Sstevel@tonic-gate 		sctp->sctp_rto_initial = psctp->sctp_rto_initial;
8907c478bd9Sstevel@tonic-gate 		sctp->sctp_pa_max_rxt = psctp->sctp_pa_max_rxt;
8917c478bd9Sstevel@tonic-gate 		sctp->sctp_pp_max_rxt = psctp->sctp_pp_max_rxt;
8927c478bd9Sstevel@tonic-gate 		sctp->sctp_max_init_rxt = psctp->sctp_max_init_rxt;
8937c478bd9Sstevel@tonic-gate 
8947c478bd9Sstevel@tonic-gate 		sctp->sctp_def_stream = psctp->sctp_def_stream;
8957c478bd9Sstevel@tonic-gate 		sctp->sctp_def_flags = psctp->sctp_def_flags;
8967c478bd9Sstevel@tonic-gate 		sctp->sctp_def_ppid = psctp->sctp_def_ppid;
8977c478bd9Sstevel@tonic-gate 		sctp->sctp_def_context = psctp->sctp_def_context;
8987c478bd9Sstevel@tonic-gate 		sctp->sctp_def_timetolive = psctp->sctp_def_timetolive;
8997c478bd9Sstevel@tonic-gate 
9007c478bd9Sstevel@tonic-gate 		sctp->sctp_num_istr = psctp->sctp_num_istr;
9017c478bd9Sstevel@tonic-gate 		sctp->sctp_num_ostr = psctp->sctp_num_ostr;
9027c478bd9Sstevel@tonic-gate 
9037c478bd9Sstevel@tonic-gate 		sctp->sctp_hb_interval = psctp->sctp_hb_interval;
9047c478bd9Sstevel@tonic-gate 		sctp->sctp_autoclose = psctp->sctp_autoclose;
905558fbd03Skcpoon 		sctp->sctp_tx_adaptation_code = psctp->sctp_tx_adaptation_code;
9067c478bd9Sstevel@tonic-gate 
9077c478bd9Sstevel@tonic-gate 		/* xxx should be a better way to copy these flags xxx */
9087c478bd9Sstevel@tonic-gate 		sctp->sctp_bound_to_all = psctp->sctp_bound_to_all;
9097c478bd9Sstevel@tonic-gate 		sctp->sctp_cansleep = psctp->sctp_cansleep;
910558fbd03Skcpoon 		sctp->sctp_send_adaptation = psctp->sctp_send_adaptation;
9117c478bd9Sstevel@tonic-gate 		sctp->sctp_ndelay = psctp->sctp_ndelay;
9127c478bd9Sstevel@tonic-gate 		sctp->sctp_events = psctp->sctp_events;
9137c478bd9Sstevel@tonic-gate 	} else {
9147c478bd9Sstevel@tonic-gate 		/*
9157c478bd9Sstevel@tonic-gate 		 * Set to system defaults
9167c478bd9Sstevel@tonic-gate 		 */
917f4b3ec61Sdh 		sctp->sctp_cookie_lifetime =
918f4b3ec61Sdh 		    MSEC_TO_TICK(sctps->sctps_cookie_life);
919*bd670b35SErik Nordmark 		connp->conn_sndlowat = sctps->sctps_xmit_lowat;
920*bd670b35SErik Nordmark 		connp->conn_sndbuf = sctps->sctps_xmit_hiwat;
921*bd670b35SErik Nordmark 		connp->conn_rcvbuf = sctps->sctps_recv_hiwat;
922*bd670b35SErik Nordmark 
923f4b3ec61Sdh 		sctp->sctp_cwnd_max = sctps->sctps_cwnd_max_;
924*bd670b35SErik Nordmark 		sctp->sctp_rwnd = connp->conn_rcvbuf;
9251d8c4025Svi 		sctp->sctp_irwnd = sctp->sctp_rwnd;
9267d546a59Svi 		sctp->sctp_pd_point = sctp->sctp_rwnd;
927f4b3ec61Sdh 		sctp->sctp_rto_max = MSEC_TO_TICK(sctps->sctps_rto_maxg);
9287c478bd9Sstevel@tonic-gate 		sctp->sctp_init_rto_max = sctp->sctp_rto_max;
929f4b3ec61Sdh 		sctp->sctp_rto_min = MSEC_TO_TICK(sctps->sctps_rto_ming);
930f4b3ec61Sdh 		sctp->sctp_rto_initial = MSEC_TO_TICK(
931f4b3ec61Sdh 		    sctps->sctps_rto_initialg);
932f4b3ec61Sdh 		sctp->sctp_pa_max_rxt = sctps->sctps_pa_max_retr;
933f4b3ec61Sdh 		sctp->sctp_pp_max_rxt = sctps->sctps_pp_max_retr;
934f4b3ec61Sdh 		sctp->sctp_max_init_rxt = sctps->sctps_max_init_retr;
935f4b3ec61Sdh 
936f4b3ec61Sdh 		sctp->sctp_num_istr = sctps->sctps_max_in_streams;
937f4b3ec61Sdh 		sctp->sctp_num_ostr = sctps->sctps_initial_out_streams;
938f4b3ec61Sdh 
939f4b3ec61Sdh 		sctp->sctp_hb_interval =
940f4b3ec61Sdh 		    MSEC_TO_TICK(sctps->sctps_heartbeat_interval);
941*bd670b35SErik Nordmark 
942*bd670b35SErik Nordmark 		if (connp->conn_family == AF_INET)
943*bd670b35SErik Nordmark 			connp->conn_default_ttl = sctps->sctps_ipv4_ttl;
944*bd670b35SErik Nordmark 		else
945*bd670b35SErik Nordmark 			connp->conn_default_ttl = sctps->sctps_ipv6_hoplimit;
946*bd670b35SErik Nordmark 
947*bd670b35SErik Nordmark 		connp->conn_xmit_ipp.ipp_unicast_hops =
948*bd670b35SErik Nordmark 		    connp->conn_default_ttl;
949*bd670b35SErik Nordmark 
950*bd670b35SErik Nordmark 		/*
951*bd670b35SErik Nordmark 		 * Initialize the header template
952*bd670b35SErik Nordmark 		 */
953*bd670b35SErik Nordmark 		if ((err = sctp_build_hdrs(sctp, sleep)) != 0) {
954*bd670b35SErik Nordmark 			goto failure;
955*bd670b35SErik Nordmark 		}
9567c478bd9Sstevel@tonic-gate 	}
957*bd670b35SErik Nordmark 
9587c478bd9Sstevel@tonic-gate 	sctp->sctp_understands_asconf = B_TRUE;
9597c478bd9Sstevel@tonic-gate 	sctp->sctp_understands_addip = B_TRUE;
9607c478bd9Sstevel@tonic-gate 	sctp->sctp_prsctp_aware = B_FALSE;
9617c478bd9Sstevel@tonic-gate 
9627c478bd9Sstevel@tonic-gate 	sctp->sctp_connp->conn_ref = 1;
9637c478bd9Sstevel@tonic-gate 
9647c478bd9Sstevel@tonic-gate 	sctp->sctp_prsctpdrop = 0;
9657c478bd9Sstevel@tonic-gate 	sctp->sctp_msgcount = 0;
9667c478bd9Sstevel@tonic-gate 
9677c478bd9Sstevel@tonic-gate 	return (0);
968121e5416Skcpoon 
969121e5416Skcpoon failure:
970*bd670b35SErik Nordmark 	sctp_headers_free(sctp);
971121e5416Skcpoon 	return (err);
9727c478bd9Sstevel@tonic-gate }
9737c478bd9Sstevel@tonic-gate 
9747c478bd9Sstevel@tonic-gate /*
9757c478bd9Sstevel@tonic-gate  * Extracts the init tag from an INIT chunk and checks if it matches
9767c478bd9Sstevel@tonic-gate  * the sctp's verification tag. Returns 0 if it doesn't match, 1 if
9777c478bd9Sstevel@tonic-gate  * it does.
9787c478bd9Sstevel@tonic-gate  */
9797c478bd9Sstevel@tonic-gate static boolean_t
9807c478bd9Sstevel@tonic-gate sctp_icmp_verf(sctp_t *sctp, sctp_hdr_t *sh, mblk_t *mp)
9817c478bd9Sstevel@tonic-gate {
9827c478bd9Sstevel@tonic-gate 	sctp_chunk_hdr_t *sch;
9837c478bd9Sstevel@tonic-gate 	uint32_t verf, *vp;
9847c478bd9Sstevel@tonic-gate 
9857c478bd9Sstevel@tonic-gate 	sch = (sctp_chunk_hdr_t *)(sh + 1);
9867c478bd9Sstevel@tonic-gate 	vp = (uint32_t *)(sch + 1);
9877c478bd9Sstevel@tonic-gate 
9887c478bd9Sstevel@tonic-gate 	/* Need at least the data chunk hdr and the first 4 bytes of INIT */
9897c478bd9Sstevel@tonic-gate 	if ((unsigned char *)(vp + 1) > mp->b_wptr) {
9907c478bd9Sstevel@tonic-gate 		return (B_FALSE);
9917c478bd9Sstevel@tonic-gate 	}
9927c478bd9Sstevel@tonic-gate 
9937c478bd9Sstevel@tonic-gate 	bcopy(vp, &verf, sizeof (verf));
9947c478bd9Sstevel@tonic-gate 
9957c478bd9Sstevel@tonic-gate 	if (verf == sctp->sctp_lvtag) {
9967c478bd9Sstevel@tonic-gate 		return (B_TRUE);
9977c478bd9Sstevel@tonic-gate 	}
9987c478bd9Sstevel@tonic-gate 	return (B_FALSE);
9997c478bd9Sstevel@tonic-gate }
10007c478bd9Sstevel@tonic-gate 
1001*bd670b35SErik Nordmark /*
1002*bd670b35SErik Nordmark  * Update the SCTP state according to change of PMTU.
1003*bd670b35SErik Nordmark  *
1004*bd670b35SErik Nordmark  * Path MTU might have changed by either increase or decrease, so need to
1005*bd670b35SErik Nordmark  * adjust the MSS based on the value of ixa_pmtu.
1006*bd670b35SErik Nordmark  */
1007*bd670b35SErik Nordmark static void
1008*bd670b35SErik Nordmark sctp_update_pmtu(sctp_t *sctp, sctp_faddr_t *fp, boolean_t decrease_only)
1009*bd670b35SErik Nordmark {
1010*bd670b35SErik Nordmark 	uint32_t	pmtu;
1011*bd670b35SErik Nordmark 	int32_t		mss;
1012*bd670b35SErik Nordmark 	ip_xmit_attr_t	*ixa = fp->ixa;
1013*bd670b35SErik Nordmark 
1014*bd670b35SErik Nordmark 	if (sctp->sctp_state < SCTPS_ESTABLISHED)
1015*bd670b35SErik Nordmark 		return;
1016*bd670b35SErik Nordmark 
1017*bd670b35SErik Nordmark 	/*
1018*bd670b35SErik Nordmark 	 * Always call ip_get_pmtu() to make sure that IP has updated
1019*bd670b35SErik Nordmark 	 * ixa_flags properly.
1020*bd670b35SErik Nordmark 	 */
1021*bd670b35SErik Nordmark 	pmtu = ip_get_pmtu(ixa);
1022*bd670b35SErik Nordmark 
1023*bd670b35SErik Nordmark 	/*
1024*bd670b35SErik Nordmark 	 * Calculate the MSS by decreasing the PMTU by sctp_hdr_len and
1025*bd670b35SErik Nordmark 	 * IPsec overhead if applied. Make sure to use the most recent
1026*bd670b35SErik Nordmark 	 * IPsec information.
1027*bd670b35SErik Nordmark 	 */
1028*bd670b35SErik Nordmark 	mss = pmtu - conn_ipsec_length(sctp->sctp_connp);
1029*bd670b35SErik Nordmark 	if (ixa->ixa_flags & IXAF_IS_IPV4)
1030*bd670b35SErik Nordmark 		mss -= sctp->sctp_hdr_len;
1031*bd670b35SErik Nordmark 	else
1032*bd670b35SErik Nordmark 		mss -= sctp->sctp_hdr6_len;
1033*bd670b35SErik Nordmark 
1034*bd670b35SErik Nordmark 	/*
1035*bd670b35SErik Nordmark 	 * Nothing to change, so just return.
1036*bd670b35SErik Nordmark 	 */
1037*bd670b35SErik Nordmark 	if (mss == fp->sfa_pmss)
1038*bd670b35SErik Nordmark 		return;
1039*bd670b35SErik Nordmark 
1040*bd670b35SErik Nordmark 	/*
1041*bd670b35SErik Nordmark 	 * Currently, for ICMP errors, only PMTU decrease is handled.
1042*bd670b35SErik Nordmark 	 */
1043*bd670b35SErik Nordmark 	if (mss > fp->sfa_pmss && decrease_only)
1044*bd670b35SErik Nordmark 		return;
1045*bd670b35SErik Nordmark 
1046*bd670b35SErik Nordmark #ifdef DEBUG
1047*bd670b35SErik Nordmark 	(void) printf("sctp_update_pmtu mss from %d to %d\n",
1048*bd670b35SErik Nordmark 	    fp->sfa_pmss, mss);
1049*bd670b35SErik Nordmark #endif
1050*bd670b35SErik Nordmark 	DTRACE_PROBE2(sctp_update_pmtu, int32_t, fp->sfa_pmss, uint32_t, mss);
1051*bd670b35SErik Nordmark 
1052*bd670b35SErik Nordmark 	/*
1053*bd670b35SErik Nordmark 	 * Update ixa_fragsize and ixa_pmtu.
1054*bd670b35SErik Nordmark 	 */
1055*bd670b35SErik Nordmark 	ixa->ixa_fragsize = ixa->ixa_pmtu = pmtu;
1056*bd670b35SErik Nordmark 
1057*bd670b35SErik Nordmark 	/*
1058*bd670b35SErik Nordmark 	 * Make sure that sfa_pmss is a multiple of
1059*bd670b35SErik Nordmark 	 * SCTP_ALIGN.
1060*bd670b35SErik Nordmark 	 */
1061*bd670b35SErik Nordmark 	fp->sfa_pmss = mss & ~(SCTP_ALIGN - 1);
1062*bd670b35SErik Nordmark 	fp->pmtu_discovered = 1;
1063*bd670b35SErik Nordmark 
1064*bd670b35SErik Nordmark #ifdef notyet
1065*bd670b35SErik Nordmark 	if (mss < sctp->sctp_sctps->sctps_mss_min)
1066*bd670b35SErik Nordmark 		ixa->ixa_flags |= IXAF_PMTU_TOO_SMALL;
1067*bd670b35SErik Nordmark #endif
1068*bd670b35SErik Nordmark 	if (ixa->ixa_flags & IXAF_PMTU_TOO_SMALL)
1069*bd670b35SErik Nordmark 		ixa->ixa_flags &= ~(IXAF_DONTFRAG | IXAF_PMTU_IPV4_DF);
1070*bd670b35SErik Nordmark 
1071*bd670b35SErik Nordmark 	/*
1072*bd670b35SErik Nordmark 	 * If below the min size then ip_get_pmtu cleared IXAF_PMTU_IPV4_DF.
1073*bd670b35SErik Nordmark 	 * Make sure to clear IXAF_DONTFRAG, which is used by IP to decide
1074*bd670b35SErik Nordmark 	 * whether to fragment the packet.
1075*bd670b35SErik Nordmark 	 */
1076*bd670b35SErik Nordmark 	if (ixa->ixa_flags & IXAF_IS_IPV4) {
1077*bd670b35SErik Nordmark 		if (!(ixa->ixa_flags & IXAF_PMTU_IPV4_DF)) {
1078*bd670b35SErik Nordmark 			fp->df = B_FALSE;
1079*bd670b35SErik Nordmark 			if (fp == sctp->sctp_current) {
1080*bd670b35SErik Nordmark 				sctp->sctp_ipha->
1081*bd670b35SErik Nordmark 				    ipha_fragment_offset_and_flags = 0;
1082*bd670b35SErik Nordmark 			}
1083*bd670b35SErik Nordmark 		}
1084*bd670b35SErik Nordmark 	}
1085*bd670b35SErik Nordmark }
1086*bd670b35SErik Nordmark 
1087*bd670b35SErik Nordmark /*
1088*bd670b35SErik Nordmark  * Notify function registered with ip_xmit_attr_t. It's called in the context
1089*bd670b35SErik Nordmark  * of conn_ip_output so it's safe to update the SCTP state.
1090*bd670b35SErik Nordmark  * Currently only used for pmtu changes.
1091*bd670b35SErik Nordmark  */
1092*bd670b35SErik Nordmark /* ARGSUSED1 */
1093*bd670b35SErik Nordmark static void
1094*bd670b35SErik Nordmark sctp_notify(void *arg, ip_xmit_attr_t *ixa, ixa_notify_type_t ntype,
1095*bd670b35SErik Nordmark     ixa_notify_arg_t narg)
1096*bd670b35SErik Nordmark {
1097*bd670b35SErik Nordmark 	sctp_t		*sctp = (sctp_t *)arg;
1098*bd670b35SErik Nordmark 	sctp_faddr_t	*fp;
1099*bd670b35SErik Nordmark 
1100*bd670b35SErik Nordmark 	switch (ntype) {
1101*bd670b35SErik Nordmark 	case IXAN_PMTU:
1102*bd670b35SErik Nordmark 		/* Find the faddr based on the ip_xmit_attr_t pointer */
1103*bd670b35SErik Nordmark 		for (fp = sctp->sctp_faddrs; fp != NULL; fp = fp->next) {
1104*bd670b35SErik Nordmark 			if (fp->ixa == ixa)
1105*bd670b35SErik Nordmark 				break;
1106*bd670b35SErik Nordmark 		}
1107*bd670b35SErik Nordmark 		if (fp != NULL)
1108*bd670b35SErik Nordmark 			sctp_update_pmtu(sctp, fp, B_FALSE);
1109*bd670b35SErik Nordmark 		break;
1110*bd670b35SErik Nordmark 	default:
1111*bd670b35SErik Nordmark 		break;
1112*bd670b35SErik Nordmark 	}
1113*bd670b35SErik Nordmark }
1114*bd670b35SErik Nordmark 
11157c478bd9Sstevel@tonic-gate /*
11167c478bd9Sstevel@tonic-gate  * sctp_icmp_error is called by sctp_input() to process ICMP error messages
1117*bd670b35SErik Nordmark  * passed up by IP.  We need to find a sctp_t
11187c478bd9Sstevel@tonic-gate  * that corresponds to the returned datagram.  Passes the message back in on
11197c478bd9Sstevel@tonic-gate  * the correct queue once it has located the connection.
11207c478bd9Sstevel@tonic-gate  * Assumes that IP has pulled up everything up to and including
11217c478bd9Sstevel@tonic-gate  * the ICMP header.
11227c478bd9Sstevel@tonic-gate  */
11237c478bd9Sstevel@tonic-gate void
11247c478bd9Sstevel@tonic-gate sctp_icmp_error(sctp_t *sctp, mblk_t *mp)
11257c478bd9Sstevel@tonic-gate {
11267c478bd9Sstevel@tonic-gate 	icmph_t *icmph;
11277c478bd9Sstevel@tonic-gate 	ipha_t	*ipha;
11287c478bd9Sstevel@tonic-gate 	int	iph_hdr_length;
11297c478bd9Sstevel@tonic-gate 	sctp_hdr_t *sctph;
11307c478bd9Sstevel@tonic-gate 	in6_addr_t dst;
11317c478bd9Sstevel@tonic-gate 	sctp_faddr_t *fp;
1132f4b3ec61Sdh 	sctp_stack_t	*sctps = sctp->sctp_sctps;
11337c478bd9Sstevel@tonic-gate 
113445916cd2Sjpk 	dprint(1, ("sctp_icmp_error: sctp=%p, mp=%p\n", (void *)sctp,
113545916cd2Sjpk 	    (void *)mp));
11367c478bd9Sstevel@tonic-gate 
11377c478bd9Sstevel@tonic-gate 	ipha = (ipha_t *)mp->b_rptr;
11387c478bd9Sstevel@tonic-gate 	if (IPH_HDR_VERSION(ipha) != IPV4_VERSION) {
11397c478bd9Sstevel@tonic-gate 		ASSERT(IPH_HDR_VERSION(ipha) == IPV6_VERSION);
1140*bd670b35SErik Nordmark 		sctp_icmp_error_ipv6(sctp, mp);
11417c478bd9Sstevel@tonic-gate 		return;
11427c478bd9Sstevel@tonic-gate 	}
11437c478bd9Sstevel@tonic-gate 
1144f0c3911fSGeorge Shepherd 	/* account for the ip hdr from the icmp message */
11457c478bd9Sstevel@tonic-gate 	iph_hdr_length = IPH_HDR_LENGTH(ipha);
11467c478bd9Sstevel@tonic-gate 	icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length];
1147f0c3911fSGeorge Shepherd 	/* now the ip hdr of message resulting in this icmp */
11487c478bd9Sstevel@tonic-gate 	ipha = (ipha_t *)&icmph[1];
11497c478bd9Sstevel@tonic-gate 	iph_hdr_length = IPH_HDR_LENGTH(ipha);
11507c478bd9Sstevel@tonic-gate 	sctph = (sctp_hdr_t *)((char *)ipha + iph_hdr_length);
1151f0c3911fSGeorge Shepherd 	/* first_mp must expose the full sctp header. */
11527c478bd9Sstevel@tonic-gate 	if ((uchar_t *)(sctph + 1) >= mp->b_wptr) {
11537c478bd9Sstevel@tonic-gate 		/* not enough data for SCTP header */
1154*bd670b35SErik Nordmark 		freemsg(mp);
11557c478bd9Sstevel@tonic-gate 		return;
11567c478bd9Sstevel@tonic-gate 	}
11577c478bd9Sstevel@tonic-gate 
11587c478bd9Sstevel@tonic-gate 	switch (icmph->icmph_type) {
11597c478bd9Sstevel@tonic-gate 	case ICMP_DEST_UNREACHABLE:
11607c478bd9Sstevel@tonic-gate 		switch (icmph->icmph_code) {
11617c478bd9Sstevel@tonic-gate 		case ICMP_FRAGMENTATION_NEEDED:
11627c478bd9Sstevel@tonic-gate 			/*
11637c478bd9Sstevel@tonic-gate 			 * Reduce the MSS based on the new MTU.  This will
11647c478bd9Sstevel@tonic-gate 			 * eliminate any fragmentation locally.
11657c478bd9Sstevel@tonic-gate 			 * N.B.  There may well be some funny side-effects on
11667c478bd9Sstevel@tonic-gate 			 * the local send policy and the remote receive policy.
11677c478bd9Sstevel@tonic-gate 			 * Pending further research, we provide
11687c478bd9Sstevel@tonic-gate 			 * sctp_ignore_path_mtu just in case this proves
11697c478bd9Sstevel@tonic-gate 			 * disastrous somewhere.
11707c478bd9Sstevel@tonic-gate 			 *
11717c478bd9Sstevel@tonic-gate 			 * After updating the MSS, retransmit part of the
11727c478bd9Sstevel@tonic-gate 			 * dropped segment using the new mss by calling
11737c478bd9Sstevel@tonic-gate 			 * sctp_wput_slow().  Need to adjust all those
11747c478bd9Sstevel@tonic-gate 			 * params to make sure sctp_wput_slow() work properly.
11757c478bd9Sstevel@tonic-gate 			 */
1176f4b3ec61Sdh 			if (sctps->sctps_ignore_path_mtu)
11777c478bd9Sstevel@tonic-gate 				break;
11787c478bd9Sstevel@tonic-gate 
11797c478bd9Sstevel@tonic-gate 			/* find the offending faddr */
11807c478bd9Sstevel@tonic-gate 			IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, &dst);
11817c478bd9Sstevel@tonic-gate 			fp = sctp_lookup_faddr(sctp, &dst);
11827c478bd9Sstevel@tonic-gate 			if (fp == NULL) {
11837c478bd9Sstevel@tonic-gate 				break;
11847c478bd9Sstevel@tonic-gate 			}
1185*bd670b35SErik Nordmark 			sctp_update_pmtu(sctp, fp, B_TRUE);
1186f0c3911fSGeorge Shepherd 			/*
1187f0c3911fSGeorge Shepherd 			 * It is possible, even likely that a fast retransmit
1188f0c3911fSGeorge Shepherd 			 * attempt has been dropped by ip as a result of this
1189f0c3911fSGeorge Shepherd 			 * error, retransmission bundles as much as possible.
1190f0c3911fSGeorge Shepherd 			 * A retransmit here prevents significant delays waiting
1191f0c3911fSGeorge Shepherd 			 * on the timer. Analogous to behaviour of TCP after
1192f0c3911fSGeorge Shepherd 			 * ICMP too big.
1193f0c3911fSGeorge Shepherd 			 */
1194f0c3911fSGeorge Shepherd 			sctp_rexmit(sctp, fp);
11957c478bd9Sstevel@tonic-gate 			break;
11967c478bd9Sstevel@tonic-gate 		case ICMP_PORT_UNREACHABLE:
11977c478bd9Sstevel@tonic-gate 		case ICMP_PROTOCOL_UNREACHABLE:
11987c478bd9Sstevel@tonic-gate 			switch (sctp->sctp_state) {
11997c478bd9Sstevel@tonic-gate 			case SCTPS_COOKIE_WAIT:
12007c478bd9Sstevel@tonic-gate 			case SCTPS_COOKIE_ECHOED:
12017c478bd9Sstevel@tonic-gate 				/* make sure the verification tag matches */
12027c478bd9Sstevel@tonic-gate 				if (!sctp_icmp_verf(sctp, sctph, mp)) {
12037c478bd9Sstevel@tonic-gate 					break;
12047c478bd9Sstevel@tonic-gate 				}
1205f4b3ec61Sdh 				BUMP_MIB(&sctps->sctps_mib, sctpAborted);
1206c9da23f8Skcpoon 				sctp_assoc_event(sctp, SCTP_CANT_STR_ASSOC, 0,
1207c9da23f8Skcpoon 				    NULL);
12087c478bd9Sstevel@tonic-gate 				sctp_clean_death(sctp, ECONNREFUSED);
12097c478bd9Sstevel@tonic-gate 				break;
12107c478bd9Sstevel@tonic-gate 			}
12117c478bd9Sstevel@tonic-gate 			break;
12127c478bd9Sstevel@tonic-gate 		case ICMP_HOST_UNREACHABLE:
12137c478bd9Sstevel@tonic-gate 		case ICMP_NET_UNREACHABLE:
12147c478bd9Sstevel@tonic-gate 			/* Record the error in case we finally time out. */
12157c478bd9Sstevel@tonic-gate 			sctp->sctp_client_errno = (icmph->icmph_code ==
12167c478bd9Sstevel@tonic-gate 			    ICMP_HOST_UNREACHABLE) ? EHOSTUNREACH : ENETUNREACH;
12177c478bd9Sstevel@tonic-gate 			break;
12187c478bd9Sstevel@tonic-gate 		default:
12197c478bd9Sstevel@tonic-gate 			break;
12207c478bd9Sstevel@tonic-gate 		}
12217c478bd9Sstevel@tonic-gate 		break;
12227c478bd9Sstevel@tonic-gate 	case ICMP_SOURCE_QUENCH: {
12237c478bd9Sstevel@tonic-gate 		/* Reduce the sending rate as if we got a retransmit timeout */
12247c478bd9Sstevel@tonic-gate 		break;
12257c478bd9Sstevel@tonic-gate 	}
12267c478bd9Sstevel@tonic-gate 	}
1227*bd670b35SErik Nordmark 	freemsg(mp);
12287c478bd9Sstevel@tonic-gate }
12297c478bd9Sstevel@tonic-gate 
12307c478bd9Sstevel@tonic-gate /*
12317c478bd9Sstevel@tonic-gate  * sctp_icmp_error_ipv6() is called by sctp_icmp_error() to process ICMPv6
12327c478bd9Sstevel@tonic-gate  * error messages passed up by IP.
12337c478bd9Sstevel@tonic-gate  * Assumes that IP has pulled up all the extension headers as well
12347c478bd9Sstevel@tonic-gate  * as the ICMPv6 header.
12357c478bd9Sstevel@tonic-gate  */
12367c478bd9Sstevel@tonic-gate static void
12377c478bd9Sstevel@tonic-gate sctp_icmp_error_ipv6(sctp_t *sctp, mblk_t *mp)
12387c478bd9Sstevel@tonic-gate {
12397c478bd9Sstevel@tonic-gate 	icmp6_t *icmp6;
12407c478bd9Sstevel@tonic-gate 	ip6_t	*ip6h;
12417c478bd9Sstevel@tonic-gate 	uint16_t	iph_hdr_length;
12427c478bd9Sstevel@tonic-gate 	sctp_hdr_t *sctpha;
12437c478bd9Sstevel@tonic-gate 	uint8_t	*nexthdrp;
12447c478bd9Sstevel@tonic-gate 	sctp_faddr_t *fp;
1245f4b3ec61Sdh 	sctp_stack_t	*sctps = sctp->sctp_sctps;
12467c478bd9Sstevel@tonic-gate 
12477c478bd9Sstevel@tonic-gate 	ip6h = (ip6_t *)mp->b_rptr;
12487c478bd9Sstevel@tonic-gate 	iph_hdr_length = (ip6h->ip6_nxt != IPPROTO_SCTP) ?
12497c478bd9Sstevel@tonic-gate 	    ip_hdr_length_v6(mp, ip6h) : IPV6_HDR_LEN;
12507c478bd9Sstevel@tonic-gate 
12517c478bd9Sstevel@tonic-gate 	icmp6 = (icmp6_t *)&mp->b_rptr[iph_hdr_length];
12527c478bd9Sstevel@tonic-gate 	ip6h = (ip6_t *)&icmp6[1];
12537c478bd9Sstevel@tonic-gate 	if (!ip_hdr_length_nexthdr_v6(mp, ip6h, &iph_hdr_length, &nexthdrp)) {
12547c478bd9Sstevel@tonic-gate 		freemsg(mp);
12557c478bd9Sstevel@tonic-gate 		return;
12567c478bd9Sstevel@tonic-gate 	}
12577c478bd9Sstevel@tonic-gate 	ASSERT(*nexthdrp == IPPROTO_SCTP);
12587c478bd9Sstevel@tonic-gate 
12597c478bd9Sstevel@tonic-gate 	/* XXX need ifindex to find connection */
12607c478bd9Sstevel@tonic-gate 	sctpha = (sctp_hdr_t *)((char *)ip6h + iph_hdr_length);
12617c478bd9Sstevel@tonic-gate 	if ((uchar_t *)sctpha >= mp->b_wptr) {
12627c478bd9Sstevel@tonic-gate 		/* not enough data for SCTP header */
12637c478bd9Sstevel@tonic-gate 		freemsg(mp);
12647c478bd9Sstevel@tonic-gate 		return;
12657c478bd9Sstevel@tonic-gate 	}
12667c478bd9Sstevel@tonic-gate 	switch (icmp6->icmp6_type) {
12677c478bd9Sstevel@tonic-gate 	case ICMP6_PACKET_TOO_BIG:
12687c478bd9Sstevel@tonic-gate 		/*
12697c478bd9Sstevel@tonic-gate 		 * Reduce the MSS based on the new MTU.  This will
12707c478bd9Sstevel@tonic-gate 		 * eliminate any fragmentation locally.
12717c478bd9Sstevel@tonic-gate 		 * N.B.  There may well be some funny side-effects on
12727c478bd9Sstevel@tonic-gate 		 * the local send policy and the remote receive policy.
12737c478bd9Sstevel@tonic-gate 		 * Pending further research, we provide
12747c478bd9Sstevel@tonic-gate 		 * sctp_ignore_path_mtu just in case this proves
12757c478bd9Sstevel@tonic-gate 		 * disastrous somewhere.
12767c478bd9Sstevel@tonic-gate 		 *
12777c478bd9Sstevel@tonic-gate 		 * After updating the MSS, retransmit part of the
12787c478bd9Sstevel@tonic-gate 		 * dropped segment using the new mss by calling
12797c478bd9Sstevel@tonic-gate 		 * sctp_wput_slow().  Need to adjust all those
12807c478bd9Sstevel@tonic-gate 		 * params to make sure sctp_wput_slow() work properly.
12817c478bd9Sstevel@tonic-gate 		 */
1282f4b3ec61Sdh 		if (sctps->sctps_ignore_path_mtu)
12837c478bd9Sstevel@tonic-gate 			break;
12847c478bd9Sstevel@tonic-gate 
12857c478bd9Sstevel@tonic-gate 		/* find the offending faddr */
12867c478bd9Sstevel@tonic-gate 		fp = sctp_lookup_faddr(sctp, &ip6h->ip6_dst);
12877c478bd9Sstevel@tonic-gate 		if (fp == NULL) {
12887c478bd9Sstevel@tonic-gate 			break;
12897c478bd9Sstevel@tonic-gate 		}
12907c478bd9Sstevel@tonic-gate 
1291*bd670b35SErik Nordmark 		sctp_update_pmtu(sctp, fp, B_TRUE);
1292*bd670b35SErik Nordmark 		/*
1293*bd670b35SErik Nordmark 		 * It is possible, even likely that a fast retransmit
1294*bd670b35SErik Nordmark 		 * attempt has been dropped by ip as a result of this
1295*bd670b35SErik Nordmark 		 * error, retransmission bundles as much as possible.
1296*bd670b35SErik Nordmark 		 * A retransmit here prevents significant delays waiting
1297*bd670b35SErik Nordmark 		 * on the timer. Analogous to behaviour of TCP after
1298*bd670b35SErik Nordmark 		 * ICMP too big.
1299*bd670b35SErik Nordmark 		 */
1300*bd670b35SErik Nordmark 		sctp_rexmit(sctp, fp);
13017c478bd9Sstevel@tonic-gate 		break;
13027c478bd9Sstevel@tonic-gate 
13037c478bd9Sstevel@tonic-gate 	case ICMP6_DST_UNREACH:
13047c478bd9Sstevel@tonic-gate 		switch (icmp6->icmp6_code) {
13057c478bd9Sstevel@tonic-gate 		case ICMP6_DST_UNREACH_NOPORT:
13067c478bd9Sstevel@tonic-gate 			/* make sure the verification tag matches */
13077c478bd9Sstevel@tonic-gate 			if (!sctp_icmp_verf(sctp, sctpha, mp)) {
13087c478bd9Sstevel@tonic-gate 				break;
13097c478bd9Sstevel@tonic-gate 			}
13107c478bd9Sstevel@tonic-gate 			if (sctp->sctp_state == SCTPS_COOKIE_WAIT ||
13117c478bd9Sstevel@tonic-gate 			    sctp->sctp_state == SCTPS_COOKIE_ECHOED) {
1312f4b3ec61Sdh 				BUMP_MIB(&sctps->sctps_mib, sctpAborted);
1313c9da23f8Skcpoon 				sctp_assoc_event(sctp, SCTP_CANT_STR_ASSOC, 0,
1314c9da23f8Skcpoon 				    NULL);
13157c478bd9Sstevel@tonic-gate 				sctp_clean_death(sctp, ECONNREFUSED);
13167c478bd9Sstevel@tonic-gate 			}
13177c478bd9Sstevel@tonic-gate 			break;
13187c478bd9Sstevel@tonic-gate 
13197c478bd9Sstevel@tonic-gate 		case ICMP6_DST_UNREACH_ADMIN:
13207c478bd9Sstevel@tonic-gate 		case ICMP6_DST_UNREACH_NOROUTE:
13217c478bd9Sstevel@tonic-gate 		case ICMP6_DST_UNREACH_NOTNEIGHBOR:
13227c478bd9Sstevel@tonic-gate 		case ICMP6_DST_UNREACH_ADDR:
13237c478bd9Sstevel@tonic-gate 			/* Record the error in case we finally time out. */
13247c478bd9Sstevel@tonic-gate 			sctp->sctp_client_errno = EHOSTUNREACH;
13257c478bd9Sstevel@tonic-gate 			break;
13267c478bd9Sstevel@tonic-gate 		default:
13277c478bd9Sstevel@tonic-gate 			break;
13287c478bd9Sstevel@tonic-gate 		}
13297c478bd9Sstevel@tonic-gate 		break;
13307c478bd9Sstevel@tonic-gate 
13317c478bd9Sstevel@tonic-gate 	case ICMP6_PARAM_PROB:
13327c478bd9Sstevel@tonic-gate 		/* If this corresponds to an ICMP_PROTOCOL_UNREACHABLE */
13337c478bd9Sstevel@tonic-gate 		if (icmp6->icmp6_code == ICMP6_PARAMPROB_NEXTHEADER &&
13347c478bd9Sstevel@tonic-gate 		    (uchar_t *)ip6h + icmp6->icmp6_pptr ==
13357c478bd9Sstevel@tonic-gate 		    (uchar_t *)nexthdrp) {
13367c478bd9Sstevel@tonic-gate 			/* make sure the verification tag matches */
13377c478bd9Sstevel@tonic-gate 			if (!sctp_icmp_verf(sctp, sctpha, mp)) {
13387c478bd9Sstevel@tonic-gate 				break;
13397c478bd9Sstevel@tonic-gate 			}
13407c478bd9Sstevel@tonic-gate 			if (sctp->sctp_state == SCTPS_COOKIE_WAIT) {
1341f4b3ec61Sdh 				BUMP_MIB(&sctps->sctps_mib, sctpAborted);
1342c9da23f8Skcpoon 				sctp_assoc_event(sctp, SCTP_CANT_STR_ASSOC, 0,
1343c9da23f8Skcpoon 				    NULL);
13447c478bd9Sstevel@tonic-gate 				sctp_clean_death(sctp, ECONNREFUSED);
13457c478bd9Sstevel@tonic-gate 			}
13467c478bd9Sstevel@tonic-gate 			break;
13477c478bd9Sstevel@tonic-gate 		}
13487c478bd9Sstevel@tonic-gate 		break;
13497c478bd9Sstevel@tonic-gate 
13507c478bd9Sstevel@tonic-gate 	case ICMP6_TIME_EXCEEDED:
13517c478bd9Sstevel@tonic-gate 	default:
13527c478bd9Sstevel@tonic-gate 		break;
13537c478bd9Sstevel@tonic-gate 	}
13547c478bd9Sstevel@tonic-gate 	freemsg(mp);
13557c478bd9Sstevel@tonic-gate }
13567c478bd9Sstevel@tonic-gate 
13577c478bd9Sstevel@tonic-gate /*
13587c478bd9Sstevel@tonic-gate  * Called by sockfs to create a new sctp instance.
13597c478bd9Sstevel@tonic-gate  *
13607c478bd9Sstevel@tonic-gate  * If parent pointer is passed in, inherit settings from it.
13617c478bd9Sstevel@tonic-gate  */
13627c478bd9Sstevel@tonic-gate sctp_t *
1363*bd670b35SErik Nordmark sctp_create(void *ulpd, sctp_t *parent, int family, int type, int flags,
13640f1702c5SYu Xiangning     sock_upcalls_t *upcalls, sctp_sockbuf_limits_t *sbl,
13657c478bd9Sstevel@tonic-gate     cred_t *credp)
13667c478bd9Sstevel@tonic-gate {
13677c478bd9Sstevel@tonic-gate 	sctp_t		*sctp, *psctp;
1368*bd670b35SErik Nordmark 	conn_t		*connp;
13697c478bd9Sstevel@tonic-gate 	mblk_t		*ack_mp, *hb_mp;
13707c478bd9Sstevel@tonic-gate 	int		sleep = flags & SCTP_CAN_BLOCK ? KM_SLEEP : KM_NOSLEEP;
1371f4b3ec61Sdh 	zoneid_t	zoneid;
1372f4b3ec61Sdh 	sctp_stack_t	*sctps;
13737c478bd9Sstevel@tonic-gate 
13747c478bd9Sstevel@tonic-gate 	/* User must supply a credential. */
13757c478bd9Sstevel@tonic-gate 	if (credp == NULL)
13767c478bd9Sstevel@tonic-gate 		return (NULL);
13777c478bd9Sstevel@tonic-gate 
13787c478bd9Sstevel@tonic-gate 	psctp = (sctp_t *)parent;
1379f4b3ec61Sdh 	if (psctp != NULL) {
1380f4b3ec61Sdh 		sctps = psctp->sctp_sctps;
1381f4b3ec61Sdh 		/* Increase here to have common decrease at end */
1382f4b3ec61Sdh 		netstack_hold(sctps->sctps_netstack);
1383f4b3ec61Sdh 	} else {
1384f4b3ec61Sdh 		netstack_t *ns;
1385f4b3ec61Sdh 
1386f4b3ec61Sdh 		ns = netstack_find_by_cred(credp);
1387f4b3ec61Sdh 		ASSERT(ns != NULL);
1388f4b3ec61Sdh 		sctps = ns->netstack_sctp;
1389f4b3ec61Sdh 		ASSERT(sctps != NULL);
13907c478bd9Sstevel@tonic-gate 
1391f4b3ec61Sdh 		/*
1392f4b3ec61Sdh 		 * For exclusive stacks we set the zoneid to zero
1393f4b3ec61Sdh 		 * to make SCTP operate as if in the global zone.
1394f4b3ec61Sdh 		 */
1395f4b3ec61Sdh 		if (sctps->sctps_netstack->netstack_stackid !=
1396f4b3ec61Sdh 		    GLOBAL_NETSTACKID)
1397f4b3ec61Sdh 			zoneid = GLOBAL_ZONEID;
1398f4b3ec61Sdh 		else
1399f4b3ec61Sdh 			zoneid = crgetzoneid(credp);
1400f4b3ec61Sdh 	}
1401*bd670b35SErik Nordmark 	if ((connp = ipcl_conn_create(IPCL_SCTPCONN, sleep,
1402b34b8d1aSkcpoon 	    sctps->sctps_netstack)) == NULL) {
1403f4b3ec61Sdh 		netstack_rele(sctps->sctps_netstack);
1404f4b3ec61Sdh 		SCTP_KSTAT(sctps, sctp_conn_create);
1405f4b3ec61Sdh 		return (NULL);
1406f4b3ec61Sdh 	}
1407f4b3ec61Sdh 	/*
1408f4b3ec61Sdh 	 * ipcl_conn_create did a netstack_hold. Undo the hold that was
1409f4b3ec61Sdh 	 * done at top of sctp_create.
1410f4b3ec61Sdh 	 */
1411f4b3ec61Sdh 	netstack_rele(sctps->sctps_netstack);
1412*bd670b35SErik Nordmark 	sctp = CONN2SCTP(connp);
1413f4b3ec61Sdh 	sctp->sctp_sctps = sctps;
14147c478bd9Sstevel@tonic-gate 
1415121e5416Skcpoon 	if ((ack_mp = sctp_timer_alloc(sctp, sctp_ack_timer, sleep)) == NULL ||
1416121e5416Skcpoon 	    (hb_mp = sctp_timer_alloc(sctp, sctp_heartbeat_timer,
1417121e5416Skcpoon 	    sleep)) == NULL) {
14187c478bd9Sstevel@tonic-gate 		if (ack_mp != NULL)
14197c478bd9Sstevel@tonic-gate 			freeb(ack_mp);
1420*bd670b35SErik Nordmark 		sctp_conn_clear(connp);
1421f4b3ec61Sdh 		sctp->sctp_sctps = NULL;
1422*bd670b35SErik Nordmark 		kmem_cache_free(sctp_conn_cache, connp);
14237c478bd9Sstevel@tonic-gate 		return (NULL);
14247c478bd9Sstevel@tonic-gate 	}
14257c478bd9Sstevel@tonic-gate 
14267c478bd9Sstevel@tonic-gate 	sctp->sctp_ack_mp = ack_mp;
14277c478bd9Sstevel@tonic-gate 	sctp->sctp_heartbeat_mp = hb_mp;
14287c478bd9Sstevel@tonic-gate 
1429*bd670b35SErik Nordmark 	/*
1430*bd670b35SErik Nordmark 	 * Have conn_ip_output drop packets should our outer source
1431*bd670b35SErik Nordmark 	 * go invalid, and tell us about mtu changes.
1432*bd670b35SErik Nordmark 	 */
1433*bd670b35SErik Nordmark 	connp->conn_ixa->ixa_flags |= IXAF_SET_ULP_CKSUM | IXAF_VERIFY_SOURCE |
1434*bd670b35SErik Nordmark 	    IXAF_VERIFY_PMTU;
1435*bd670b35SErik Nordmark 	connp->conn_family = family;
1436*bd670b35SErik Nordmark 	connp->conn_so_type = type;
14377c478bd9Sstevel@tonic-gate 
14387c478bd9Sstevel@tonic-gate 	if (sctp_init_values(sctp, psctp, sleep) != 0) {
14397c478bd9Sstevel@tonic-gate 		freeb(ack_mp);
14407c478bd9Sstevel@tonic-gate 		freeb(hb_mp);
1441*bd670b35SErik Nordmark 		sctp_conn_clear(connp);
1442f4b3ec61Sdh 		sctp->sctp_sctps = NULL;
1443*bd670b35SErik Nordmark 		kmem_cache_free(sctp_conn_cache, connp);
14447c478bd9Sstevel@tonic-gate 		return (NULL);
14457c478bd9Sstevel@tonic-gate 	}
14467c478bd9Sstevel@tonic-gate 	sctp->sctp_cansleep = ((flags & SCTP_CAN_BLOCK) == SCTP_CAN_BLOCK);
14477c478bd9Sstevel@tonic-gate 
1448f4b3ec61Sdh 	sctp->sctp_mss = sctps->sctps_initial_mtu - ((family == AF_INET6) ?
1449b34b8d1aSkcpoon 	    sctp->sctp_hdr6_len : sctp->sctp_hdr_len);
14507c478bd9Sstevel@tonic-gate 
14517c478bd9Sstevel@tonic-gate 	if (psctp != NULL) {
1452*bd670b35SErik Nordmark 		conn_t	*pconnp = psctp->sctp_connp;
1453*bd670b35SErik Nordmark 
14547c478bd9Sstevel@tonic-gate 		RUN_SCTP(psctp);
14557c478bd9Sstevel@tonic-gate 		/*
14567c478bd9Sstevel@tonic-gate 		 * Inherit local address list, local port. Parent is either
14577c478bd9Sstevel@tonic-gate 		 * in SCTPS_BOUND, or SCTPS_LISTEN state.
14587c478bd9Sstevel@tonic-gate 		 */
14597c478bd9Sstevel@tonic-gate 		ASSERT((psctp->sctp_state == SCTPS_BOUND) ||
14607c478bd9Sstevel@tonic-gate 		    (psctp->sctp_state == SCTPS_LISTEN));
14617c478bd9Sstevel@tonic-gate 		if (sctp_dup_saddrs(psctp, sctp, sleep)) {
14627c478bd9Sstevel@tonic-gate 			WAKE_SCTP(psctp);
14637c478bd9Sstevel@tonic-gate 			freeb(ack_mp);
14647c478bd9Sstevel@tonic-gate 			freeb(hb_mp);
14657c478bd9Sstevel@tonic-gate 			sctp_headers_free(sctp);
1466*bd670b35SErik Nordmark 			sctp_conn_clear(connp);
1467f4b3ec61Sdh 			sctp->sctp_sctps = NULL;
1468*bd670b35SErik Nordmark 			kmem_cache_free(sctp_conn_cache, connp);
14697c478bd9Sstevel@tonic-gate 			return (NULL);
14707c478bd9Sstevel@tonic-gate 		}
14717c478bd9Sstevel@tonic-gate 
14727c478bd9Sstevel@tonic-gate 		/*
14737c478bd9Sstevel@tonic-gate 		 * If the parent is specified, it'll be immediatelly
14747c478bd9Sstevel@tonic-gate 		 * followed by sctp_connect(). So don't add this guy to
14757c478bd9Sstevel@tonic-gate 		 * bind hash.
14767c478bd9Sstevel@tonic-gate 		 */
1477*bd670b35SErik Nordmark 		connp->conn_lport = pconnp->conn_lport;
14787c478bd9Sstevel@tonic-gate 		sctp->sctp_state = SCTPS_BOUND;
14797c478bd9Sstevel@tonic-gate 		WAKE_SCTP(psctp);
14807c478bd9Sstevel@tonic-gate 	} else {
1481*bd670b35SErik Nordmark 		ASSERT(connp->conn_cred == NULL);
1482*bd670b35SErik Nordmark 		connp->conn_zoneid = zoneid;
1483*bd670b35SErik Nordmark 		/*
1484*bd670b35SErik Nordmark 		 * conn_allzones can not be set this early, hence
1485*bd670b35SErik Nordmark 		 * no IPCL_ZONEID
1486*bd670b35SErik Nordmark 		 */
1487*bd670b35SErik Nordmark 		connp->conn_ixa->ixa_zoneid = zoneid;
1488*bd670b35SErik Nordmark 		connp->conn_open_time = lbolt64;
1489*bd670b35SErik Nordmark 		connp->conn_cred = credp;
1490*bd670b35SErik Nordmark 		crhold(credp);
1491*bd670b35SErik Nordmark 		connp->conn_cpid = curproc->p_pid;
1492de8c4a14SErik Nordmark 
1493*bd670b35SErik Nordmark 		/*
1494*bd670b35SErik Nordmark 		 * If the caller has the process-wide flag set, then default to
1495*bd670b35SErik Nordmark 		 * MAC exempt mode.  This allows read-down to unlabeled hosts.
1496*bd670b35SErik Nordmark 		 */
1497*bd670b35SErik Nordmark 		if (getpflags(NET_MAC_AWARE, credp) != 0)
1498*bd670b35SErik Nordmark 			connp->conn_mac_mode = CONN_MAC_AWARE;
14997c478bd9Sstevel@tonic-gate 
1500*bd670b35SErik Nordmark 		connp->conn_zone_is_global =
1501*bd670b35SErik Nordmark 		    (crgetzoneid(credp) == GLOBAL_ZONEID);
1502*bd670b35SErik Nordmark 	}
150345916cd2Sjpk 
15047c478bd9Sstevel@tonic-gate 	/* Initialize SCTP instance values,  our verf tag must never be 0 */
15057c478bd9Sstevel@tonic-gate 	(void) random_get_pseudo_bytes((uint8_t *)&sctp->sctp_lvtag,
15067c478bd9Sstevel@tonic-gate 	    sizeof (sctp->sctp_lvtag));
15077c478bd9Sstevel@tonic-gate 	if (sctp->sctp_lvtag == 0)
15087c478bd9Sstevel@tonic-gate 		sctp->sctp_lvtag = (uint32_t)gethrtime();
15097c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_lvtag != 0);
15107c478bd9Sstevel@tonic-gate 
15117c478bd9Sstevel@tonic-gate 	sctp->sctp_ltsn = sctp->sctp_lvtag + 1;
15127c478bd9Sstevel@tonic-gate 	sctp->sctp_lcsn = sctp->sctp_ltsn;
15137c478bd9Sstevel@tonic-gate 	sctp->sctp_recovery_tsn = sctp->sctp_lastack_rxd = sctp->sctp_ltsn - 1;
15147c478bd9Sstevel@tonic-gate 	sctp->sctp_adv_pap = sctp->sctp_lastack_rxd;
15157c478bd9Sstevel@tonic-gate 
15167c478bd9Sstevel@tonic-gate 	/* Information required by upper layer */
1517*bd670b35SErik Nordmark 	ASSERT(ulpd != NULL);
1518*bd670b35SErik Nordmark 	sctp->sctp_ulpd = ulpd;
1519*bd670b35SErik Nordmark 
1520*bd670b35SErik Nordmark 	ASSERT(upcalls != NULL);
1521*bd670b35SErik Nordmark 	sctp->sctp_upcalls = upcalls;
1522*bd670b35SErik Nordmark 	ASSERT(sbl != NULL);
1523*bd670b35SErik Nordmark 	/* Fill in the socket buffer limits for sctpsockfs */
1524*bd670b35SErik Nordmark 	sbl->sbl_txlowat = connp->conn_sndlowat;
1525*bd670b35SErik Nordmark 	sbl->sbl_txbuf = connp->conn_sndbuf;
1526*bd670b35SErik Nordmark 	sbl->sbl_rxbuf = sctp->sctp_rwnd;
1527*bd670b35SErik Nordmark 	sbl->sbl_rxlowat = SCTP_RECV_LOWATER;
15287c478bd9Sstevel@tonic-gate 
15297c478bd9Sstevel@tonic-gate 	/* Insert this in the global list. */
1530f4b3ec61Sdh 	SCTP_LINK(sctp, sctps);
15317c478bd9Sstevel@tonic-gate 
15327c478bd9Sstevel@tonic-gate 	return (sctp);
15337c478bd9Sstevel@tonic-gate }
15347c478bd9Sstevel@tonic-gate 
1535f4b3ec61Sdh /* Run at module load time */
1536f4b3ec61Sdh void
1537f4b3ec61Sdh sctp_ddi_g_init(void)
1538f4b3ec61Sdh {
15397c478bd9Sstevel@tonic-gate 	/* Create sctp_t/conn_t cache */
15407c478bd9Sstevel@tonic-gate 	sctp_conn_cache_init();
15417c478bd9Sstevel@tonic-gate 
15427c478bd9Sstevel@tonic-gate 	/* Create the faddr cache */
15437c478bd9Sstevel@tonic-gate 	sctp_faddr_init();
15447c478bd9Sstevel@tonic-gate 
15457c478bd9Sstevel@tonic-gate 	/* Create the sets cache */
15467c478bd9Sstevel@tonic-gate 	sctp_sets_init();
15477c478bd9Sstevel@tonic-gate 
15487c478bd9Sstevel@tonic-gate 	/* Create the PR-SCTP sets cache */
15497c478bd9Sstevel@tonic-gate 	sctp_ftsn_sets_init();
15507c478bd9Sstevel@tonic-gate 
1551f4b3ec61Sdh 	/* Initialize tables used for CRC calculation */
1552f4b3ec61Sdh 	sctp_crc32_init();
1553f4b3ec61Sdh 
1554f4b3ec61Sdh 	/*
1555f4b3ec61Sdh 	 * We want to be informed each time a stack is created or
1556f4b3ec61Sdh 	 * destroyed in the kernel, so we can maintain the
1557f4b3ec61Sdh 	 * set of sctp_stack_t's.
1558f4b3ec61Sdh 	 */
1559*bd670b35SErik Nordmark 	netstack_register(NS_SCTP, sctp_stack_init, NULL, sctp_stack_fini);
1560f4b3ec61Sdh }
1561f4b3ec61Sdh 
1562f4b3ec61Sdh static void *
1563f4b3ec61Sdh sctp_stack_init(netstackid_t stackid, netstack_t *ns)
1564f4b3ec61Sdh {
1565f4b3ec61Sdh 	sctp_stack_t	*sctps;
1566f4b3ec61Sdh 
1567f4b3ec61Sdh 	sctps = kmem_zalloc(sizeof (*sctps), KM_SLEEP);
1568f4b3ec61Sdh 	sctps->sctps_netstack = ns;
1569f4b3ec61Sdh 
1570f4b3ec61Sdh 	/* Initialize locks */
1571f4b3ec61Sdh 	mutex_init(&sctps->sctps_g_lock, NULL, MUTEX_DEFAULT, NULL);
1572f4b3ec61Sdh 	mutex_init(&sctps->sctps_epriv_port_lock, NULL, MUTEX_DEFAULT, NULL);
1573f4b3ec61Sdh 	sctps->sctps_g_num_epriv_ports = SCTP_NUM_EPRIV_PORTS;
1574f4b3ec61Sdh 	sctps->sctps_g_epriv_ports[0] = 2049;
1575f4b3ec61Sdh 	sctps->sctps_g_epriv_ports[1] = 4045;
1576f4b3ec61Sdh 
1577f4b3ec61Sdh 	/* Initialize SCTP hash arrays. */
1578f4b3ec61Sdh 	sctp_hash_init(sctps);
1579f4b3ec61Sdh 
1580f4b3ec61Sdh 	if (!sctp_nd_init(sctps)) {
1581f4b3ec61Sdh 		sctp_nd_free(sctps);
1582f4b3ec61Sdh 	}
1583f4b3ec61Sdh 
15847c478bd9Sstevel@tonic-gate 	/* Initialize the recvq taskq. */
1585f4b3ec61Sdh 	sctp_rq_tq_init(sctps);
15867c478bd9Sstevel@tonic-gate 
15877c478bd9Sstevel@tonic-gate 	/* saddr init */
1588f4b3ec61Sdh 	sctp_saddr_init(sctps);
15897c478bd9Sstevel@tonic-gate 
15907c478bd9Sstevel@tonic-gate 	/* Global SCTP PCB list. */
1591f4b3ec61Sdh 	list_create(&sctps->sctps_g_list, sizeof (sctp_t),
15927c478bd9Sstevel@tonic-gate 	    offsetof(sctp_t, sctp_list));
15937c478bd9Sstevel@tonic-gate 
15947c478bd9Sstevel@tonic-gate 	/* Initialize sctp kernel stats. */
1595f4b3ec61Sdh 	sctps->sctps_mibkp = sctp_kstat_init(stackid);
1596f4b3ec61Sdh 	sctps->sctps_kstat =
1597f4b3ec61Sdh 	    sctp_kstat2_init(stackid, &sctps->sctps_statistics);
1598f4b3ec61Sdh 
1599f4b3ec61Sdh 	return (sctps);
16007c478bd9Sstevel@tonic-gate }
16017c478bd9Sstevel@tonic-gate 
1602f4b3ec61Sdh /*
1603f4b3ec61Sdh  * Called when the module is about to be unloaded.
1604f4b3ec61Sdh  */
16057c478bd9Sstevel@tonic-gate void
1606f4b3ec61Sdh sctp_ddi_g_destroy(void)
16077c478bd9Sstevel@tonic-gate {
16087c478bd9Sstevel@tonic-gate 	/* Destroy sctp_t/conn_t caches */
16097c478bd9Sstevel@tonic-gate 	sctp_conn_cache_fini();
16107c478bd9Sstevel@tonic-gate 
16117c478bd9Sstevel@tonic-gate 	/* Destroy the faddr cache */
16127c478bd9Sstevel@tonic-gate 	sctp_faddr_fini();
16137c478bd9Sstevel@tonic-gate 
16147c478bd9Sstevel@tonic-gate 	/* Destroy the sets cache */
16157c478bd9Sstevel@tonic-gate 	sctp_sets_fini();
16167c478bd9Sstevel@tonic-gate 
16177c478bd9Sstevel@tonic-gate 	/* Destroy the PR-SCTP sets cache */
16187c478bd9Sstevel@tonic-gate 	sctp_ftsn_sets_fini();
16197c478bd9Sstevel@tonic-gate 
1620f4b3ec61Sdh 	netstack_unregister(NS_SCTP);
1621f4b3ec61Sdh }
1622f4b3ec61Sdh 
1623f4b3ec61Sdh /*
1624f4b3ec61Sdh  * Free the SCTP stack instance.
1625f4b3ec61Sdh  */
1626f4b3ec61Sdh static void
1627f4b3ec61Sdh sctp_stack_fini(netstackid_t stackid, void *arg)
1628f4b3ec61Sdh {
1629f4b3ec61Sdh 	sctp_stack_t *sctps = (sctp_stack_t *)arg;
1630f4b3ec61Sdh 
1631f4b3ec61Sdh 	sctp_nd_free(sctps);
1632f4b3ec61Sdh 
16337c478bd9Sstevel@tonic-gate 	/* Destroy the recvq taskqs. */
1634f4b3ec61Sdh 	sctp_rq_tq_fini(sctps);
16357c478bd9Sstevel@tonic-gate 
16367c478bd9Sstevel@tonic-gate 	/* Destroy saddr  */
1637f4b3ec61Sdh 	sctp_saddr_fini(sctps);
16387c478bd9Sstevel@tonic-gate 
16397c478bd9Sstevel@tonic-gate 	/* Global SCTP PCB list. */
1640f4b3ec61Sdh 	list_destroy(&sctps->sctps_g_list);
16417c478bd9Sstevel@tonic-gate 
16427c478bd9Sstevel@tonic-gate 	/* Destroy SCTP hash arrays. */
1643f4b3ec61Sdh 	sctp_hash_destroy(sctps);
1644f4b3ec61Sdh 
1645f4b3ec61Sdh 	/* Destroy SCTP kernel stats. */
1646f4b3ec61Sdh 	sctp_kstat2_fini(stackid, sctps->sctps_kstat);
1647f4b3ec61Sdh 	sctps->sctps_kstat = NULL;
1648f4b3ec61Sdh 	bzero(&sctps->sctps_statistics, sizeof (sctps->sctps_statistics));
1649f4b3ec61Sdh 
1650f4b3ec61Sdh 	sctp_kstat_fini(stackid, sctps->sctps_mibkp);
1651f4b3ec61Sdh 	sctps->sctps_mibkp = 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 void
1660f4b3ec61Sdh sctp_display_all(sctp_stack_t *sctps)
16617c478bd9Sstevel@tonic-gate {
16627c478bd9Sstevel@tonic-gate 	sctp_t *sctp_walker;
16637c478bd9Sstevel@tonic-gate 
1664f4b3ec61Sdh 	mutex_enter(&sctps->sctps_g_lock);
1665*bd670b35SErik Nordmark 	for (sctp_walker = list_head(&sctps->sctps_g_list);
1666*bd670b35SErik Nordmark 	    sctp_walker != NULL;
1667f4b3ec61Sdh 	    sctp_walker = (sctp_t *)list_next(&sctps->sctps_g_list,
1668f4b3ec61Sdh 	    sctp_walker)) {
16697c478bd9Sstevel@tonic-gate 		(void) sctp_display(sctp_walker, NULL);
16707c478bd9Sstevel@tonic-gate 	}
1671f4b3ec61Sdh 	mutex_exit(&sctps->sctps_g_lock);
16727c478bd9Sstevel@tonic-gate }
16737c478bd9Sstevel@tonic-gate 
16747c478bd9Sstevel@tonic-gate static void
1675f4b3ec61Sdh sctp_rq_tq_init(sctp_stack_t *sctps)
16767c478bd9Sstevel@tonic-gate {
1677f4b3ec61Sdh 	sctps->sctps_recvq_tq_list_max_sz = 16;
1678f4b3ec61Sdh 	sctps->sctps_recvq_tq_list_cur_sz = 1;
16797c478bd9Sstevel@tonic-gate 	/*
16807c478bd9Sstevel@tonic-gate 	 * Initialize the recvq_tq_list and create the first recvq taskq.
16817c478bd9Sstevel@tonic-gate 	 * What to do if it fails?
16827c478bd9Sstevel@tonic-gate 	 */
1683f4b3ec61Sdh 	sctps->sctps_recvq_tq_list =
1684f4b3ec61Sdh 	    kmem_zalloc(sctps->sctps_recvq_tq_list_max_sz * sizeof (taskq_t *),
1685b34b8d1aSkcpoon 	    KM_SLEEP);
1686f4b3ec61Sdh 	sctps->sctps_recvq_tq_list[0] = taskq_create("sctp_def_recvq_taskq",
16877c478bd9Sstevel@tonic-gate 	    MIN(sctp_recvq_tq_thr_max, MAX(sctp_recvq_tq_thr_min, ncpus)),
16887c478bd9Sstevel@tonic-gate 	    minclsyspri, sctp_recvq_tq_task_min, sctp_recvq_tq_task_max,
16897c478bd9Sstevel@tonic-gate 	    TASKQ_PREPOPULATE);
1690f4b3ec61Sdh 	mutex_init(&sctps->sctps_rq_tq_lock, NULL, MUTEX_DEFAULT, NULL);
16917c478bd9Sstevel@tonic-gate }
16927c478bd9Sstevel@tonic-gate 
16937c478bd9Sstevel@tonic-gate static void
1694f4b3ec61Sdh sctp_rq_tq_fini(sctp_stack_t *sctps)
16957c478bd9Sstevel@tonic-gate {
16967c478bd9Sstevel@tonic-gate 	int i;
16977c478bd9Sstevel@tonic-gate 
1698f4b3ec61Sdh 	for (i = 0; i < sctps->sctps_recvq_tq_list_cur_sz; i++) {
1699f4b3ec61Sdh 		ASSERT(sctps->sctps_recvq_tq_list[i] != NULL);
1700f4b3ec61Sdh 		taskq_destroy(sctps->sctps_recvq_tq_list[i]);
17017c478bd9Sstevel@tonic-gate 	}
1702f4b3ec61Sdh 	kmem_free(sctps->sctps_recvq_tq_list,
1703f4b3ec61Sdh 	    sctps->sctps_recvq_tq_list_max_sz * sizeof (taskq_t *));
1704f4b3ec61Sdh 	sctps->sctps_recvq_tq_list = NULL;
17057c478bd9Sstevel@tonic-gate }
17067c478bd9Sstevel@tonic-gate 
17077c478bd9Sstevel@tonic-gate /* Add another taskq for a new ill. */
17087c478bd9Sstevel@tonic-gate void
1709f4b3ec61Sdh sctp_inc_taskq(sctp_stack_t *sctps)
17107c478bd9Sstevel@tonic-gate {
17117c478bd9Sstevel@tonic-gate 	taskq_t *tq;
17127c478bd9Sstevel@tonic-gate 	char tq_name[TASKQ_NAMELEN];
17137c478bd9Sstevel@tonic-gate 
1714f4b3ec61Sdh 	mutex_enter(&sctps->sctps_rq_tq_lock);
1715f4b3ec61Sdh 	if (sctps->sctps_recvq_tq_list_cur_sz + 1 >
1716f4b3ec61Sdh 	    sctps->sctps_recvq_tq_list_max_sz) {
1717f4b3ec61Sdh 		mutex_exit(&sctps->sctps_rq_tq_lock);
17187c478bd9Sstevel@tonic-gate 		cmn_err(CE_NOTE, "Cannot create more SCTP recvq taskq");
17197c478bd9Sstevel@tonic-gate 		return;
17207c478bd9Sstevel@tonic-gate 	}
17217c478bd9Sstevel@tonic-gate 
17227c478bd9Sstevel@tonic-gate 	(void) snprintf(tq_name, sizeof (tq_name), "sctp_recvq_taskq_%u",
1723f4b3ec61Sdh 	    sctps->sctps_recvq_tq_list_cur_sz);
17247c478bd9Sstevel@tonic-gate 	tq = taskq_create(tq_name,
17257c478bd9Sstevel@tonic-gate 	    MIN(sctp_recvq_tq_thr_max, MAX(sctp_recvq_tq_thr_min, ncpus)),
17267c478bd9Sstevel@tonic-gate 	    minclsyspri, sctp_recvq_tq_task_min, sctp_recvq_tq_task_max,
17277c478bd9Sstevel@tonic-gate 	    TASKQ_PREPOPULATE);
17287c478bd9Sstevel@tonic-gate 	if (tq == NULL) {
1729f4b3ec61Sdh 		mutex_exit(&sctps->sctps_rq_tq_lock);
17307c478bd9Sstevel@tonic-gate 		cmn_err(CE_NOTE, "SCTP recvq taskq creation failed");
17317c478bd9Sstevel@tonic-gate 		return;
17327c478bd9Sstevel@tonic-gate 	}
1733f4b3ec61Sdh 	ASSERT(sctps->sctps_recvq_tq_list[
1734b34b8d1aSkcpoon 	    sctps->sctps_recvq_tq_list_cur_sz] == NULL);
1735f4b3ec61Sdh 	sctps->sctps_recvq_tq_list[sctps->sctps_recvq_tq_list_cur_sz] = tq;
1736f4b3ec61Sdh 	atomic_add_32(&sctps->sctps_recvq_tq_list_cur_sz, 1);
1737f4b3ec61Sdh 	mutex_exit(&sctps->sctps_rq_tq_lock);
17387c478bd9Sstevel@tonic-gate }
17397c478bd9Sstevel@tonic-gate 
17407c478bd9Sstevel@tonic-gate #ifdef DEBUG
17417c478bd9Sstevel@tonic-gate uint32_t recvq_loop_cnt = 0;
17427c478bd9Sstevel@tonic-gate uint32_t recvq_call = 0;
17437c478bd9Sstevel@tonic-gate #endif
17447c478bd9Sstevel@tonic-gate 
17457c478bd9Sstevel@tonic-gate /*
17467c478bd9Sstevel@tonic-gate  * Find the next recvq_tq to use.  This routine will go thru all the
17477c478bd9Sstevel@tonic-gate  * taskqs until it can dispatch a job for the sctp.  If this fails,
17487c478bd9Sstevel@tonic-gate  * it will create a new taskq and try it.
17497c478bd9Sstevel@tonic-gate  */
17507c478bd9Sstevel@tonic-gate static boolean_t
17517c478bd9Sstevel@tonic-gate sctp_find_next_tq(sctp_t *sctp)
17527c478bd9Sstevel@tonic-gate {
17537c478bd9Sstevel@tonic-gate 	int next_tq, try;
17547c478bd9Sstevel@tonic-gate 	taskq_t *tq;
1755f4b3ec61Sdh 	sctp_stack_t	*sctps = sctp->sctp_sctps;
17567c478bd9Sstevel@tonic-gate 
17577c478bd9Sstevel@tonic-gate 	/*
17587c478bd9Sstevel@tonic-gate 	 * Note that since we don't hold a lock on sctp_rq_tq_lock for
17597c478bd9Sstevel@tonic-gate 	 * performance reason, recvq_ta_list_cur_sz can be changed during
17607c478bd9Sstevel@tonic-gate 	 * this loop.  The problem this will create is that the loop may
17617c478bd9Sstevel@tonic-gate 	 * not have tried all the recvq_tq.  This should be OK.
17627c478bd9Sstevel@tonic-gate 	 */
1763f4b3ec61Sdh 	next_tq = atomic_add_32_nv(&sctps->sctps_recvq_tq_list_cur, 1) %
1764f4b3ec61Sdh 	    sctps->sctps_recvq_tq_list_cur_sz;
1765f4b3ec61Sdh 	for (try = 0; try < sctps->sctps_recvq_tq_list_cur_sz; try++) {
1766f4b3ec61Sdh 		tq = sctps->sctps_recvq_tq_list[next_tq];
17677c478bd9Sstevel@tonic-gate 		if (taskq_dispatch(tq, sctp_process_recvq, sctp,
17687c478bd9Sstevel@tonic-gate 		    TQ_NOSLEEP) != NULL) {
17697c478bd9Sstevel@tonic-gate 			sctp->sctp_recvq_tq = tq;
17707c478bd9Sstevel@tonic-gate 			return (B_TRUE);
17717c478bd9Sstevel@tonic-gate 		}
1772f4b3ec61Sdh 		next_tq = (next_tq + 1) % sctps->sctps_recvq_tq_list_cur_sz;
17737c478bd9Sstevel@tonic-gate 	}
17747c478bd9Sstevel@tonic-gate 
17757c478bd9Sstevel@tonic-gate 	/*
17767c478bd9Sstevel@tonic-gate 	 * Create one more taskq and try it.  Note that sctp_inc_taskq()
17777c478bd9Sstevel@tonic-gate 	 * may not have created another taskq if the number of recvq
17787c478bd9Sstevel@tonic-gate 	 * taskqs is at the maximum.  We are probably in a pretty bad
17797c478bd9Sstevel@tonic-gate 	 * shape if this actually happens...
17807c478bd9Sstevel@tonic-gate 	 */
1781f4b3ec61Sdh 	sctp_inc_taskq(sctps);
1782f4b3ec61Sdh 	tq = sctps->sctps_recvq_tq_list[sctps->sctps_recvq_tq_list_cur_sz - 1];
17837c478bd9Sstevel@tonic-gate 	if (taskq_dispatch(tq, sctp_process_recvq, sctp, TQ_NOSLEEP) != NULL) {
17847c478bd9Sstevel@tonic-gate 		sctp->sctp_recvq_tq = tq;
17857c478bd9Sstevel@tonic-gate 		return (B_TRUE);
17867c478bd9Sstevel@tonic-gate 	}
1787f4b3ec61Sdh 	SCTP_KSTAT(sctps, sctp_find_next_tq);
17887c478bd9Sstevel@tonic-gate 	return (B_FALSE);
17897c478bd9Sstevel@tonic-gate }
17907c478bd9Sstevel@tonic-gate 
17917c478bd9Sstevel@tonic-gate /*
17927c478bd9Sstevel@tonic-gate  * To add a message to the recvq.  Note that the sctp_timer_fire()
17937c478bd9Sstevel@tonic-gate  * routine also uses this function to add the timer message to the
17947c478bd9Sstevel@tonic-gate  * receive queue for later processing.  And it should be the only
17957c478bd9Sstevel@tonic-gate  * caller of sctp_add_recvq() which sets the try_harder argument
17967c478bd9Sstevel@tonic-gate  * to B_TRUE.
17977c478bd9Sstevel@tonic-gate  *
17987c478bd9Sstevel@tonic-gate  * If the try_harder argument is B_TRUE, this routine sctp_find_next_tq()
17997c478bd9Sstevel@tonic-gate  * will try very hard to dispatch the task.  Refer to the comment
18007c478bd9Sstevel@tonic-gate  * for that routine on how it does that.
1801*bd670b35SErik Nordmark  *
1802*bd670b35SErik Nordmark  * On failure the message has been freed i.e., this routine always consumes the
1803*bd670b35SErik Nordmark  * message. It bumps ipIfStatsInDiscards and and uses ip_drop_input to drop.
18047c478bd9Sstevel@tonic-gate  */
1805*bd670b35SErik Nordmark void
1806*bd670b35SErik Nordmark sctp_add_recvq(sctp_t *sctp, mblk_t *mp, boolean_t caller_hold_lock,
1807*bd670b35SErik Nordmark     ip_recv_attr_t *ira)
18087c478bd9Sstevel@tonic-gate {
1809*bd670b35SErik Nordmark 	mblk_t	*attrmp;
1810*bd670b35SErik Nordmark 	ip_stack_t	*ipst = sctp->sctp_sctps->sctps_netstack->netstack_ip;
1811*bd670b35SErik Nordmark 
1812*bd670b35SErik Nordmark 	ASSERT(ira->ira_ill == NULL);
1813*bd670b35SErik Nordmark 
18147c478bd9Sstevel@tonic-gate 	if (!caller_hold_lock)
18157c478bd9Sstevel@tonic-gate 		mutex_enter(&sctp->sctp_recvq_lock);
18167c478bd9Sstevel@tonic-gate 
18177c478bd9Sstevel@tonic-gate 	/* If the taskq dispatch has not been scheduled, do it now. */
18187c478bd9Sstevel@tonic-gate 	if (sctp->sctp_recvq_tq == NULL) {
18197c478bd9Sstevel@tonic-gate 		ASSERT(sctp->sctp_recvq == NULL);
18207c478bd9Sstevel@tonic-gate 		if (!sctp_find_next_tq(sctp)) {
18217c478bd9Sstevel@tonic-gate 			if (!caller_hold_lock)
18227c478bd9Sstevel@tonic-gate 				mutex_exit(&sctp->sctp_recvq_lock);
1823*bd670b35SErik Nordmark 			BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsInDiscards);
1824*bd670b35SErik Nordmark 			ip_drop_input("ipIfStatsInDiscards", mp, NULL);
1825*bd670b35SErik Nordmark 			freemsg(mp);
1826*bd670b35SErik Nordmark 			return;
18277c478bd9Sstevel@tonic-gate 		}
18287c478bd9Sstevel@tonic-gate 		/* Make sure the sctp_t will not go away. */
18297c478bd9Sstevel@tonic-gate 		SCTP_REFHOLD(sctp);
18307c478bd9Sstevel@tonic-gate 	}
18317c478bd9Sstevel@tonic-gate 
1832*bd670b35SErik Nordmark 	attrmp = ip_recv_attr_to_mblk(ira);
1833*bd670b35SErik Nordmark 	if (attrmp == NULL) {
1834*bd670b35SErik Nordmark 		if (!caller_hold_lock)
1835*bd670b35SErik Nordmark 			mutex_exit(&sctp->sctp_recvq_lock);
1836*bd670b35SErik Nordmark 		BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsInDiscards);
1837*bd670b35SErik Nordmark 		ip_drop_input("ipIfStatsInDiscards", mp, NULL);
1838*bd670b35SErik Nordmark 		freemsg(mp);
1839*bd670b35SErik Nordmark 		return;
1840*bd670b35SErik Nordmark 	}
1841*bd670b35SErik Nordmark 	ASSERT(attrmp->b_cont == NULL);
1842*bd670b35SErik Nordmark 	attrmp->b_cont = mp;
1843*bd670b35SErik Nordmark 	mp = attrmp;
1844*bd670b35SErik Nordmark 
18457c478bd9Sstevel@tonic-gate 	if (sctp->sctp_recvq == NULL) {
18467c478bd9Sstevel@tonic-gate 		sctp->sctp_recvq = mp;
18477c478bd9Sstevel@tonic-gate 		sctp->sctp_recvq_tail = mp;
18487c478bd9Sstevel@tonic-gate 	} else {
18497c478bd9Sstevel@tonic-gate 		sctp->sctp_recvq_tail->b_next = mp;
18507c478bd9Sstevel@tonic-gate 		sctp->sctp_recvq_tail = mp;
18517c478bd9Sstevel@tonic-gate 	}
18527c478bd9Sstevel@tonic-gate 
18537c478bd9Sstevel@tonic-gate 	if (!caller_hold_lock)
18547c478bd9Sstevel@tonic-gate 		mutex_exit(&sctp->sctp_recvq_lock);
18557c478bd9Sstevel@tonic-gate }
18567c478bd9Sstevel@tonic-gate 
18577c478bd9Sstevel@tonic-gate static void
18587c478bd9Sstevel@tonic-gate sctp_process_recvq(void *arg)
18597c478bd9Sstevel@tonic-gate {
18607c478bd9Sstevel@tonic-gate 	sctp_t		*sctp = (sctp_t *)arg;
18617c478bd9Sstevel@tonic-gate 	mblk_t		*mp;
18627c478bd9Sstevel@tonic-gate #ifdef DEBUG
18637c478bd9Sstevel@tonic-gate 	uint32_t	loop_cnt = 0;
18647c478bd9Sstevel@tonic-gate #endif
1865*bd670b35SErik Nordmark 	ip_recv_attr_t	iras;
18667c478bd9Sstevel@tonic-gate 
18677c478bd9Sstevel@tonic-gate #ifdef	_BIG_ENDIAN
18687c478bd9Sstevel@tonic-gate #define	IPVER(ip6h)	((((uint32_t *)ip6h)[0] >> 28) & 0x7)
18697c478bd9Sstevel@tonic-gate #else
18707c478bd9Sstevel@tonic-gate #define	IPVER(ip6h)	((((uint32_t *)ip6h)[0] >> 4) & 0x7)
18717c478bd9Sstevel@tonic-gate #endif
18727c478bd9Sstevel@tonic-gate 
18737c478bd9Sstevel@tonic-gate 	RUN_SCTP(sctp);
18747c478bd9Sstevel@tonic-gate 	mutex_enter(&sctp->sctp_recvq_lock);
18757c478bd9Sstevel@tonic-gate 
18767c478bd9Sstevel@tonic-gate #ifdef DEBUG
18777c478bd9Sstevel@tonic-gate 	recvq_call++;
18787c478bd9Sstevel@tonic-gate #endif
18797c478bd9Sstevel@tonic-gate 	/*
18807c478bd9Sstevel@tonic-gate 	 * Note that while we are in this loop, other thread can put
18817c478bd9Sstevel@tonic-gate 	 * new packets in the receive queue.  We may be looping for
18827c478bd9Sstevel@tonic-gate 	 * quite a while.
18837c478bd9Sstevel@tonic-gate 	 */
18847c478bd9Sstevel@tonic-gate 	while ((mp = sctp->sctp_recvq) != NULL) {
1885*bd670b35SErik Nordmark 		mblk_t *data_mp;
1886*bd670b35SErik Nordmark 
18877c478bd9Sstevel@tonic-gate 		sctp->sctp_recvq = mp->b_next;
18887c478bd9Sstevel@tonic-gate 		mutex_exit(&sctp->sctp_recvq_lock);
18897c478bd9Sstevel@tonic-gate 		mp->b_next = NULL;
18907c478bd9Sstevel@tonic-gate #ifdef DEBUG
18917c478bd9Sstevel@tonic-gate 		loop_cnt++;
18927c478bd9Sstevel@tonic-gate #endif
18937c478bd9Sstevel@tonic-gate 		mp->b_prev = NULL;
18947c478bd9Sstevel@tonic-gate 
1895*bd670b35SErik Nordmark 		data_mp = mp->b_cont;
1896*bd670b35SErik Nordmark 		mp->b_cont = NULL;
1897*bd670b35SErik Nordmark 		if (!ip_recv_attr_from_mblk(mp, &iras)) {
1898*bd670b35SErik Nordmark 			ip_drop_input("ip_recv_attr_from_mblk", mp, NULL);
1899*bd670b35SErik Nordmark 			freemsg(mp);
1900*bd670b35SErik Nordmark 			ira_cleanup(&iras, B_TRUE);
1901*bd670b35SErik Nordmark 			continue;
1902*bd670b35SErik Nordmark 		}
1903*bd670b35SErik Nordmark 
1904*bd670b35SErik Nordmark 		if (iras.ira_flags & IRAF_ICMP_ERROR)
1905*bd670b35SErik Nordmark 			sctp_icmp_error(sctp, data_mp);
1906*bd670b35SErik Nordmark 		else
1907*bd670b35SErik Nordmark 			sctp_input_data(sctp, data_mp, &iras);
1908*bd670b35SErik Nordmark 
1909*bd670b35SErik Nordmark 		ira_cleanup(&iras, B_TRUE);
19107c478bd9Sstevel@tonic-gate 		mutex_enter(&sctp->sctp_recvq_lock);
19117c478bd9Sstevel@tonic-gate 	}
19127c478bd9Sstevel@tonic-gate 
19137c478bd9Sstevel@tonic-gate 	sctp->sctp_recvq_tail = NULL;
19147c478bd9Sstevel@tonic-gate 	sctp->sctp_recvq_tq = NULL;
19157c478bd9Sstevel@tonic-gate 
19167c478bd9Sstevel@tonic-gate 	mutex_exit(&sctp->sctp_recvq_lock);
19177c478bd9Sstevel@tonic-gate 
19187c478bd9Sstevel@tonic-gate 	WAKE_SCTP(sctp);
19197c478bd9Sstevel@tonic-gate 
19207c478bd9Sstevel@tonic-gate #ifdef DEBUG
19217c478bd9Sstevel@tonic-gate 	if (loop_cnt > recvq_loop_cnt)
19227c478bd9Sstevel@tonic-gate 		recvq_loop_cnt = loop_cnt;
19237c478bd9Sstevel@tonic-gate #endif
19247c478bd9Sstevel@tonic-gate 	/* Now it can go away. */
19257c478bd9Sstevel@tonic-gate 	SCTP_REFRELE(sctp);
19267c478bd9Sstevel@tonic-gate }
19277c478bd9Sstevel@tonic-gate 
19287c478bd9Sstevel@tonic-gate /* ARGSUSED */
19297c478bd9Sstevel@tonic-gate static int
19307c478bd9Sstevel@tonic-gate sctp_conn_cache_constructor(void *buf, void *cdrarg, int kmflags)
19317c478bd9Sstevel@tonic-gate {
1932*bd670b35SErik Nordmark 	conn_t	*connp = (conn_t *)buf;
1933*bd670b35SErik Nordmark 	sctp_t	*sctp = (sctp_t *)&connp[1];
19347c478bd9Sstevel@tonic-gate 
1935*bd670b35SErik Nordmark 	bzero(connp, sizeof (conn_t));
19367c478bd9Sstevel@tonic-gate 	bzero(buf, (char *)&sctp[1] - (char *)buf);
19377c478bd9Sstevel@tonic-gate 
19387c478bd9Sstevel@tonic-gate 	mutex_init(&sctp->sctp_reflock, NULL, MUTEX_DEFAULT, NULL);
19397c478bd9Sstevel@tonic-gate 	mutex_init(&sctp->sctp_lock, NULL, MUTEX_DEFAULT, NULL);
19407c478bd9Sstevel@tonic-gate 	mutex_init(&sctp->sctp_recvq_lock, NULL, MUTEX_DEFAULT, NULL);
19417c478bd9Sstevel@tonic-gate 	cv_init(&sctp->sctp_cv, NULL, CV_DEFAULT, NULL);
19427c478bd9Sstevel@tonic-gate 
1943*bd670b35SErik Nordmark 	mutex_init(&connp->conn_lock, NULL, MUTEX_DEFAULT, NULL);
1944*bd670b35SErik Nordmark 	cv_init(&connp->conn_cv, NULL, CV_DEFAULT, NULL);
1945*bd670b35SErik Nordmark 	connp->conn_flags = IPCL_SCTPCONN;
1946*bd670b35SErik Nordmark 	connp->conn_proto = IPPROTO_SCTP;
1947*bd670b35SErik Nordmark 	connp->conn_sctp = sctp;
1948*bd670b35SErik Nordmark 	sctp->sctp_connp = connp;
1949*bd670b35SErik Nordmark 	rw_init(&connp->conn_ilg_lock, NULL, RW_DEFAULT, NULL);
1950*bd670b35SErik Nordmark 
1951*bd670b35SErik Nordmark 	connp->conn_ixa = kmem_zalloc(sizeof (ip_xmit_attr_t), kmflags);
1952*bd670b35SErik Nordmark 	if (connp->conn_ixa == NULL) {
1953*bd670b35SErik Nordmark 		return (ENOMEM);
1954*bd670b35SErik Nordmark 	}
1955*bd670b35SErik Nordmark 	connp->conn_ixa->ixa_refcnt = 1;
1956*bd670b35SErik Nordmark 	connp->conn_ixa->ixa_protocol = connp->conn_proto;
1957*bd670b35SErik Nordmark 	connp->conn_ixa->ixa_xmit_hint = CONN_TO_XMIT_HINT(connp);
19587c478bd9Sstevel@tonic-gate 	return (0);
19597c478bd9Sstevel@tonic-gate }
19607c478bd9Sstevel@tonic-gate 
19617c478bd9Sstevel@tonic-gate /* ARGSUSED */
19627c478bd9Sstevel@tonic-gate static void
19637c478bd9Sstevel@tonic-gate sctp_conn_cache_destructor(void *buf, void *cdrarg)
19647c478bd9Sstevel@tonic-gate {
1965*bd670b35SErik Nordmark 	conn_t	*connp = (conn_t *)buf;
1966*bd670b35SErik Nordmark 	sctp_t	*sctp = (sctp_t *)&connp[1];
19677c478bd9Sstevel@tonic-gate 
1968*bd670b35SErik Nordmark 	ASSERT(sctp->sctp_connp == connp);
19697c478bd9Sstevel@tonic-gate 	ASSERT(!MUTEX_HELD(&sctp->sctp_lock));
19707c478bd9Sstevel@tonic-gate 	ASSERT(!MUTEX_HELD(&sctp->sctp_reflock));
19717c478bd9Sstevel@tonic-gate 	ASSERT(!MUTEX_HELD(&sctp->sctp_recvq_lock));
19727c478bd9Sstevel@tonic-gate 
19737c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_conn_hash_next == NULL);
19747c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_conn_hash_prev == NULL);
19757c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_listen_hash_next == NULL);
19767c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_listen_hash_prev == NULL);
19777c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_listen_tfp == NULL);
19787c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_conn_tfp == NULL);
19797c478bd9Sstevel@tonic-gate 
19807c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_faddrs == NULL);
19817c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_nsaddrs == 0);
19827c478bd9Sstevel@tonic-gate 
19837c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_ulpd == NULL);
19847c478bd9Sstevel@tonic-gate 
19857c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_lastfaddr == NULL);
19867c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_primary == NULL);
19877c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_current == NULL);
19887c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_lastdata == NULL);
19897c478bd9Sstevel@tonic-gate 
19907c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_xmit_head == NULL);
19917c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_xmit_tail == NULL);
19927c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_xmit_unsent == NULL);
19937c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_xmit_unsent_tail == NULL);
19947c478bd9Sstevel@tonic-gate 
19957c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_ostrcntrs == NULL);
19967c478bd9Sstevel@tonic-gate 
19977c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_sack_info == NULL);
19987c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_ack_mp == NULL);
19997c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_instr == NULL);
20007c478bd9Sstevel@tonic-gate 
20017c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_iphc == NULL);
20027c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_iphc6 == NULL);
20037c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_ipha == NULL);
20047c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_ip6h == NULL);
20057c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_sctph == NULL);
20067c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_sctph6 == NULL);
20077c478bd9Sstevel@tonic-gate 
20087c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_cookie_mp == NULL);
20097c478bd9Sstevel@tonic-gate 
20107c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_refcnt == 0);
20117c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_timer_mp == NULL);
20127c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_connp->conn_ref == 0);
20137c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_heartbeat_mp == NULL);
20147c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_ptpbhn == NULL && sctp->sctp_bind_hash == NULL);
20157c478bd9Sstevel@tonic-gate 
20167c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_shutdown_faddr == NULL);
20177c478bd9Sstevel@tonic-gate 
20187c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_cxmit_list == NULL);
20197c478bd9Sstevel@tonic-gate 
20207c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_recvq == NULL);
20217c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_recvq_tail == NULL);
20227c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_recvq_tq == NULL);
20237c478bd9Sstevel@tonic-gate 
2024121e5416Skcpoon 	/*
2025121e5416Skcpoon 	 * sctp_pad_mp can be NULL if the memory allocation fails
2026121e5416Skcpoon 	 * in sctp_init_values() and the conn_t is freed.
2027121e5416Skcpoon 	 */
2028121e5416Skcpoon 	if (sctp->sctp_pad_mp != NULL) {
2029121e5416Skcpoon 		freeb(sctp->sctp_pad_mp);
2030121e5416Skcpoon 		sctp->sctp_pad_mp = NULL;
2031121e5416Skcpoon 	}
2032121e5416Skcpoon 
20337c478bd9Sstevel@tonic-gate 	mutex_destroy(&sctp->sctp_reflock);
20347c478bd9Sstevel@tonic-gate 	mutex_destroy(&sctp->sctp_lock);
20357c478bd9Sstevel@tonic-gate 	mutex_destroy(&sctp->sctp_recvq_lock);
20367c478bd9Sstevel@tonic-gate 	cv_destroy(&sctp->sctp_cv);
20377c478bd9Sstevel@tonic-gate 
2038*bd670b35SErik Nordmark 	mutex_destroy(&connp->conn_lock);
2039*bd670b35SErik Nordmark 	cv_destroy(&connp->conn_cv);
2040*bd670b35SErik Nordmark 	rw_destroy(&connp->conn_ilg_lock);
2041*bd670b35SErik Nordmark 
2042*bd670b35SErik Nordmark 	/* Can be NULL if constructor failed */
2043*bd670b35SErik Nordmark 	if (connp->conn_ixa != NULL) {
2044*bd670b35SErik Nordmark 		ASSERT(connp->conn_ixa->ixa_refcnt == 1);
2045*bd670b35SErik Nordmark 		ASSERT(connp->conn_ixa->ixa_ire == NULL);
2046*bd670b35SErik Nordmark 		ASSERT(connp->conn_ixa->ixa_nce == NULL);
2047*bd670b35SErik Nordmark 		ixa_refrele(connp->conn_ixa);
2048*bd670b35SErik Nordmark 	}
20497c478bd9Sstevel@tonic-gate }
20507c478bd9Sstevel@tonic-gate 
20517c478bd9Sstevel@tonic-gate static void
20527c478bd9Sstevel@tonic-gate sctp_conn_cache_init()
20537c478bd9Sstevel@tonic-gate {
20547c478bd9Sstevel@tonic-gate 	sctp_conn_cache = kmem_cache_create("sctp_conn_cache",
20557c478bd9Sstevel@tonic-gate 	    sizeof (sctp_t) + sizeof (conn_t), 0, sctp_conn_cache_constructor,
20567c478bd9Sstevel@tonic-gate 	    sctp_conn_cache_destructor, NULL, NULL, NULL, 0);
20577c478bd9Sstevel@tonic-gate }
20587c478bd9Sstevel@tonic-gate 
20597c478bd9Sstevel@tonic-gate static void
20607c478bd9Sstevel@tonic-gate sctp_conn_cache_fini()
20617c478bd9Sstevel@tonic-gate {
20627c478bd9Sstevel@tonic-gate 	kmem_cache_destroy(sctp_conn_cache);
20637c478bd9Sstevel@tonic-gate }
2064121e5416Skcpoon 
2065121e5416Skcpoon void
2066121e5416Skcpoon sctp_conn_init(conn_t *connp)
2067121e5416Skcpoon {
2068*bd670b35SErik Nordmark 	ASSERT(connp->conn_flags == IPCL_SCTPCONN);
2069121e5416Skcpoon 	connp->conn_rq = connp->conn_wq = NULL;
2070*bd670b35SErik Nordmark 	connp->conn_ixa->ixa_flags |= IXAF_SET_ULP_CKSUM | IXAF_VERIFY_SOURCE |
2071*bd670b35SErik Nordmark 	    IXAF_VERIFY_PMTU;
2072*bd670b35SErik Nordmark 
2073*bd670b35SErik Nordmark 	ASSERT(connp->conn_proto == IPPROTO_SCTP);
2074*bd670b35SErik Nordmark 	ASSERT(connp->conn_ixa->ixa_protocol == connp->conn_proto);
2075121e5416Skcpoon 	connp->conn_state_flags |= CONN_INCIPIENT;
2076*bd670b35SErik Nordmark 
2077*bd670b35SErik Nordmark 	ASSERT(connp->conn_sctp != NULL);
2078*bd670b35SErik Nordmark 
2079*bd670b35SErik Nordmark 	/*
2080*bd670b35SErik Nordmark 	 * Register sctp_notify to listen to capability changes detected by IP.
2081*bd670b35SErik Nordmark 	 * This upcall is made in the context of the call to conn_ip_output
2082*bd670b35SErik Nordmark 	 * thus it holds whatever locks sctp holds across conn_ip_output.
2083*bd670b35SErik Nordmark 	 */
2084*bd670b35SErik Nordmark 	connp->conn_ixa->ixa_notify = sctp_notify;
2085*bd670b35SErik Nordmark 	connp->conn_ixa->ixa_notify_cookie = connp->conn_sctp;
2086121e5416Skcpoon }
2087121e5416Skcpoon 
2088121e5416Skcpoon static void
2089121e5416Skcpoon sctp_conn_clear(conn_t *connp)
2090121e5416Skcpoon {
2091121e5416Skcpoon 	/* Clean up conn_t stuff */
2092*bd670b35SErik Nordmark 	if (connp->conn_latch != NULL) {
2093*bd670b35SErik Nordmark 		IPLATCH_REFRELE(connp->conn_latch);
2094*bd670b35SErik Nordmark 		connp->conn_latch = NULL;
2095*bd670b35SErik Nordmark 	}
2096*bd670b35SErik Nordmark 	if (connp->conn_latch_in_policy != NULL) {
2097*bd670b35SErik Nordmark 		IPPOL_REFRELE(connp->conn_latch_in_policy);
2098*bd670b35SErik Nordmark 		connp->conn_latch_in_policy = NULL;
2099*bd670b35SErik Nordmark 	}
2100*bd670b35SErik Nordmark 	if (connp->conn_latch_in_action != NULL) {
2101*bd670b35SErik Nordmark 		IPACT_REFRELE(connp->conn_latch_in_action);
2102*bd670b35SErik Nordmark 		connp->conn_latch_in_action = NULL;
2103*bd670b35SErik Nordmark 	}
2104*bd670b35SErik Nordmark 	if (connp->conn_policy != NULL) {
2105121e5416Skcpoon 		IPPH_REFRELE(connp->conn_policy, connp->conn_netstack);
2106*bd670b35SErik Nordmark 		connp->conn_policy = NULL;
2107*bd670b35SErik Nordmark 	}
2108*bd670b35SErik Nordmark 	if (connp->conn_ipsec_opt_mp != NULL) {
2109121e5416Skcpoon 		freemsg(connp->conn_ipsec_opt_mp);
2110*bd670b35SErik Nordmark 		connp->conn_ipsec_opt_mp = NULL;
2111*bd670b35SErik Nordmark 	}
2112121e5416Skcpoon 	netstack_rele(connp->conn_netstack);
2113*bd670b35SErik Nordmark 	connp->conn_netstack = NULL;
2114*bd670b35SErik Nordmark 
2115*bd670b35SErik Nordmark 	/* Leave conn_ixa and other constructed fields in place */
2116*bd670b35SErik Nordmark 	ipcl_conn_cleanup(connp);
2117121e5416Skcpoon }
2118