xref: /illumos-gate/usr/src/uts/common/inet/sctp/sctp.c (revision 92baa190)
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>
59bd670b35SErik Nordmark #include <inet/ip_if.h>
60bd670b35SErik 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 *);
90bd670b35SErik Nordmark static void	sctp_notify(void *, ip_xmit_attr_t *, ixa_notify_type_t,
91bd670b35SErik 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;
175bd670b35SErik Nordmark 	conn_t	*connp;
176bd670b35SErik 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 
211bd670b35SErik 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 {
294bd670b35SErik Nordmark 	int		error = 0;
295bd670b35SErik 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 		 */
325bd670b35SErik 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 */
349bd670b35SErik 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 		 */
358bd670b35SErik 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;
366d3d50737SRafael Vanoni 			stoptime = ddi_get_lbolt() +
367d3d50737SRafael Vanoni 			    connp->conn_lingertime * hz;
3687c478bd9Sstevel@tonic-gate 
3697c478bd9Sstevel@tonic-gate 			mutex_enter(&sctp->sctp_lock);
3707c478bd9Sstevel@tonic-gate 			sctp->sctp_running = B_FALSE;
3717c478bd9Sstevel@tonic-gate 			while (sctp->sctp_state >= SCTPS_ESTABLISHED &&
3727c478bd9Sstevel@tonic-gate 			    sctp->sctp_client_errno == 0) {
3737c478bd9Sstevel@tonic-gate 				cv_broadcast(&sctp->sctp_cv);
3747c478bd9Sstevel@tonic-gate 				ret = cv_timedwait_sig(&sctp->sctp_cv,
3757c478bd9Sstevel@tonic-gate 				    &sctp->sctp_lock, stoptime);
3767c478bd9Sstevel@tonic-gate 				if (ret < 0) {
3777c478bd9Sstevel@tonic-gate 					/* Stoptime has reached. */
3787c478bd9Sstevel@tonic-gate 					sctp->sctp_client_errno = EWOULDBLOCK;
3797c478bd9Sstevel@tonic-gate 					break;
3807c478bd9Sstevel@tonic-gate 				} else if (ret == 0) {
3817c478bd9Sstevel@tonic-gate 					/* Got a signal. */
3827c478bd9Sstevel@tonic-gate 					break;
3837c478bd9Sstevel@tonic-gate 				}
3847c478bd9Sstevel@tonic-gate 			}
3857c478bd9Sstevel@tonic-gate 			error = sctp->sctp_client_errno;
3867c478bd9Sstevel@tonic-gate 			sctp->sctp_client_errno = 0;
3877c478bd9Sstevel@tonic-gate 			mutex_exit(&sctp->sctp_lock);
3887c478bd9Sstevel@tonic-gate 		}
3897c478bd9Sstevel@tonic-gate 
3907c478bd9Sstevel@tonic-gate 		WAKE_SCTP(sctp);
3917c478bd9Sstevel@tonic-gate 		return (error);
3927c478bd9Sstevel@tonic-gate 	}
3937c478bd9Sstevel@tonic-gate 
3947c478bd9Sstevel@tonic-gate 
3957c478bd9Sstevel@tonic-gate 	/* Remove this sctp from all hashes so nobody can find it. */
3967c478bd9Sstevel@tonic-gate 	sctp_closei_local(sctp);
3977c478bd9Sstevel@tonic-gate 	WAKE_SCTP(sctp);
3987c478bd9Sstevel@tonic-gate 	return (error);
3997c478bd9Sstevel@tonic-gate }
4007c478bd9Sstevel@tonic-gate 
4017c478bd9Sstevel@tonic-gate void
4027c478bd9Sstevel@tonic-gate sctp_close(sctp_t *sctp)
4037c478bd9Sstevel@tonic-gate {
40445916cd2Sjpk 	dprint(3, ("sctp_close %p, state %d\n", (void *)sctp,
40545916cd2Sjpk 	    sctp->sctp_state));
4067c478bd9Sstevel@tonic-gate 
4077c478bd9Sstevel@tonic-gate 	RUN_SCTP(sctp);
4087c478bd9Sstevel@tonic-gate 	sctp->sctp_detached = 1;
4097c478bd9Sstevel@tonic-gate 	sctp->sctp_ulpd = NULL;
4100f1702c5SYu Xiangning 	sctp->sctp_upcalls = NULL;
4117c478bd9Sstevel@tonic-gate 	bzero(&sctp->sctp_events, sizeof (sctp->sctp_events));
4127c478bd9Sstevel@tonic-gate 
4137c478bd9Sstevel@tonic-gate 	/* If the graceful shutdown has not been completed, just return. */
4147c478bd9Sstevel@tonic-gate 	if (sctp->sctp_state != SCTPS_IDLE) {
4157c478bd9Sstevel@tonic-gate 		WAKE_SCTP(sctp);
4167c478bd9Sstevel@tonic-gate 		return;
4177c478bd9Sstevel@tonic-gate 	}
4187c478bd9Sstevel@tonic-gate 
4197c478bd9Sstevel@tonic-gate 	/*
4207c478bd9Sstevel@tonic-gate 	 * Since sctp_t is in SCTPS_IDLE state, so the only thread which
4217c478bd9Sstevel@tonic-gate 	 * can have a hold on the sctp_t is doing snmp stuff.  Just do
4227c478bd9Sstevel@tonic-gate 	 * a SCTP_REFRELE() here after the SCTP_UNLINK().  It will
4237c478bd9Sstevel@tonic-gate 	 * be freed when the other thread is done.
4247c478bd9Sstevel@tonic-gate 	 */
4257c478bd9Sstevel@tonic-gate 	SCTP_CONDEMNED(sctp);
4267c478bd9Sstevel@tonic-gate 	WAKE_SCTP(sctp);
4277c478bd9Sstevel@tonic-gate 	SCTP_REFRELE(sctp);
4287c478bd9Sstevel@tonic-gate }
4297c478bd9Sstevel@tonic-gate 
4307c478bd9Sstevel@tonic-gate /*
4317c478bd9Sstevel@tonic-gate  * Unlink from global list and do the eager close.
4327c478bd9Sstevel@tonic-gate  * Remove the refhold implicit in being on the global list.
4337c478bd9Sstevel@tonic-gate  */
4347c478bd9Sstevel@tonic-gate void
4357c478bd9Sstevel@tonic-gate sctp_close_eager(sctp_t *sctp)
4367c478bd9Sstevel@tonic-gate {
4377c478bd9Sstevel@tonic-gate 	SCTP_CONDEMNED(sctp);
4387c478bd9Sstevel@tonic-gate 	sctp_closei_local(sctp);
4397c478bd9Sstevel@tonic-gate 	SCTP_REFRELE(sctp);
4407c478bd9Sstevel@tonic-gate }
4417c478bd9Sstevel@tonic-gate 
4427c478bd9Sstevel@tonic-gate /*
4437c478bd9Sstevel@tonic-gate  * The sctp_t is going away. Remove it from all lists and set it
4447c478bd9Sstevel@tonic-gate  * to SCTPS_IDLE. The caller has to remove it from the
4457c478bd9Sstevel@tonic-gate  * global list. The freeing up of memory is deferred until
4467c478bd9Sstevel@tonic-gate  * sctp_free(). This is needed since a thread in sctp_input() might have
4477c478bd9Sstevel@tonic-gate  * done a SCTP_REFHOLD on this structure before it was removed from the
4487c478bd9Sstevel@tonic-gate  * hashes.
4497c478bd9Sstevel@tonic-gate  */
4507c478bd9Sstevel@tonic-gate static void
4517c478bd9Sstevel@tonic-gate sctp_closei_local(sctp_t *sctp)
4527c478bd9Sstevel@tonic-gate {
4537c478bd9Sstevel@tonic-gate 	mblk_t	*mp;
4547c478bd9Sstevel@tonic-gate 	conn_t	*connp = sctp->sctp_connp;
4557c478bd9Sstevel@tonic-gate 
456b34b8d1aSkcpoon 	/* Sanity check, don't do the same thing twice.  */
457b34b8d1aSkcpoon 	if (connp->conn_state_flags & CONN_CLOSING) {
458b34b8d1aSkcpoon 		ASSERT(sctp->sctp_state == SCTPS_IDLE);
459b34b8d1aSkcpoon 		return;
460b34b8d1aSkcpoon 	}
461b34b8d1aSkcpoon 
4627c478bd9Sstevel@tonic-gate 	/* Stop and free the timers */
4637c478bd9Sstevel@tonic-gate 	sctp_free_faddr_timers(sctp);
4647c478bd9Sstevel@tonic-gate 	if ((mp = sctp->sctp_heartbeat_mp) != NULL) {
4657c478bd9Sstevel@tonic-gate 		sctp_timer_free(mp);
4667c478bd9Sstevel@tonic-gate 		sctp->sctp_heartbeat_mp = NULL;
4677c478bd9Sstevel@tonic-gate 	}
4687c478bd9Sstevel@tonic-gate 	if ((mp = sctp->sctp_ack_mp) != NULL) {
4697c478bd9Sstevel@tonic-gate 		sctp_timer_free(mp);
4707c478bd9Sstevel@tonic-gate 		sctp->sctp_ack_mp = NULL;
4717c478bd9Sstevel@tonic-gate 	}
4727c478bd9Sstevel@tonic-gate 
4737c478bd9Sstevel@tonic-gate 	/* Set the CONN_CLOSING flag so that IP will not cache IRE again. */
4747c478bd9Sstevel@tonic-gate 	mutex_enter(&connp->conn_lock);
4757c478bd9Sstevel@tonic-gate 	connp->conn_state_flags |= CONN_CLOSING;
4767c478bd9Sstevel@tonic-gate 	mutex_exit(&connp->conn_lock);
4777c478bd9Sstevel@tonic-gate 
4787c478bd9Sstevel@tonic-gate 	/* Remove from all hashes. */
4797c478bd9Sstevel@tonic-gate 	sctp_bind_hash_remove(sctp);
4807c478bd9Sstevel@tonic-gate 	sctp_conn_hash_remove(sctp);
4817c478bd9Sstevel@tonic-gate 	sctp_listen_hash_remove(sctp);
4827c478bd9Sstevel@tonic-gate 	sctp->sctp_state = SCTPS_IDLE;
4837c478bd9Sstevel@tonic-gate 
4847c478bd9Sstevel@tonic-gate 	/*
4857c478bd9Sstevel@tonic-gate 	 * Clean up the recvq as much as possible.  All those packets
4867c478bd9Sstevel@tonic-gate 	 * will be silently dropped as this sctp_t is now in idle state.
4877c478bd9Sstevel@tonic-gate 	 */
4887c478bd9Sstevel@tonic-gate 	mutex_enter(&sctp->sctp_recvq_lock);
4897c478bd9Sstevel@tonic-gate 	while ((mp = sctp->sctp_recvq) != NULL) {
4907c478bd9Sstevel@tonic-gate 		sctp->sctp_recvq = mp->b_next;
4917c478bd9Sstevel@tonic-gate 		mp->b_next = NULL;
492bd670b35SErik Nordmark 
493bd670b35SErik Nordmark 		if (ip_recv_attr_is_mblk(mp))
494bd670b35SErik Nordmark 			mp = ip_recv_attr_free_mblk(mp);
495bd670b35SErik Nordmark 
4967c478bd9Sstevel@tonic-gate 		freemsg(mp);
4977c478bd9Sstevel@tonic-gate 	}
4987c478bd9Sstevel@tonic-gate 	mutex_exit(&sctp->sctp_recvq_lock);
4997c478bd9Sstevel@tonic-gate }
5007c478bd9Sstevel@tonic-gate 
5017c478bd9Sstevel@tonic-gate /*
5027c478bd9Sstevel@tonic-gate  * Free memory associated with the sctp/ip header template.
5037c478bd9Sstevel@tonic-gate  */
5047c478bd9Sstevel@tonic-gate static void
5057c478bd9Sstevel@tonic-gate sctp_headers_free(sctp_t *sctp)
5067c478bd9Sstevel@tonic-gate {
5077c478bd9Sstevel@tonic-gate 	if (sctp->sctp_iphc != NULL) {
5087c478bd9Sstevel@tonic-gate 		kmem_free(sctp->sctp_iphc, sctp->sctp_iphc_len);
5097c478bd9Sstevel@tonic-gate 		sctp->sctp_iphc = NULL;
5107c478bd9Sstevel@tonic-gate 		sctp->sctp_ipha = NULL;
5117c478bd9Sstevel@tonic-gate 		sctp->sctp_hdr_len = 0;
5127c478bd9Sstevel@tonic-gate 		sctp->sctp_ip_hdr_len = 0;
5137c478bd9Sstevel@tonic-gate 		sctp->sctp_iphc_len = 0;
5147c478bd9Sstevel@tonic-gate 		sctp->sctp_sctph = NULL;
5157c478bd9Sstevel@tonic-gate 		sctp->sctp_hdr_len = 0;
5167c478bd9Sstevel@tonic-gate 	}
5177c478bd9Sstevel@tonic-gate 	if (sctp->sctp_iphc6 != NULL) {
5187c478bd9Sstevel@tonic-gate 		kmem_free(sctp->sctp_iphc6, sctp->sctp_iphc6_len);
5197c478bd9Sstevel@tonic-gate 		sctp->sctp_iphc6 = NULL;
5207c478bd9Sstevel@tonic-gate 		sctp->sctp_ip6h = NULL;
5217c478bd9Sstevel@tonic-gate 		sctp->sctp_hdr6_len = 0;
5227c478bd9Sstevel@tonic-gate 		sctp->sctp_ip_hdr6_len = 0;
5237c478bd9Sstevel@tonic-gate 		sctp->sctp_iphc6_len = 0;
5247c478bd9Sstevel@tonic-gate 		sctp->sctp_sctph6 = NULL;
5257c478bd9Sstevel@tonic-gate 		sctp->sctp_hdr6_len = 0;
5267c478bd9Sstevel@tonic-gate 	}
5277c478bd9Sstevel@tonic-gate }
5287c478bd9Sstevel@tonic-gate 
5297c478bd9Sstevel@tonic-gate static void
5307c478bd9Sstevel@tonic-gate sctp_free_xmit_data(sctp_t *sctp)
5317c478bd9Sstevel@tonic-gate {
5327c478bd9Sstevel@tonic-gate 	mblk_t	*ump = NULL;
5337c478bd9Sstevel@tonic-gate 	mblk_t	*nump;
5347c478bd9Sstevel@tonic-gate 	mblk_t	*mp;
5357c478bd9Sstevel@tonic-gate 	mblk_t	*nmp;
5367c478bd9Sstevel@tonic-gate 
5377c478bd9Sstevel@tonic-gate 	sctp->sctp_xmit_unacked = NULL;
5387c478bd9Sstevel@tonic-gate 	ump = sctp->sctp_xmit_head;
5397c478bd9Sstevel@tonic-gate 	sctp->sctp_xmit_tail = sctp->sctp_xmit_head = NULL;
5407c478bd9Sstevel@tonic-gate free_unsent:
5417c478bd9Sstevel@tonic-gate 	for (; ump != NULL; ump = nump) {
5427c478bd9Sstevel@tonic-gate 		for (mp = ump->b_cont; mp != NULL; mp = nmp) {
5437c478bd9Sstevel@tonic-gate 			nmp = mp->b_next;
5447c478bd9Sstevel@tonic-gate 			mp->b_next = NULL;
5457c478bd9Sstevel@tonic-gate 			mp->b_prev = NULL;
5467c478bd9Sstevel@tonic-gate 			freemsg(mp);
5477c478bd9Sstevel@tonic-gate 		}
5487c478bd9Sstevel@tonic-gate 		ASSERT(DB_REF(ump) == 1);
5497c478bd9Sstevel@tonic-gate 		nump = ump->b_next;
5507c478bd9Sstevel@tonic-gate 		ump->b_next = NULL;
5517c478bd9Sstevel@tonic-gate 		ump->b_prev = NULL;
5527c478bd9Sstevel@tonic-gate 		ump->b_cont = NULL;
5537c478bd9Sstevel@tonic-gate 		freeb(ump);
5547c478bd9Sstevel@tonic-gate 	}
5557c478bd9Sstevel@tonic-gate 	if ((ump = sctp->sctp_xmit_unsent) == NULL) {
5567c478bd9Sstevel@tonic-gate 		ASSERT(sctp->sctp_xmit_unsent_tail == NULL);
5577c478bd9Sstevel@tonic-gate 		return;
5587c478bd9Sstevel@tonic-gate 	}
5597c478bd9Sstevel@tonic-gate 	sctp->sctp_xmit_unsent = sctp->sctp_xmit_unsent_tail = NULL;
5607c478bd9Sstevel@tonic-gate 	goto free_unsent;
5617c478bd9Sstevel@tonic-gate }
5627c478bd9Sstevel@tonic-gate 
5637c478bd9Sstevel@tonic-gate /*
5647c478bd9Sstevel@tonic-gate  * Cleanup all the messages in the stream queue and the reassembly lists.
5657c478bd9Sstevel@tonic-gate  * If 'free' is true, then delete the streams as well.
5667c478bd9Sstevel@tonic-gate  */
5677c478bd9Sstevel@tonic-gate void
5687c478bd9Sstevel@tonic-gate sctp_instream_cleanup(sctp_t *sctp, boolean_t free)
5697c478bd9Sstevel@tonic-gate {
5707c478bd9Sstevel@tonic-gate 	int	i;
5717c478bd9Sstevel@tonic-gate 	mblk_t	*mp;
5727c478bd9Sstevel@tonic-gate 	mblk_t	*mp1;
5737c478bd9Sstevel@tonic-gate 
5747c478bd9Sstevel@tonic-gate 	if (sctp->sctp_instr != NULL) {
5757c478bd9Sstevel@tonic-gate 		/* walk thru and flush out anything remaining in the Q */
5767c478bd9Sstevel@tonic-gate 		for (i = 0; i < sctp->sctp_num_istr; i++) {
5777c478bd9Sstevel@tonic-gate 			mp = sctp->sctp_instr[i].istr_msgs;
5787c478bd9Sstevel@tonic-gate 			while (mp != NULL) {
5797c478bd9Sstevel@tonic-gate 				mp1 = mp->b_next;
5807c478bd9Sstevel@tonic-gate 				mp->b_next = mp->b_prev = NULL;
5817c478bd9Sstevel@tonic-gate 				freemsg(mp);
5827c478bd9Sstevel@tonic-gate 				mp = mp1;
5837c478bd9Sstevel@tonic-gate 			}
5847c478bd9Sstevel@tonic-gate 			sctp->sctp_instr[i].istr_msgs = NULL;
585a0263d1cSnh 			sctp->sctp_instr[i].istr_nmsgs = 0;
5867c478bd9Sstevel@tonic-gate 			sctp_free_reass((sctp->sctp_instr) + i);
5877c478bd9Sstevel@tonic-gate 			sctp->sctp_instr[i].nextseq = 0;
5887c478bd9Sstevel@tonic-gate 		}
5897c478bd9Sstevel@tonic-gate 		if (free) {
5907c478bd9Sstevel@tonic-gate 			kmem_free(sctp->sctp_instr,
5917c478bd9Sstevel@tonic-gate 			    sizeof (*sctp->sctp_instr) * sctp->sctp_num_istr);
5927c478bd9Sstevel@tonic-gate 			sctp->sctp_instr = NULL;
5937c478bd9Sstevel@tonic-gate 			sctp->sctp_num_istr = 0;
5947c478bd9Sstevel@tonic-gate 		}
5957c478bd9Sstevel@tonic-gate 	}
5967c478bd9Sstevel@tonic-gate 	/* un-ordered fragments */
5977c478bd9Sstevel@tonic-gate 	if (sctp->sctp_uo_frags != NULL) {
5987c478bd9Sstevel@tonic-gate 		for (mp = sctp->sctp_uo_frags; mp != NULL; mp = mp1) {
5997c478bd9Sstevel@tonic-gate 			mp1 = mp->b_next;
6007c478bd9Sstevel@tonic-gate 			mp->b_next = mp->b_prev = NULL;
6017c478bd9Sstevel@tonic-gate 			freemsg(mp);
6027c478bd9Sstevel@tonic-gate 		}
603*92baa190SGeorge Shepherd 		sctp->sctp_uo_frags = NULL;
6047c478bd9Sstevel@tonic-gate 	}
6057c478bd9Sstevel@tonic-gate }
6067c478bd9Sstevel@tonic-gate 
6077c478bd9Sstevel@tonic-gate /*
6087c478bd9Sstevel@tonic-gate  * Last reference to the sctp_t is gone. Free all memory associated with it.
6097c478bd9Sstevel@tonic-gate  * Called from SCTP_REFRELE. Called inline in sctp_close()
6107c478bd9Sstevel@tonic-gate  */
6117c478bd9Sstevel@tonic-gate void
6127c478bd9Sstevel@tonic-gate sctp_free(conn_t *connp)
6137c478bd9Sstevel@tonic-gate {
6147c478bd9Sstevel@tonic-gate 	sctp_t *sctp = CONN2SCTP(connp);
6157c478bd9Sstevel@tonic-gate 	int		cnt;
616f4b3ec61Sdh 	sctp_stack_t	*sctps = sctp->sctp_sctps;
6177c478bd9Sstevel@tonic-gate 
618f4b3ec61Sdh 	ASSERT(sctps != NULL);
6197c478bd9Sstevel@tonic-gate 	/* Unlink it from the global list */
620f4b3ec61Sdh 	SCTP_UNLINK(sctp, sctps);
6217c478bd9Sstevel@tonic-gate 
6227c478bd9Sstevel@tonic-gate 	ASSERT(connp->conn_ref == 0);
623bd670b35SErik Nordmark 	ASSERT(connp->conn_proto == IPPROTO_SCTP);
6247c478bd9Sstevel@tonic-gate 	ASSERT(!MUTEX_HELD(&sctp->sctp_reflock));
6257c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_refcnt == 0);
6267c478bd9Sstevel@tonic-gate 
6277c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_ptpbhn == NULL && sctp->sctp_bind_hash == NULL);
6287c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_conn_hash_next == NULL &&
6297c478bd9Sstevel@tonic-gate 	    sctp->sctp_conn_hash_prev == NULL);
6307c478bd9Sstevel@tonic-gate 
6317c478bd9Sstevel@tonic-gate 
6327c478bd9Sstevel@tonic-gate 	/* Free up all the resources. */
6337c478bd9Sstevel@tonic-gate 
6347c478bd9Sstevel@tonic-gate 	/* blow away sctp stream management */
6357c478bd9Sstevel@tonic-gate 	if (sctp->sctp_ostrcntrs != NULL) {
6367c478bd9Sstevel@tonic-gate 		kmem_free(sctp->sctp_ostrcntrs,
6377c478bd9Sstevel@tonic-gate 		    sizeof (uint16_t) * sctp->sctp_num_ostr);
6387c478bd9Sstevel@tonic-gate 		sctp->sctp_ostrcntrs = NULL;
6397c478bd9Sstevel@tonic-gate 	}
6407c478bd9Sstevel@tonic-gate 	sctp_instream_cleanup(sctp, B_TRUE);
6417c478bd9Sstevel@tonic-gate 
6427c478bd9Sstevel@tonic-gate 	/* Remove all data transfer resources. */
6437c478bd9Sstevel@tonic-gate 	sctp->sctp_istr_nmsgs = 0;
6447c478bd9Sstevel@tonic-gate 	sctp->sctp_rxqueued = 0;
6457c478bd9Sstevel@tonic-gate 	sctp_free_xmit_data(sctp);
6467c478bd9Sstevel@tonic-gate 	sctp->sctp_unacked = 0;
6477c478bd9Sstevel@tonic-gate 	sctp->sctp_unsent = 0;
6481d8c4025Svi 	if (sctp->sctp_cxmit_list != NULL)
6491d8c4025Svi 		sctp_asconf_free_cxmit(sctp, NULL);
6501d8c4025Svi 
6517c478bd9Sstevel@tonic-gate 	sctp->sctp_lastdata = NULL;
6527c478bd9Sstevel@tonic-gate 
6537c478bd9Sstevel@tonic-gate 	/* Clear out default xmit settings */
6547c478bd9Sstevel@tonic-gate 	sctp->sctp_def_stream = 0;
6557c478bd9Sstevel@tonic-gate 	sctp->sctp_def_flags = 0;
6567c478bd9Sstevel@tonic-gate 	sctp->sctp_def_ppid = 0;
6577c478bd9Sstevel@tonic-gate 	sctp->sctp_def_context = 0;
6587c478bd9Sstevel@tonic-gate 	sctp->sctp_def_timetolive = 0;
6597c478bd9Sstevel@tonic-gate 
6607c478bd9Sstevel@tonic-gate 	if (sctp->sctp_sack_info != NULL) {
6617c478bd9Sstevel@tonic-gate 		sctp_free_set(sctp->sctp_sack_info);
6627c478bd9Sstevel@tonic-gate 		sctp->sctp_sack_info = NULL;
6637c478bd9Sstevel@tonic-gate 	}
6647c478bd9Sstevel@tonic-gate 	sctp->sctp_sack_gaps = 0;
6657c478bd9Sstevel@tonic-gate 
6667c478bd9Sstevel@tonic-gate 	if (sctp->sctp_cookie_mp != NULL) {
6677c478bd9Sstevel@tonic-gate 		freemsg(sctp->sctp_cookie_mp);
6687c478bd9Sstevel@tonic-gate 		sctp->sctp_cookie_mp = NULL;
6697c478bd9Sstevel@tonic-gate 	}
6707c478bd9Sstevel@tonic-gate 
6717c478bd9Sstevel@tonic-gate 	/* Remove all the address resources. */
6727c478bd9Sstevel@tonic-gate 	sctp_zap_addrs(sctp);
6737c478bd9Sstevel@tonic-gate 	for (cnt = 0; cnt < SCTP_IPIF_HASH; cnt++) {
6747c478bd9Sstevel@tonic-gate 		ASSERT(sctp->sctp_saddrs[cnt].ipif_count == 0);
6757c478bd9Sstevel@tonic-gate 		list_destroy(&sctp->sctp_saddrs[cnt].sctp_ipif_list);
6767c478bd9Sstevel@tonic-gate 	}
6777c478bd9Sstevel@tonic-gate 
6787c478bd9Sstevel@tonic-gate 	if (sctp->sctp_hopopts != NULL) {
6797c478bd9Sstevel@tonic-gate 		mi_free(sctp->sctp_hopopts);
6807c478bd9Sstevel@tonic-gate 		sctp->sctp_hopopts = NULL;
6817c478bd9Sstevel@tonic-gate 		sctp->sctp_hopoptslen = 0;
6827c478bd9Sstevel@tonic-gate 	}
6837c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_hopoptslen == 0);
6847c478bd9Sstevel@tonic-gate 	if (sctp->sctp_dstopts != NULL) {
6857c478bd9Sstevel@tonic-gate 		mi_free(sctp->sctp_dstopts);
6867c478bd9Sstevel@tonic-gate 		sctp->sctp_dstopts = NULL;
6877c478bd9Sstevel@tonic-gate 		sctp->sctp_dstoptslen = 0;
6887c478bd9Sstevel@tonic-gate 	}
6897c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_dstoptslen == 0);
690bd670b35SErik Nordmark 	if (sctp->sctp_rthdrdstopts != NULL) {
691bd670b35SErik Nordmark 		mi_free(sctp->sctp_rthdrdstopts);
692bd670b35SErik Nordmark 		sctp->sctp_rthdrdstopts = NULL;
693bd670b35SErik Nordmark 		sctp->sctp_rthdrdstoptslen = 0;
6947c478bd9Sstevel@tonic-gate 	}
695bd670b35SErik Nordmark 	ASSERT(sctp->sctp_rthdrdstoptslen == 0);
6967c478bd9Sstevel@tonic-gate 	if (sctp->sctp_rthdr != NULL) {
6977c478bd9Sstevel@tonic-gate 		mi_free(sctp->sctp_rthdr);
6987c478bd9Sstevel@tonic-gate 		sctp->sctp_rthdr = NULL;
6997c478bd9Sstevel@tonic-gate 		sctp->sctp_rthdrlen = 0;
7007c478bd9Sstevel@tonic-gate 	}
7017c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_rthdrlen == 0);
7027c478bd9Sstevel@tonic-gate 	sctp_headers_free(sctp);
7037c478bd9Sstevel@tonic-gate 
7047c478bd9Sstevel@tonic-gate 	sctp->sctp_shutdown_faddr = NULL;
7057c478bd9Sstevel@tonic-gate 
7067f093707Skcpoon 	if (sctp->sctp_err_chunks != NULL) {
7077f093707Skcpoon 		freemsg(sctp->sctp_err_chunks);
7087f093707Skcpoon 		sctp->sctp_err_chunks = NULL;
7097f093707Skcpoon 		sctp->sctp_err_len = 0;
7107f093707Skcpoon 	}
7117f093707Skcpoon 
7127c478bd9Sstevel@tonic-gate 	/* Clear all the bitfields. */
7137c478bd9Sstevel@tonic-gate 	bzero(&sctp->sctp_bits, sizeof (sctp->sctp_bits));
7147c478bd9Sstevel@tonic-gate 
7157c478bd9Sstevel@tonic-gate 	/* It is time to update the global statistics. */
716f4b3ec61Sdh 	UPDATE_MIB(&sctps->sctps_mib, sctpOutSCTPPkts, sctp->sctp_opkts);
717f4b3ec61Sdh 	UPDATE_MIB(&sctps->sctps_mib, sctpOutCtrlChunks, sctp->sctp_obchunks);
718f4b3ec61Sdh 	UPDATE_MIB(&sctps->sctps_mib, sctpOutOrderChunks, sctp->sctp_odchunks);
719f4b3ec61Sdh 	UPDATE_MIB(&sctps->sctps_mib,
720f4b3ec61Sdh 	    sctpOutUnorderChunks, sctp->sctp_oudchunks);
721f4b3ec61Sdh 	UPDATE_MIB(&sctps->sctps_mib, sctpRetransChunks, sctp->sctp_rxtchunks);
722f4b3ec61Sdh 	UPDATE_MIB(&sctps->sctps_mib, sctpInSCTPPkts, sctp->sctp_ipkts);
723f4b3ec61Sdh 	UPDATE_MIB(&sctps->sctps_mib, sctpInCtrlChunks, sctp->sctp_ibchunks);
724f4b3ec61Sdh 	UPDATE_MIB(&sctps->sctps_mib, sctpInOrderChunks, sctp->sctp_idchunks);
725f4b3ec61Sdh 	UPDATE_MIB(&sctps->sctps_mib,
726f4b3ec61Sdh 	    sctpInUnorderChunks, sctp->sctp_iudchunks);
727f4b3ec61Sdh 	UPDATE_MIB(&sctps->sctps_mib, sctpFragUsrMsgs, sctp->sctp_fragdmsgs);
728f4b3ec61Sdh 	UPDATE_MIB(&sctps->sctps_mib, sctpReasmUsrMsgs, sctp->sctp_reassmsgs);
7297c478bd9Sstevel@tonic-gate 	sctp->sctp_opkts = 0;
7307c478bd9Sstevel@tonic-gate 	sctp->sctp_obchunks = 0;
7317c478bd9Sstevel@tonic-gate 	sctp->sctp_odchunks = 0;
7327c478bd9Sstevel@tonic-gate 	sctp->sctp_oudchunks = 0;
7337c478bd9Sstevel@tonic-gate 	sctp->sctp_rxtchunks = 0;
7347c478bd9Sstevel@tonic-gate 	sctp->sctp_ipkts = 0;
7357c478bd9Sstevel@tonic-gate 	sctp->sctp_ibchunks = 0;
7367c478bd9Sstevel@tonic-gate 	sctp->sctp_idchunks = 0;
7377c478bd9Sstevel@tonic-gate 	sctp->sctp_iudchunks = 0;
7387c478bd9Sstevel@tonic-gate 	sctp->sctp_fragdmsgs = 0;
7397c478bd9Sstevel@tonic-gate 	sctp->sctp_reassmsgs = 0;
74035e12f9cSGeorge Shepherd 	sctp->sctp_outseqtsns = 0;
74135e12f9cSGeorge Shepherd 	sctp->sctp_osacks = 0;
74235e12f9cSGeorge Shepherd 	sctp->sctp_isacks = 0;
74335e12f9cSGeorge Shepherd 	sctp->sctp_idupchunks = 0;
74435e12f9cSGeorge Shepherd 	sctp->sctp_gapcnt = 0;
74535e12f9cSGeorge Shepherd 	sctp->sctp_cum_obchunks = 0;
74635e12f9cSGeorge Shepherd 	sctp->sctp_cum_odchunks = 0;
74735e12f9cSGeorge Shepherd 	sctp->sctp_cum_oudchunks = 0;
74835e12f9cSGeorge Shepherd 	sctp->sctp_cum_rxtchunks = 0;
74935e12f9cSGeorge Shepherd 	sctp->sctp_cum_ibchunks = 0;
75035e12f9cSGeorge Shepherd 	sctp->sctp_cum_idchunks = 0;
75135e12f9cSGeorge Shepherd 	sctp->sctp_cum_iudchunks = 0;
7527c478bd9Sstevel@tonic-gate 
7537c478bd9Sstevel@tonic-gate 	sctp->sctp_autoclose = 0;
754558fbd03Skcpoon 	sctp->sctp_tx_adaptation_code = 0;
7557c478bd9Sstevel@tonic-gate 
7567f99e6fcSkp 	sctp->sctp_v6label_len = 0;
7577f99e6fcSkp 	sctp->sctp_v4label_len = 0;
7587f99e6fcSkp 
759f4b3ec61Sdh 	sctp->sctp_sctps = NULL;
760f4b3ec61Sdh 
761121e5416Skcpoon 	sctp_conn_clear(connp);
7627c478bd9Sstevel@tonic-gate 	kmem_cache_free(sctp_conn_cache, connp);
7637c478bd9Sstevel@tonic-gate }
7647c478bd9Sstevel@tonic-gate 
7657c478bd9Sstevel@tonic-gate /* Diagnostic routine used to return a string associated with the sctp state. */
7667c478bd9Sstevel@tonic-gate char *
7677c478bd9Sstevel@tonic-gate sctp_display(sctp_t *sctp, char *sup_buf)
7687c478bd9Sstevel@tonic-gate {
7697c478bd9Sstevel@tonic-gate 	char	*buf;
7707c478bd9Sstevel@tonic-gate 	char	buf1[30];
7717c478bd9Sstevel@tonic-gate 	static char	priv_buf[INET6_ADDRSTRLEN * 2 + 80];
7727c478bd9Sstevel@tonic-gate 	char	*cp;
773bd670b35SErik Nordmark 	conn_t	*connp;
7747c478bd9Sstevel@tonic-gate 
7757c478bd9Sstevel@tonic-gate 	if (sctp == NULL)
7767c478bd9Sstevel@tonic-gate 		return ("NULL_SCTP");
7777c478bd9Sstevel@tonic-gate 
778bd670b35SErik Nordmark 	connp = sctp->sctp_connp;
7797c478bd9Sstevel@tonic-gate 	buf = (sup_buf != NULL) ? sup_buf : priv_buf;
7807c478bd9Sstevel@tonic-gate 
7817c478bd9Sstevel@tonic-gate 	switch (sctp->sctp_state) {
7827c478bd9Sstevel@tonic-gate 	case SCTPS_IDLE:
7837c478bd9Sstevel@tonic-gate 		cp = "SCTP_IDLE";
7847c478bd9Sstevel@tonic-gate 		break;
7857c478bd9Sstevel@tonic-gate 	case SCTPS_BOUND:
7867c478bd9Sstevel@tonic-gate 		cp = "SCTP_BOUND";
7877c478bd9Sstevel@tonic-gate 		break;
7887c478bd9Sstevel@tonic-gate 	case SCTPS_LISTEN:
7897c478bd9Sstevel@tonic-gate 		cp = "SCTP_LISTEN";
7907c478bd9Sstevel@tonic-gate 		break;
7917c478bd9Sstevel@tonic-gate 	case SCTPS_COOKIE_WAIT:
7927c478bd9Sstevel@tonic-gate 		cp = "SCTP_COOKIE_WAIT";
7937c478bd9Sstevel@tonic-gate 		break;
7947c478bd9Sstevel@tonic-gate 	case SCTPS_COOKIE_ECHOED:
7957c478bd9Sstevel@tonic-gate 		cp = "SCTP_COOKIE_ECHOED";
7967c478bd9Sstevel@tonic-gate 		break;
7977c478bd9Sstevel@tonic-gate 	case SCTPS_ESTABLISHED:
7987c478bd9Sstevel@tonic-gate 		cp = "SCTP_ESTABLISHED";
7997c478bd9Sstevel@tonic-gate 		break;
8007c478bd9Sstevel@tonic-gate 	case SCTPS_SHUTDOWN_PENDING:
8017c478bd9Sstevel@tonic-gate 		cp = "SCTP_SHUTDOWN_PENDING";
8027c478bd9Sstevel@tonic-gate 		break;
8037c478bd9Sstevel@tonic-gate 	case SCTPS_SHUTDOWN_SENT:
8047c478bd9Sstevel@tonic-gate 		cp = "SCTPS_SHUTDOWN_SENT";
8057c478bd9Sstevel@tonic-gate 		break;
8067c478bd9Sstevel@tonic-gate 	case SCTPS_SHUTDOWN_RECEIVED:
8077c478bd9Sstevel@tonic-gate 		cp = "SCTPS_SHUTDOWN_RECEIVED";
8087c478bd9Sstevel@tonic-gate 		break;
8097c478bd9Sstevel@tonic-gate 	case SCTPS_SHUTDOWN_ACK_SENT:
8107c478bd9Sstevel@tonic-gate 		cp = "SCTPS_SHUTDOWN_ACK_SENT";
8117c478bd9Sstevel@tonic-gate 		break;
8127c478bd9Sstevel@tonic-gate 	default:
8137c478bd9Sstevel@tonic-gate 		(void) mi_sprintf(buf1, "SCTPUnkState(%d)", sctp->sctp_state);
8147c478bd9Sstevel@tonic-gate 		cp = buf1;
8157c478bd9Sstevel@tonic-gate 		break;
8167c478bd9Sstevel@tonic-gate 	}
8177c478bd9Sstevel@tonic-gate 	(void) mi_sprintf(buf, "[%u, %u] %s",
818bd670b35SErik Nordmark 	    ntohs(connp->conn_lport), ntohs(connp->conn_fport), cp);
8197c478bd9Sstevel@tonic-gate 
8207c478bd9Sstevel@tonic-gate 	return (buf);
8217c478bd9Sstevel@tonic-gate }
8227c478bd9Sstevel@tonic-gate 
8237c478bd9Sstevel@tonic-gate /*
8247c478bd9Sstevel@tonic-gate  * Initialize protocol control block. If a parent exists, inherit
8257c478bd9Sstevel@tonic-gate  * all values set through setsockopt().
8267c478bd9Sstevel@tonic-gate  */
8277c478bd9Sstevel@tonic-gate static int
8287c478bd9Sstevel@tonic-gate sctp_init_values(sctp_t *sctp, sctp_t *psctp, int sleep)
8297c478bd9Sstevel@tonic-gate {
8307c478bd9Sstevel@tonic-gate 	int	err;
8317c478bd9Sstevel@tonic-gate 	int	cnt;
832f4b3ec61Sdh 	sctp_stack_t	*sctps = sctp->sctp_sctps;
833bd670b35SErik Nordmark 	conn_t 	*connp;
8347c478bd9Sstevel@tonic-gate 
835bd670b35SErik Nordmark 	connp = sctp->sctp_connp;
8367c478bd9Sstevel@tonic-gate 
8377c478bd9Sstevel@tonic-gate 	sctp->sctp_nsaddrs = 0;
8387c478bd9Sstevel@tonic-gate 	for (cnt = 0; cnt < SCTP_IPIF_HASH; cnt++) {
8397c478bd9Sstevel@tonic-gate 		sctp->sctp_saddrs[cnt].ipif_count = 0;
8407c478bd9Sstevel@tonic-gate 		list_create(&sctp->sctp_saddrs[cnt].sctp_ipif_list,
8417c478bd9Sstevel@tonic-gate 		    sizeof (sctp_saddr_ipif_t), offsetof(sctp_saddr_ipif_t,
8427c478bd9Sstevel@tonic-gate 		    saddr_ipif));
8437c478bd9Sstevel@tonic-gate 	}
844bd670b35SErik Nordmark 	connp->conn_ports = 0;
8457c478bd9Sstevel@tonic-gate 	sctp->sctp_running = B_FALSE;
8467c478bd9Sstevel@tonic-gate 	sctp->sctp_state = SCTPS_IDLE;
8477c478bd9Sstevel@tonic-gate 
8487c478bd9Sstevel@tonic-gate 	sctp->sctp_refcnt = 1;
8497c478bd9Sstevel@tonic-gate 
8507c478bd9Sstevel@tonic-gate 	sctp->sctp_strikes = 0;
8517c478bd9Sstevel@tonic-gate 
852d3d50737SRafael Vanoni 	sctp->sctp_last_mtu_probe = ddi_get_lbolt64();
853f4b3ec61Sdh 	sctp->sctp_mtu_probe_intvl = sctps->sctps_mtu_probe_interval;
8547c478bd9Sstevel@tonic-gate 
8557c478bd9Sstevel@tonic-gate 	sctp->sctp_sack_gaps = 0;
8567c478bd9Sstevel@tonic-gate 	sctp->sctp_sack_toggle = 2;
8577c478bd9Sstevel@tonic-gate 
858121e5416Skcpoon 	/* Only need to do the allocation if there is no "cached" one. */
859121e5416Skcpoon 	if (sctp->sctp_pad_mp == NULL) {
860121e5416Skcpoon 		if (sleep == KM_SLEEP) {
861121e5416Skcpoon 			sctp->sctp_pad_mp = allocb_wait(SCTP_ALIGN, BPRI_MED,
862121e5416Skcpoon 			    STR_NOSIG, NULL);
863121e5416Skcpoon 		} else {
864121e5416Skcpoon 			sctp->sctp_pad_mp = allocb(SCTP_ALIGN, BPRI_MED);
865121e5416Skcpoon 			if (sctp->sctp_pad_mp == NULL)
866121e5416Skcpoon 				return (ENOMEM);
867121e5416Skcpoon 		}
868121e5416Skcpoon 		bzero(sctp->sctp_pad_mp->b_rptr, SCTP_ALIGN);
869121e5416Skcpoon 	}
870121e5416Skcpoon 
8717c478bd9Sstevel@tonic-gate 	if (psctp != NULL) {
8727c478bd9Sstevel@tonic-gate 		/*
8737c478bd9Sstevel@tonic-gate 		 * Inherit from parent
874bd670b35SErik Nordmark 		 *
875bd670b35SErik Nordmark 		 * Start by inheriting from the conn_t, including conn_ixa and
876bd670b35SErik Nordmark 		 * conn_xmit_ipp.
8777c478bd9Sstevel@tonic-gate 		 */
878bd670b35SErik Nordmark 		err = conn_inherit_parent(psctp->sctp_connp, connp);
879bd670b35SErik Nordmark 		if (err != 0)
880121e5416Skcpoon 			goto failure;
8817c478bd9Sstevel@tonic-gate 
8827c478bd9Sstevel@tonic-gate 		sctp->sctp_cookie_lifetime = psctp->sctp_cookie_lifetime;
883bd670b35SErik Nordmark 
8847c478bd9Sstevel@tonic-gate 		sctp->sctp_cwnd_max = psctp->sctp_cwnd_max;
8857c478bd9Sstevel@tonic-gate 		sctp->sctp_rwnd = psctp->sctp_rwnd;
8861d8c4025Svi 		sctp->sctp_irwnd = psctp->sctp_rwnd;
8877d546a59Svi 		sctp->sctp_pd_point = psctp->sctp_pd_point;
8887c478bd9Sstevel@tonic-gate 		sctp->sctp_rto_max = psctp->sctp_rto_max;
8897c478bd9Sstevel@tonic-gate 		sctp->sctp_init_rto_max = psctp->sctp_init_rto_max;
8907c478bd9Sstevel@tonic-gate 		sctp->sctp_rto_min = psctp->sctp_rto_min;
8917c478bd9Sstevel@tonic-gate 		sctp->sctp_rto_initial = psctp->sctp_rto_initial;
8927c478bd9Sstevel@tonic-gate 		sctp->sctp_pa_max_rxt = psctp->sctp_pa_max_rxt;
8937c478bd9Sstevel@tonic-gate 		sctp->sctp_pp_max_rxt = psctp->sctp_pp_max_rxt;
8947c478bd9Sstevel@tonic-gate 		sctp->sctp_max_init_rxt = psctp->sctp_max_init_rxt;
8957c478bd9Sstevel@tonic-gate 
8967c478bd9Sstevel@tonic-gate 		sctp->sctp_def_stream = psctp->sctp_def_stream;
8977c478bd9Sstevel@tonic-gate 		sctp->sctp_def_flags = psctp->sctp_def_flags;
8987c478bd9Sstevel@tonic-gate 		sctp->sctp_def_ppid = psctp->sctp_def_ppid;
8997c478bd9Sstevel@tonic-gate 		sctp->sctp_def_context = psctp->sctp_def_context;
9007c478bd9Sstevel@tonic-gate 		sctp->sctp_def_timetolive = psctp->sctp_def_timetolive;
9017c478bd9Sstevel@tonic-gate 
9027c478bd9Sstevel@tonic-gate 		sctp->sctp_num_istr = psctp->sctp_num_istr;
9037c478bd9Sstevel@tonic-gate 		sctp->sctp_num_ostr = psctp->sctp_num_ostr;
9047c478bd9Sstevel@tonic-gate 
9057c478bd9Sstevel@tonic-gate 		sctp->sctp_hb_interval = psctp->sctp_hb_interval;
9067c478bd9Sstevel@tonic-gate 		sctp->sctp_autoclose = psctp->sctp_autoclose;
907558fbd03Skcpoon 		sctp->sctp_tx_adaptation_code = psctp->sctp_tx_adaptation_code;
9087c478bd9Sstevel@tonic-gate 
9097c478bd9Sstevel@tonic-gate 		/* xxx should be a better way to copy these flags xxx */
9107c478bd9Sstevel@tonic-gate 		sctp->sctp_bound_to_all = psctp->sctp_bound_to_all;
9117c478bd9Sstevel@tonic-gate 		sctp->sctp_cansleep = psctp->sctp_cansleep;
912558fbd03Skcpoon 		sctp->sctp_send_adaptation = psctp->sctp_send_adaptation;
9137c478bd9Sstevel@tonic-gate 		sctp->sctp_ndelay = psctp->sctp_ndelay;
9147c478bd9Sstevel@tonic-gate 		sctp->sctp_events = psctp->sctp_events;
9157c478bd9Sstevel@tonic-gate 	} else {
9167c478bd9Sstevel@tonic-gate 		/*
9177c478bd9Sstevel@tonic-gate 		 * Set to system defaults
9187c478bd9Sstevel@tonic-gate 		 */
919f4b3ec61Sdh 		sctp->sctp_cookie_lifetime =
920f4b3ec61Sdh 		    MSEC_TO_TICK(sctps->sctps_cookie_life);
921bd670b35SErik Nordmark 		connp->conn_sndlowat = sctps->sctps_xmit_lowat;
922bd670b35SErik Nordmark 		connp->conn_sndbuf = sctps->sctps_xmit_hiwat;
923bd670b35SErik Nordmark 		connp->conn_rcvbuf = sctps->sctps_recv_hiwat;
924bd670b35SErik Nordmark 
925f4b3ec61Sdh 		sctp->sctp_cwnd_max = sctps->sctps_cwnd_max_;
926bd670b35SErik Nordmark 		sctp->sctp_rwnd = connp->conn_rcvbuf;
9271d8c4025Svi 		sctp->sctp_irwnd = sctp->sctp_rwnd;
9287d546a59Svi 		sctp->sctp_pd_point = sctp->sctp_rwnd;
929f4b3ec61Sdh 		sctp->sctp_rto_max = MSEC_TO_TICK(sctps->sctps_rto_maxg);
9307c478bd9Sstevel@tonic-gate 		sctp->sctp_init_rto_max = sctp->sctp_rto_max;
931f4b3ec61Sdh 		sctp->sctp_rto_min = MSEC_TO_TICK(sctps->sctps_rto_ming);
932f4b3ec61Sdh 		sctp->sctp_rto_initial = MSEC_TO_TICK(
933f4b3ec61Sdh 		    sctps->sctps_rto_initialg);
934f4b3ec61Sdh 		sctp->sctp_pa_max_rxt = sctps->sctps_pa_max_retr;
935f4b3ec61Sdh 		sctp->sctp_pp_max_rxt = sctps->sctps_pp_max_retr;
936f4b3ec61Sdh 		sctp->sctp_max_init_rxt = sctps->sctps_max_init_retr;
937f4b3ec61Sdh 
938f4b3ec61Sdh 		sctp->sctp_num_istr = sctps->sctps_max_in_streams;
939f4b3ec61Sdh 		sctp->sctp_num_ostr = sctps->sctps_initial_out_streams;
940f4b3ec61Sdh 
941f4b3ec61Sdh 		sctp->sctp_hb_interval =
942f4b3ec61Sdh 		    MSEC_TO_TICK(sctps->sctps_heartbeat_interval);
943bd670b35SErik Nordmark 
944bd670b35SErik Nordmark 		if (connp->conn_family == AF_INET)
945bd670b35SErik Nordmark 			connp->conn_default_ttl = sctps->sctps_ipv4_ttl;
946bd670b35SErik Nordmark 		else
947bd670b35SErik Nordmark 			connp->conn_default_ttl = sctps->sctps_ipv6_hoplimit;
948bd670b35SErik Nordmark 
949bd670b35SErik Nordmark 		connp->conn_xmit_ipp.ipp_unicast_hops =
950bd670b35SErik Nordmark 		    connp->conn_default_ttl;
951bd670b35SErik Nordmark 
952bd670b35SErik Nordmark 		/*
953bd670b35SErik Nordmark 		 * Initialize the header template
954bd670b35SErik Nordmark 		 */
955bd670b35SErik Nordmark 		if ((err = sctp_build_hdrs(sctp, sleep)) != 0) {
956bd670b35SErik Nordmark 			goto failure;
957bd670b35SErik Nordmark 		}
9587c478bd9Sstevel@tonic-gate 	}
959bd670b35SErik Nordmark 
9607c478bd9Sstevel@tonic-gate 	sctp->sctp_understands_asconf = B_TRUE;
9617c478bd9Sstevel@tonic-gate 	sctp->sctp_understands_addip = B_TRUE;
9627c478bd9Sstevel@tonic-gate 	sctp->sctp_prsctp_aware = B_FALSE;
9637c478bd9Sstevel@tonic-gate 
9647c478bd9Sstevel@tonic-gate 	sctp->sctp_connp->conn_ref = 1;
9657c478bd9Sstevel@tonic-gate 
9667c478bd9Sstevel@tonic-gate 	sctp->sctp_prsctpdrop = 0;
9677c478bd9Sstevel@tonic-gate 	sctp->sctp_msgcount = 0;
9687c478bd9Sstevel@tonic-gate 
9697c478bd9Sstevel@tonic-gate 	return (0);
970121e5416Skcpoon 
971121e5416Skcpoon failure:
972bd670b35SErik Nordmark 	sctp_headers_free(sctp);
973121e5416Skcpoon 	return (err);
9747c478bd9Sstevel@tonic-gate }
9757c478bd9Sstevel@tonic-gate 
9767c478bd9Sstevel@tonic-gate /*
9777c478bd9Sstevel@tonic-gate  * Extracts the init tag from an INIT chunk and checks if it matches
9787c478bd9Sstevel@tonic-gate  * the sctp's verification tag. Returns 0 if it doesn't match, 1 if
9797c478bd9Sstevel@tonic-gate  * it does.
9807c478bd9Sstevel@tonic-gate  */
9817c478bd9Sstevel@tonic-gate static boolean_t
9827c478bd9Sstevel@tonic-gate sctp_icmp_verf(sctp_t *sctp, sctp_hdr_t *sh, mblk_t *mp)
9837c478bd9Sstevel@tonic-gate {
9847c478bd9Sstevel@tonic-gate 	sctp_chunk_hdr_t *sch;
9857c478bd9Sstevel@tonic-gate 	uint32_t verf, *vp;
9867c478bd9Sstevel@tonic-gate 
9877c478bd9Sstevel@tonic-gate 	sch = (sctp_chunk_hdr_t *)(sh + 1);
9887c478bd9Sstevel@tonic-gate 	vp = (uint32_t *)(sch + 1);
9897c478bd9Sstevel@tonic-gate 
9907c478bd9Sstevel@tonic-gate 	/* Need at least the data chunk hdr and the first 4 bytes of INIT */
9917c478bd9Sstevel@tonic-gate 	if ((unsigned char *)(vp + 1) > mp->b_wptr) {
9927c478bd9Sstevel@tonic-gate 		return (B_FALSE);
9937c478bd9Sstevel@tonic-gate 	}
9947c478bd9Sstevel@tonic-gate 
9957c478bd9Sstevel@tonic-gate 	bcopy(vp, &verf, sizeof (verf));
9967c478bd9Sstevel@tonic-gate 
9977c478bd9Sstevel@tonic-gate 	if (verf == sctp->sctp_lvtag) {
9987c478bd9Sstevel@tonic-gate 		return (B_TRUE);
9997c478bd9Sstevel@tonic-gate 	}
10007c478bd9Sstevel@tonic-gate 	return (B_FALSE);
10017c478bd9Sstevel@tonic-gate }
10027c478bd9Sstevel@tonic-gate 
1003bd670b35SErik Nordmark /*
1004bd670b35SErik Nordmark  * Update the SCTP state according to change of PMTU.
1005bd670b35SErik Nordmark  *
1006bd670b35SErik Nordmark  * Path MTU might have changed by either increase or decrease, so need to
1007bd670b35SErik Nordmark  * adjust the MSS based on the value of ixa_pmtu.
1008bd670b35SErik Nordmark  */
1009bd670b35SErik Nordmark static void
1010bd670b35SErik Nordmark sctp_update_pmtu(sctp_t *sctp, sctp_faddr_t *fp, boolean_t decrease_only)
1011bd670b35SErik Nordmark {
1012bd670b35SErik Nordmark 	uint32_t	pmtu;
1013bd670b35SErik Nordmark 	int32_t		mss;
1014bd670b35SErik Nordmark 	ip_xmit_attr_t	*ixa = fp->ixa;
1015bd670b35SErik Nordmark 
1016bd670b35SErik Nordmark 	if (sctp->sctp_state < SCTPS_ESTABLISHED)
1017bd670b35SErik Nordmark 		return;
1018bd670b35SErik Nordmark 
1019bd670b35SErik Nordmark 	/*
1020bd670b35SErik Nordmark 	 * Always call ip_get_pmtu() to make sure that IP has updated
1021bd670b35SErik Nordmark 	 * ixa_flags properly.
1022bd670b35SErik Nordmark 	 */
1023bd670b35SErik Nordmark 	pmtu = ip_get_pmtu(ixa);
1024bd670b35SErik Nordmark 
1025bd670b35SErik Nordmark 	/*
1026bd670b35SErik Nordmark 	 * Calculate the MSS by decreasing the PMTU by sctp_hdr_len and
1027bd670b35SErik Nordmark 	 * IPsec overhead if applied. Make sure to use the most recent
1028bd670b35SErik Nordmark 	 * IPsec information.
1029bd670b35SErik Nordmark 	 */
1030bd670b35SErik Nordmark 	mss = pmtu - conn_ipsec_length(sctp->sctp_connp);
1031bd670b35SErik Nordmark 	if (ixa->ixa_flags & IXAF_IS_IPV4)
1032bd670b35SErik Nordmark 		mss -= sctp->sctp_hdr_len;
1033bd670b35SErik Nordmark 	else
1034bd670b35SErik Nordmark 		mss -= sctp->sctp_hdr6_len;
1035bd670b35SErik Nordmark 
1036bd670b35SErik Nordmark 	/*
1037bd670b35SErik Nordmark 	 * Nothing to change, so just return.
1038bd670b35SErik Nordmark 	 */
1039bd670b35SErik Nordmark 	if (mss == fp->sfa_pmss)
1040bd670b35SErik Nordmark 		return;
1041bd670b35SErik Nordmark 
1042bd670b35SErik Nordmark 	/*
1043bd670b35SErik Nordmark 	 * Currently, for ICMP errors, only PMTU decrease is handled.
1044bd670b35SErik Nordmark 	 */
1045bd670b35SErik Nordmark 	if (mss > fp->sfa_pmss && decrease_only)
1046bd670b35SErik Nordmark 		return;
1047bd670b35SErik Nordmark 
1048bd670b35SErik Nordmark #ifdef DEBUG
1049bd670b35SErik Nordmark 	(void) printf("sctp_update_pmtu mss from %d to %d\n",
1050bd670b35SErik Nordmark 	    fp->sfa_pmss, mss);
1051bd670b35SErik Nordmark #endif
1052bd670b35SErik Nordmark 	DTRACE_PROBE2(sctp_update_pmtu, int32_t, fp->sfa_pmss, uint32_t, mss);
1053bd670b35SErik Nordmark 
1054bd670b35SErik Nordmark 	/*
1055bd670b35SErik Nordmark 	 * Update ixa_fragsize and ixa_pmtu.
1056bd670b35SErik Nordmark 	 */
1057bd670b35SErik Nordmark 	ixa->ixa_fragsize = ixa->ixa_pmtu = pmtu;
1058bd670b35SErik Nordmark 
1059bd670b35SErik Nordmark 	/*
1060bd670b35SErik Nordmark 	 * Make sure that sfa_pmss is a multiple of
1061bd670b35SErik Nordmark 	 * SCTP_ALIGN.
1062bd670b35SErik Nordmark 	 */
1063bd670b35SErik Nordmark 	fp->sfa_pmss = mss & ~(SCTP_ALIGN - 1);
1064bd670b35SErik Nordmark 	fp->pmtu_discovered = 1;
1065bd670b35SErik Nordmark 
1066bd670b35SErik Nordmark #ifdef notyet
1067bd670b35SErik Nordmark 	if (mss < sctp->sctp_sctps->sctps_mss_min)
1068bd670b35SErik Nordmark 		ixa->ixa_flags |= IXAF_PMTU_TOO_SMALL;
1069bd670b35SErik Nordmark #endif
1070bd670b35SErik Nordmark 	if (ixa->ixa_flags & IXAF_PMTU_TOO_SMALL)
1071bd670b35SErik Nordmark 		ixa->ixa_flags &= ~(IXAF_DONTFRAG | IXAF_PMTU_IPV4_DF);
1072bd670b35SErik Nordmark 
1073bd670b35SErik Nordmark 	/*
1074bd670b35SErik Nordmark 	 * If below the min size then ip_get_pmtu cleared IXAF_PMTU_IPV4_DF.
1075bd670b35SErik Nordmark 	 * Make sure to clear IXAF_DONTFRAG, which is used by IP to decide
1076bd670b35SErik Nordmark 	 * whether to fragment the packet.
1077bd670b35SErik Nordmark 	 */
1078bd670b35SErik Nordmark 	if (ixa->ixa_flags & IXAF_IS_IPV4) {
1079bd670b35SErik Nordmark 		if (!(ixa->ixa_flags & IXAF_PMTU_IPV4_DF)) {
1080bd670b35SErik Nordmark 			fp->df = B_FALSE;
1081bd670b35SErik Nordmark 			if (fp == sctp->sctp_current) {
1082bd670b35SErik Nordmark 				sctp->sctp_ipha->
1083bd670b35SErik Nordmark 				    ipha_fragment_offset_and_flags = 0;
1084bd670b35SErik Nordmark 			}
1085bd670b35SErik Nordmark 		}
1086bd670b35SErik Nordmark 	}
1087bd670b35SErik Nordmark }
1088bd670b35SErik Nordmark 
1089bd670b35SErik Nordmark /*
1090bd670b35SErik Nordmark  * Notify function registered with ip_xmit_attr_t. It's called in the context
1091bd670b35SErik Nordmark  * of conn_ip_output so it's safe to update the SCTP state.
1092bd670b35SErik Nordmark  * Currently only used for pmtu changes.
1093bd670b35SErik Nordmark  */
1094bd670b35SErik Nordmark /* ARGSUSED1 */
1095bd670b35SErik Nordmark static void
1096bd670b35SErik Nordmark sctp_notify(void *arg, ip_xmit_attr_t *ixa, ixa_notify_type_t ntype,
1097bd670b35SErik Nordmark     ixa_notify_arg_t narg)
1098bd670b35SErik Nordmark {
1099bd670b35SErik Nordmark 	sctp_t		*sctp = (sctp_t *)arg;
1100bd670b35SErik Nordmark 	sctp_faddr_t	*fp;
1101bd670b35SErik Nordmark 
1102bd670b35SErik Nordmark 	switch (ntype) {
1103bd670b35SErik Nordmark 	case IXAN_PMTU:
1104bd670b35SErik Nordmark 		/* Find the faddr based on the ip_xmit_attr_t pointer */
1105bd670b35SErik Nordmark 		for (fp = sctp->sctp_faddrs; fp != NULL; fp = fp->next) {
1106bd670b35SErik Nordmark 			if (fp->ixa == ixa)
1107bd670b35SErik Nordmark 				break;
1108bd670b35SErik Nordmark 		}
1109bd670b35SErik Nordmark 		if (fp != NULL)
1110bd670b35SErik Nordmark 			sctp_update_pmtu(sctp, fp, B_FALSE);
1111bd670b35SErik Nordmark 		break;
1112bd670b35SErik Nordmark 	default:
1113bd670b35SErik Nordmark 		break;
1114bd670b35SErik Nordmark 	}
1115bd670b35SErik Nordmark }
1116bd670b35SErik Nordmark 
11177c478bd9Sstevel@tonic-gate /*
11187c478bd9Sstevel@tonic-gate  * sctp_icmp_error is called by sctp_input() to process ICMP error messages
1119bd670b35SErik Nordmark  * passed up by IP.  We need to find a sctp_t
11207c478bd9Sstevel@tonic-gate  * that corresponds to the returned datagram.  Passes the message back in on
11217c478bd9Sstevel@tonic-gate  * the correct queue once it has located the connection.
11227c478bd9Sstevel@tonic-gate  * Assumes that IP has pulled up everything up to and including
11237c478bd9Sstevel@tonic-gate  * the ICMP header.
11247c478bd9Sstevel@tonic-gate  */
11257c478bd9Sstevel@tonic-gate void
11267c478bd9Sstevel@tonic-gate sctp_icmp_error(sctp_t *sctp, mblk_t *mp)
11277c478bd9Sstevel@tonic-gate {
11287c478bd9Sstevel@tonic-gate 	icmph_t *icmph;
11297c478bd9Sstevel@tonic-gate 	ipha_t	*ipha;
11307c478bd9Sstevel@tonic-gate 	int	iph_hdr_length;
11317c478bd9Sstevel@tonic-gate 	sctp_hdr_t *sctph;
11327c478bd9Sstevel@tonic-gate 	in6_addr_t dst;
11337c478bd9Sstevel@tonic-gate 	sctp_faddr_t *fp;
1134f4b3ec61Sdh 	sctp_stack_t	*sctps = sctp->sctp_sctps;
11357c478bd9Sstevel@tonic-gate 
113645916cd2Sjpk 	dprint(1, ("sctp_icmp_error: sctp=%p, mp=%p\n", (void *)sctp,
113745916cd2Sjpk 	    (void *)mp));
11387c478bd9Sstevel@tonic-gate 
11397c478bd9Sstevel@tonic-gate 	ipha = (ipha_t *)mp->b_rptr;
11407c478bd9Sstevel@tonic-gate 	if (IPH_HDR_VERSION(ipha) != IPV4_VERSION) {
11417c478bd9Sstevel@tonic-gate 		ASSERT(IPH_HDR_VERSION(ipha) == IPV6_VERSION);
1142bd670b35SErik Nordmark 		sctp_icmp_error_ipv6(sctp, mp);
11437c478bd9Sstevel@tonic-gate 		return;
11447c478bd9Sstevel@tonic-gate 	}
11457c478bd9Sstevel@tonic-gate 
1146f0c3911fSGeorge Shepherd 	/* account for the ip hdr from the icmp message */
11477c478bd9Sstevel@tonic-gate 	iph_hdr_length = IPH_HDR_LENGTH(ipha);
11487c478bd9Sstevel@tonic-gate 	icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length];
1149f0c3911fSGeorge Shepherd 	/* now the ip hdr of message resulting in this icmp */
11507c478bd9Sstevel@tonic-gate 	ipha = (ipha_t *)&icmph[1];
11517c478bd9Sstevel@tonic-gate 	iph_hdr_length = IPH_HDR_LENGTH(ipha);
11527c478bd9Sstevel@tonic-gate 	sctph = (sctp_hdr_t *)((char *)ipha + iph_hdr_length);
1153f0c3911fSGeorge Shepherd 	/* first_mp must expose the full sctp header. */
11547c478bd9Sstevel@tonic-gate 	if ((uchar_t *)(sctph + 1) >= mp->b_wptr) {
11557c478bd9Sstevel@tonic-gate 		/* not enough data for SCTP header */
1156bd670b35SErik Nordmark 		freemsg(mp);
11577c478bd9Sstevel@tonic-gate 		return;
11587c478bd9Sstevel@tonic-gate 	}
11597c478bd9Sstevel@tonic-gate 
11607c478bd9Sstevel@tonic-gate 	switch (icmph->icmph_type) {
11617c478bd9Sstevel@tonic-gate 	case ICMP_DEST_UNREACHABLE:
11627c478bd9Sstevel@tonic-gate 		switch (icmph->icmph_code) {
11637c478bd9Sstevel@tonic-gate 		case ICMP_FRAGMENTATION_NEEDED:
11647c478bd9Sstevel@tonic-gate 			/*
11657c478bd9Sstevel@tonic-gate 			 * Reduce the MSS based on the new MTU.  This will
11667c478bd9Sstevel@tonic-gate 			 * eliminate any fragmentation locally.
11677c478bd9Sstevel@tonic-gate 			 * N.B.  There may well be some funny side-effects on
11687c478bd9Sstevel@tonic-gate 			 * the local send policy and the remote receive policy.
11697c478bd9Sstevel@tonic-gate 			 * Pending further research, we provide
11707c478bd9Sstevel@tonic-gate 			 * sctp_ignore_path_mtu just in case this proves
11717c478bd9Sstevel@tonic-gate 			 * disastrous somewhere.
11727c478bd9Sstevel@tonic-gate 			 *
11737c478bd9Sstevel@tonic-gate 			 * After updating the MSS, retransmit part of the
11747c478bd9Sstevel@tonic-gate 			 * dropped segment using the new mss by calling
11757c478bd9Sstevel@tonic-gate 			 * sctp_wput_slow().  Need to adjust all those
11767c478bd9Sstevel@tonic-gate 			 * params to make sure sctp_wput_slow() work properly.
11777c478bd9Sstevel@tonic-gate 			 */
1178f4b3ec61Sdh 			if (sctps->sctps_ignore_path_mtu)
11797c478bd9Sstevel@tonic-gate 				break;
11807c478bd9Sstevel@tonic-gate 
11817c478bd9Sstevel@tonic-gate 			/* find the offending faddr */
11827c478bd9Sstevel@tonic-gate 			IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, &dst);
11837c478bd9Sstevel@tonic-gate 			fp = sctp_lookup_faddr(sctp, &dst);
11847c478bd9Sstevel@tonic-gate 			if (fp == NULL) {
11857c478bd9Sstevel@tonic-gate 				break;
11867c478bd9Sstevel@tonic-gate 			}
1187bd670b35SErik Nordmark 			sctp_update_pmtu(sctp, fp, B_TRUE);
1188f0c3911fSGeorge Shepherd 			/*
1189f0c3911fSGeorge Shepherd 			 * It is possible, even likely that a fast retransmit
1190f0c3911fSGeorge Shepherd 			 * attempt has been dropped by ip as a result of this
1191f0c3911fSGeorge Shepherd 			 * error, retransmission bundles as much as possible.
1192f0c3911fSGeorge Shepherd 			 * A retransmit here prevents significant delays waiting
1193f0c3911fSGeorge Shepherd 			 * on the timer. Analogous to behaviour of TCP after
1194f0c3911fSGeorge Shepherd 			 * ICMP too big.
1195f0c3911fSGeorge Shepherd 			 */
1196f0c3911fSGeorge Shepherd 			sctp_rexmit(sctp, fp);
11977c478bd9Sstevel@tonic-gate 			break;
11987c478bd9Sstevel@tonic-gate 		case ICMP_PORT_UNREACHABLE:
11997c478bd9Sstevel@tonic-gate 		case ICMP_PROTOCOL_UNREACHABLE:
12007c478bd9Sstevel@tonic-gate 			switch (sctp->sctp_state) {
12017c478bd9Sstevel@tonic-gate 			case SCTPS_COOKIE_WAIT:
12027c478bd9Sstevel@tonic-gate 			case SCTPS_COOKIE_ECHOED:
12037c478bd9Sstevel@tonic-gate 				/* make sure the verification tag matches */
12047c478bd9Sstevel@tonic-gate 				if (!sctp_icmp_verf(sctp, sctph, mp)) {
12057c478bd9Sstevel@tonic-gate 					break;
12067c478bd9Sstevel@tonic-gate 				}
1207f4b3ec61Sdh 				BUMP_MIB(&sctps->sctps_mib, sctpAborted);
1208c9da23f8Skcpoon 				sctp_assoc_event(sctp, SCTP_CANT_STR_ASSOC, 0,
1209c9da23f8Skcpoon 				    NULL);
12107c478bd9Sstevel@tonic-gate 				sctp_clean_death(sctp, ECONNREFUSED);
12117c478bd9Sstevel@tonic-gate 				break;
12127c478bd9Sstevel@tonic-gate 			}
12137c478bd9Sstevel@tonic-gate 			break;
12147c478bd9Sstevel@tonic-gate 		case ICMP_HOST_UNREACHABLE:
12157c478bd9Sstevel@tonic-gate 		case ICMP_NET_UNREACHABLE:
12167c478bd9Sstevel@tonic-gate 			/* Record the error in case we finally time out. */
12177c478bd9Sstevel@tonic-gate 			sctp->sctp_client_errno = (icmph->icmph_code ==
12187c478bd9Sstevel@tonic-gate 			    ICMP_HOST_UNREACHABLE) ? EHOSTUNREACH : ENETUNREACH;
12197c478bd9Sstevel@tonic-gate 			break;
12207c478bd9Sstevel@tonic-gate 		default:
12217c478bd9Sstevel@tonic-gate 			break;
12227c478bd9Sstevel@tonic-gate 		}
12237c478bd9Sstevel@tonic-gate 		break;
12247c478bd9Sstevel@tonic-gate 	case ICMP_SOURCE_QUENCH: {
12257c478bd9Sstevel@tonic-gate 		/* Reduce the sending rate as if we got a retransmit timeout */
12267c478bd9Sstevel@tonic-gate 		break;
12277c478bd9Sstevel@tonic-gate 	}
12287c478bd9Sstevel@tonic-gate 	}
1229bd670b35SErik Nordmark 	freemsg(mp);
12307c478bd9Sstevel@tonic-gate }
12317c478bd9Sstevel@tonic-gate 
12327c478bd9Sstevel@tonic-gate /*
12337c478bd9Sstevel@tonic-gate  * sctp_icmp_error_ipv6() is called by sctp_icmp_error() to process ICMPv6
12347c478bd9Sstevel@tonic-gate  * error messages passed up by IP.
12357c478bd9Sstevel@tonic-gate  * Assumes that IP has pulled up all the extension headers as well
12367c478bd9Sstevel@tonic-gate  * as the ICMPv6 header.
12377c478bd9Sstevel@tonic-gate  */
12387c478bd9Sstevel@tonic-gate static void
12397c478bd9Sstevel@tonic-gate sctp_icmp_error_ipv6(sctp_t *sctp, mblk_t *mp)
12407c478bd9Sstevel@tonic-gate {
12417c478bd9Sstevel@tonic-gate 	icmp6_t *icmp6;
12427c478bd9Sstevel@tonic-gate 	ip6_t	*ip6h;
12437c478bd9Sstevel@tonic-gate 	uint16_t	iph_hdr_length;
12447c478bd9Sstevel@tonic-gate 	sctp_hdr_t *sctpha;
12457c478bd9Sstevel@tonic-gate 	uint8_t	*nexthdrp;
12467c478bd9Sstevel@tonic-gate 	sctp_faddr_t *fp;
1247f4b3ec61Sdh 	sctp_stack_t	*sctps = sctp->sctp_sctps;
12487c478bd9Sstevel@tonic-gate 
12497c478bd9Sstevel@tonic-gate 	ip6h = (ip6_t *)mp->b_rptr;
12507c478bd9Sstevel@tonic-gate 	iph_hdr_length = (ip6h->ip6_nxt != IPPROTO_SCTP) ?
12517c478bd9Sstevel@tonic-gate 	    ip_hdr_length_v6(mp, ip6h) : IPV6_HDR_LEN;
12527c478bd9Sstevel@tonic-gate 
12537c478bd9Sstevel@tonic-gate 	icmp6 = (icmp6_t *)&mp->b_rptr[iph_hdr_length];
12547c478bd9Sstevel@tonic-gate 	ip6h = (ip6_t *)&icmp6[1];
12557c478bd9Sstevel@tonic-gate 	if (!ip_hdr_length_nexthdr_v6(mp, ip6h, &iph_hdr_length, &nexthdrp)) {
12567c478bd9Sstevel@tonic-gate 		freemsg(mp);
12577c478bd9Sstevel@tonic-gate 		return;
12587c478bd9Sstevel@tonic-gate 	}
12597c478bd9Sstevel@tonic-gate 	ASSERT(*nexthdrp == IPPROTO_SCTP);
12607c478bd9Sstevel@tonic-gate 
12617c478bd9Sstevel@tonic-gate 	/* XXX need ifindex to find connection */
12627c478bd9Sstevel@tonic-gate 	sctpha = (sctp_hdr_t *)((char *)ip6h + iph_hdr_length);
12637c478bd9Sstevel@tonic-gate 	if ((uchar_t *)sctpha >= mp->b_wptr) {
12647c478bd9Sstevel@tonic-gate 		/* not enough data for SCTP header */
12657c478bd9Sstevel@tonic-gate 		freemsg(mp);
12667c478bd9Sstevel@tonic-gate 		return;
12677c478bd9Sstevel@tonic-gate 	}
12687c478bd9Sstevel@tonic-gate 	switch (icmp6->icmp6_type) {
12697c478bd9Sstevel@tonic-gate 	case ICMP6_PACKET_TOO_BIG:
12707c478bd9Sstevel@tonic-gate 		/*
12717c478bd9Sstevel@tonic-gate 		 * Reduce the MSS based on the new MTU.  This will
12727c478bd9Sstevel@tonic-gate 		 * eliminate any fragmentation locally.
12737c478bd9Sstevel@tonic-gate 		 * N.B.  There may well be some funny side-effects on
12747c478bd9Sstevel@tonic-gate 		 * the local send policy and the remote receive policy.
12757c478bd9Sstevel@tonic-gate 		 * Pending further research, we provide
12767c478bd9Sstevel@tonic-gate 		 * sctp_ignore_path_mtu just in case this proves
12777c478bd9Sstevel@tonic-gate 		 * disastrous somewhere.
12787c478bd9Sstevel@tonic-gate 		 *
12797c478bd9Sstevel@tonic-gate 		 * After updating the MSS, retransmit part of the
12807c478bd9Sstevel@tonic-gate 		 * dropped segment using the new mss by calling
12817c478bd9Sstevel@tonic-gate 		 * sctp_wput_slow().  Need to adjust all those
12827c478bd9Sstevel@tonic-gate 		 * params to make sure sctp_wput_slow() work properly.
12837c478bd9Sstevel@tonic-gate 		 */
1284f4b3ec61Sdh 		if (sctps->sctps_ignore_path_mtu)
12857c478bd9Sstevel@tonic-gate 			break;
12867c478bd9Sstevel@tonic-gate 
12877c478bd9Sstevel@tonic-gate 		/* find the offending faddr */
12887c478bd9Sstevel@tonic-gate 		fp = sctp_lookup_faddr(sctp, &ip6h->ip6_dst);
12897c478bd9Sstevel@tonic-gate 		if (fp == NULL) {
12907c478bd9Sstevel@tonic-gate 			break;
12917c478bd9Sstevel@tonic-gate 		}
12927c478bd9Sstevel@tonic-gate 
1293bd670b35SErik Nordmark 		sctp_update_pmtu(sctp, fp, B_TRUE);
1294bd670b35SErik Nordmark 		/*
1295bd670b35SErik Nordmark 		 * It is possible, even likely that a fast retransmit
1296bd670b35SErik Nordmark 		 * attempt has been dropped by ip as a result of this
1297bd670b35SErik Nordmark 		 * error, retransmission bundles as much as possible.
1298bd670b35SErik Nordmark 		 * A retransmit here prevents significant delays waiting
1299bd670b35SErik Nordmark 		 * on the timer. Analogous to behaviour of TCP after
1300bd670b35SErik Nordmark 		 * ICMP too big.
1301bd670b35SErik Nordmark 		 */
1302bd670b35SErik Nordmark 		sctp_rexmit(sctp, fp);
13037c478bd9Sstevel@tonic-gate 		break;
13047c478bd9Sstevel@tonic-gate 
13057c478bd9Sstevel@tonic-gate 	case ICMP6_DST_UNREACH:
13067c478bd9Sstevel@tonic-gate 		switch (icmp6->icmp6_code) {
13077c478bd9Sstevel@tonic-gate 		case ICMP6_DST_UNREACH_NOPORT:
13087c478bd9Sstevel@tonic-gate 			/* make sure the verification tag matches */
13097c478bd9Sstevel@tonic-gate 			if (!sctp_icmp_verf(sctp, sctpha, mp)) {
13107c478bd9Sstevel@tonic-gate 				break;
13117c478bd9Sstevel@tonic-gate 			}
13127c478bd9Sstevel@tonic-gate 			if (sctp->sctp_state == SCTPS_COOKIE_WAIT ||
13137c478bd9Sstevel@tonic-gate 			    sctp->sctp_state == SCTPS_COOKIE_ECHOED) {
1314f4b3ec61Sdh 				BUMP_MIB(&sctps->sctps_mib, sctpAborted);
1315c9da23f8Skcpoon 				sctp_assoc_event(sctp, SCTP_CANT_STR_ASSOC, 0,
1316c9da23f8Skcpoon 				    NULL);
13177c478bd9Sstevel@tonic-gate 				sctp_clean_death(sctp, ECONNREFUSED);
13187c478bd9Sstevel@tonic-gate 			}
13197c478bd9Sstevel@tonic-gate 			break;
13207c478bd9Sstevel@tonic-gate 
13217c478bd9Sstevel@tonic-gate 		case ICMP6_DST_UNREACH_ADMIN:
13227c478bd9Sstevel@tonic-gate 		case ICMP6_DST_UNREACH_NOROUTE:
13237c478bd9Sstevel@tonic-gate 		case ICMP6_DST_UNREACH_NOTNEIGHBOR:
13247c478bd9Sstevel@tonic-gate 		case ICMP6_DST_UNREACH_ADDR:
13257c478bd9Sstevel@tonic-gate 			/* Record the error in case we finally time out. */
13267c478bd9Sstevel@tonic-gate 			sctp->sctp_client_errno = EHOSTUNREACH;
13277c478bd9Sstevel@tonic-gate 			break;
13287c478bd9Sstevel@tonic-gate 		default:
13297c478bd9Sstevel@tonic-gate 			break;
13307c478bd9Sstevel@tonic-gate 		}
13317c478bd9Sstevel@tonic-gate 		break;
13327c478bd9Sstevel@tonic-gate 
13337c478bd9Sstevel@tonic-gate 	case ICMP6_PARAM_PROB:
13347c478bd9Sstevel@tonic-gate 		/* If this corresponds to an ICMP_PROTOCOL_UNREACHABLE */
13357c478bd9Sstevel@tonic-gate 		if (icmp6->icmp6_code == ICMP6_PARAMPROB_NEXTHEADER &&
13367c478bd9Sstevel@tonic-gate 		    (uchar_t *)ip6h + icmp6->icmp6_pptr ==
13377c478bd9Sstevel@tonic-gate 		    (uchar_t *)nexthdrp) {
13387c478bd9Sstevel@tonic-gate 			/* make sure the verification tag matches */
13397c478bd9Sstevel@tonic-gate 			if (!sctp_icmp_verf(sctp, sctpha, mp)) {
13407c478bd9Sstevel@tonic-gate 				break;
13417c478bd9Sstevel@tonic-gate 			}
13427c478bd9Sstevel@tonic-gate 			if (sctp->sctp_state == SCTPS_COOKIE_WAIT) {
1343f4b3ec61Sdh 				BUMP_MIB(&sctps->sctps_mib, sctpAborted);
1344c9da23f8Skcpoon 				sctp_assoc_event(sctp, SCTP_CANT_STR_ASSOC, 0,
1345c9da23f8Skcpoon 				    NULL);
13467c478bd9Sstevel@tonic-gate 				sctp_clean_death(sctp, ECONNREFUSED);
13477c478bd9Sstevel@tonic-gate 			}
13487c478bd9Sstevel@tonic-gate 			break;
13497c478bd9Sstevel@tonic-gate 		}
13507c478bd9Sstevel@tonic-gate 		break;
13517c478bd9Sstevel@tonic-gate 
13527c478bd9Sstevel@tonic-gate 	case ICMP6_TIME_EXCEEDED:
13537c478bd9Sstevel@tonic-gate 	default:
13547c478bd9Sstevel@tonic-gate 		break;
13557c478bd9Sstevel@tonic-gate 	}
13567c478bd9Sstevel@tonic-gate 	freemsg(mp);
13577c478bd9Sstevel@tonic-gate }
13587c478bd9Sstevel@tonic-gate 
13597c478bd9Sstevel@tonic-gate /*
13607c478bd9Sstevel@tonic-gate  * Called by sockfs to create a new sctp instance.
13617c478bd9Sstevel@tonic-gate  *
13627c478bd9Sstevel@tonic-gate  * If parent pointer is passed in, inherit settings from it.
13637c478bd9Sstevel@tonic-gate  */
13647c478bd9Sstevel@tonic-gate sctp_t *
1365bd670b35SErik Nordmark sctp_create(void *ulpd, sctp_t *parent, int family, int type, int flags,
13660f1702c5SYu Xiangning     sock_upcalls_t *upcalls, sctp_sockbuf_limits_t *sbl,
13677c478bd9Sstevel@tonic-gate     cred_t *credp)
13687c478bd9Sstevel@tonic-gate {
13697c478bd9Sstevel@tonic-gate 	sctp_t		*sctp, *psctp;
1370bd670b35SErik Nordmark 	conn_t		*connp;
13717c478bd9Sstevel@tonic-gate 	mblk_t		*ack_mp, *hb_mp;
13727c478bd9Sstevel@tonic-gate 	int		sleep = flags & SCTP_CAN_BLOCK ? KM_SLEEP : KM_NOSLEEP;
1373f4b3ec61Sdh 	zoneid_t	zoneid;
1374f4b3ec61Sdh 	sctp_stack_t	*sctps;
13757c478bd9Sstevel@tonic-gate 
13767c478bd9Sstevel@tonic-gate 	/* User must supply a credential. */
13777c478bd9Sstevel@tonic-gate 	if (credp == NULL)
13787c478bd9Sstevel@tonic-gate 		return (NULL);
13797c478bd9Sstevel@tonic-gate 
13807c478bd9Sstevel@tonic-gate 	psctp = (sctp_t *)parent;
1381f4b3ec61Sdh 	if (psctp != NULL) {
1382f4b3ec61Sdh 		sctps = psctp->sctp_sctps;
1383f4b3ec61Sdh 		/* Increase here to have common decrease at end */
1384f4b3ec61Sdh 		netstack_hold(sctps->sctps_netstack);
1385f4b3ec61Sdh 	} else {
1386f4b3ec61Sdh 		netstack_t *ns;
1387f4b3ec61Sdh 
1388f4b3ec61Sdh 		ns = netstack_find_by_cred(credp);
1389f4b3ec61Sdh 		ASSERT(ns != NULL);
1390f4b3ec61Sdh 		sctps = ns->netstack_sctp;
1391f4b3ec61Sdh 		ASSERT(sctps != NULL);
13927c478bd9Sstevel@tonic-gate 
1393f4b3ec61Sdh 		/*
1394f4b3ec61Sdh 		 * For exclusive stacks we set the zoneid to zero
1395f4b3ec61Sdh 		 * to make SCTP operate as if in the global zone.
1396f4b3ec61Sdh 		 */
1397f4b3ec61Sdh 		if (sctps->sctps_netstack->netstack_stackid !=
1398f4b3ec61Sdh 		    GLOBAL_NETSTACKID)
1399f4b3ec61Sdh 			zoneid = GLOBAL_ZONEID;
1400f4b3ec61Sdh 		else
1401f4b3ec61Sdh 			zoneid = crgetzoneid(credp);
1402f4b3ec61Sdh 	}
1403bd670b35SErik Nordmark 	if ((connp = ipcl_conn_create(IPCL_SCTPCONN, sleep,
1404b34b8d1aSkcpoon 	    sctps->sctps_netstack)) == NULL) {
1405f4b3ec61Sdh 		netstack_rele(sctps->sctps_netstack);
1406f4b3ec61Sdh 		SCTP_KSTAT(sctps, sctp_conn_create);
1407f4b3ec61Sdh 		return (NULL);
1408f4b3ec61Sdh 	}
1409f4b3ec61Sdh 	/*
1410f4b3ec61Sdh 	 * ipcl_conn_create did a netstack_hold. Undo the hold that was
1411f4b3ec61Sdh 	 * done at top of sctp_create.
1412f4b3ec61Sdh 	 */
1413f4b3ec61Sdh 	netstack_rele(sctps->sctps_netstack);
1414bd670b35SErik Nordmark 	sctp = CONN2SCTP(connp);
1415f4b3ec61Sdh 	sctp->sctp_sctps = sctps;
14167c478bd9Sstevel@tonic-gate 
1417121e5416Skcpoon 	if ((ack_mp = sctp_timer_alloc(sctp, sctp_ack_timer, sleep)) == NULL ||
1418121e5416Skcpoon 	    (hb_mp = sctp_timer_alloc(sctp, sctp_heartbeat_timer,
1419121e5416Skcpoon 	    sleep)) == NULL) {
14207c478bd9Sstevel@tonic-gate 		if (ack_mp != NULL)
14217c478bd9Sstevel@tonic-gate 			freeb(ack_mp);
1422bd670b35SErik Nordmark 		sctp_conn_clear(connp);
1423f4b3ec61Sdh 		sctp->sctp_sctps = NULL;
1424bd670b35SErik Nordmark 		kmem_cache_free(sctp_conn_cache, connp);
14257c478bd9Sstevel@tonic-gate 		return (NULL);
14267c478bd9Sstevel@tonic-gate 	}
14277c478bd9Sstevel@tonic-gate 
14287c478bd9Sstevel@tonic-gate 	sctp->sctp_ack_mp = ack_mp;
14297c478bd9Sstevel@tonic-gate 	sctp->sctp_heartbeat_mp = hb_mp;
14307c478bd9Sstevel@tonic-gate 
1431bd670b35SErik Nordmark 	/*
1432bd670b35SErik Nordmark 	 * Have conn_ip_output drop packets should our outer source
1433bd670b35SErik Nordmark 	 * go invalid, and tell us about mtu changes.
1434bd670b35SErik Nordmark 	 */
1435bd670b35SErik Nordmark 	connp->conn_ixa->ixa_flags |= IXAF_SET_ULP_CKSUM | IXAF_VERIFY_SOURCE |
1436bd670b35SErik Nordmark 	    IXAF_VERIFY_PMTU;
1437bd670b35SErik Nordmark 	connp->conn_family = family;
1438bd670b35SErik Nordmark 	connp->conn_so_type = type;
14397c478bd9Sstevel@tonic-gate 
14407c478bd9Sstevel@tonic-gate 	if (sctp_init_values(sctp, psctp, sleep) != 0) {
14417c478bd9Sstevel@tonic-gate 		freeb(ack_mp);
14427c478bd9Sstevel@tonic-gate 		freeb(hb_mp);
1443bd670b35SErik Nordmark 		sctp_conn_clear(connp);
1444f4b3ec61Sdh 		sctp->sctp_sctps = NULL;
1445bd670b35SErik Nordmark 		kmem_cache_free(sctp_conn_cache, connp);
14467c478bd9Sstevel@tonic-gate 		return (NULL);
14477c478bd9Sstevel@tonic-gate 	}
14487c478bd9Sstevel@tonic-gate 	sctp->sctp_cansleep = ((flags & SCTP_CAN_BLOCK) == SCTP_CAN_BLOCK);
14497c478bd9Sstevel@tonic-gate 
1450f4b3ec61Sdh 	sctp->sctp_mss = sctps->sctps_initial_mtu - ((family == AF_INET6) ?
1451b34b8d1aSkcpoon 	    sctp->sctp_hdr6_len : sctp->sctp_hdr_len);
14527c478bd9Sstevel@tonic-gate 
14537c478bd9Sstevel@tonic-gate 	if (psctp != NULL) {
1454bd670b35SErik Nordmark 		conn_t	*pconnp = psctp->sctp_connp;
1455bd670b35SErik Nordmark 
14567c478bd9Sstevel@tonic-gate 		RUN_SCTP(psctp);
14577c478bd9Sstevel@tonic-gate 		/*
14587c478bd9Sstevel@tonic-gate 		 * Inherit local address list, local port. Parent is either
14597c478bd9Sstevel@tonic-gate 		 * in SCTPS_BOUND, or SCTPS_LISTEN state.
14607c478bd9Sstevel@tonic-gate 		 */
14617c478bd9Sstevel@tonic-gate 		ASSERT((psctp->sctp_state == SCTPS_BOUND) ||
14627c478bd9Sstevel@tonic-gate 		    (psctp->sctp_state == SCTPS_LISTEN));
14637c478bd9Sstevel@tonic-gate 		if (sctp_dup_saddrs(psctp, sctp, sleep)) {
14647c478bd9Sstevel@tonic-gate 			WAKE_SCTP(psctp);
14657c478bd9Sstevel@tonic-gate 			freeb(ack_mp);
14667c478bd9Sstevel@tonic-gate 			freeb(hb_mp);
14677c478bd9Sstevel@tonic-gate 			sctp_headers_free(sctp);
1468bd670b35SErik Nordmark 			sctp_conn_clear(connp);
1469f4b3ec61Sdh 			sctp->sctp_sctps = NULL;
1470bd670b35SErik Nordmark 			kmem_cache_free(sctp_conn_cache, connp);
14717c478bd9Sstevel@tonic-gate 			return (NULL);
14727c478bd9Sstevel@tonic-gate 		}
14737c478bd9Sstevel@tonic-gate 
14747c478bd9Sstevel@tonic-gate 		/*
14757c478bd9Sstevel@tonic-gate 		 * If the parent is specified, it'll be immediatelly
14767c478bd9Sstevel@tonic-gate 		 * followed by sctp_connect(). So don't add this guy to
14777c478bd9Sstevel@tonic-gate 		 * bind hash.
14787c478bd9Sstevel@tonic-gate 		 */
1479bd670b35SErik Nordmark 		connp->conn_lport = pconnp->conn_lport;
14807c478bd9Sstevel@tonic-gate 		sctp->sctp_state = SCTPS_BOUND;
14817c478bd9Sstevel@tonic-gate 		WAKE_SCTP(psctp);
14827c478bd9Sstevel@tonic-gate 	} else {
1483bd670b35SErik Nordmark 		ASSERT(connp->conn_cred == NULL);
1484bd670b35SErik Nordmark 		connp->conn_zoneid = zoneid;
1485bd670b35SErik Nordmark 		/*
1486bd670b35SErik Nordmark 		 * conn_allzones can not be set this early, hence
1487bd670b35SErik Nordmark 		 * no IPCL_ZONEID
1488bd670b35SErik Nordmark 		 */
1489bd670b35SErik Nordmark 		connp->conn_ixa->ixa_zoneid = zoneid;
1490d3d50737SRafael Vanoni 		connp->conn_open_time = ddi_get_lbolt64();
1491bd670b35SErik Nordmark 		connp->conn_cred = credp;
1492bd670b35SErik Nordmark 		crhold(credp);
1493bd670b35SErik Nordmark 		connp->conn_cpid = curproc->p_pid;
1494de8c4a14SErik Nordmark 
1495bd670b35SErik Nordmark 		/*
1496bd670b35SErik Nordmark 		 * If the caller has the process-wide flag set, then default to
1497bd670b35SErik Nordmark 		 * MAC exempt mode.  This allows read-down to unlabeled hosts.
1498bd670b35SErik Nordmark 		 */
1499bd670b35SErik Nordmark 		if (getpflags(NET_MAC_AWARE, credp) != 0)
1500bd670b35SErik Nordmark 			connp->conn_mac_mode = CONN_MAC_AWARE;
15017c478bd9Sstevel@tonic-gate 
1502bd670b35SErik Nordmark 		connp->conn_zone_is_global =
1503bd670b35SErik Nordmark 		    (crgetzoneid(credp) == GLOBAL_ZONEID);
1504bd670b35SErik Nordmark 	}
150545916cd2Sjpk 
15067c478bd9Sstevel@tonic-gate 	/* Initialize SCTP instance values,  our verf tag must never be 0 */
15077c478bd9Sstevel@tonic-gate 	(void) random_get_pseudo_bytes((uint8_t *)&sctp->sctp_lvtag,
15087c478bd9Sstevel@tonic-gate 	    sizeof (sctp->sctp_lvtag));
15097c478bd9Sstevel@tonic-gate 	if (sctp->sctp_lvtag == 0)
15107c478bd9Sstevel@tonic-gate 		sctp->sctp_lvtag = (uint32_t)gethrtime();
15117c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_lvtag != 0);
15127c478bd9Sstevel@tonic-gate 
15137c478bd9Sstevel@tonic-gate 	sctp->sctp_ltsn = sctp->sctp_lvtag + 1;
15147c478bd9Sstevel@tonic-gate 	sctp->sctp_lcsn = sctp->sctp_ltsn;
15157c478bd9Sstevel@tonic-gate 	sctp->sctp_recovery_tsn = sctp->sctp_lastack_rxd = sctp->sctp_ltsn - 1;
15167c478bd9Sstevel@tonic-gate 	sctp->sctp_adv_pap = sctp->sctp_lastack_rxd;
15177c478bd9Sstevel@tonic-gate 
15187c478bd9Sstevel@tonic-gate 	/* Information required by upper layer */
1519bd670b35SErik Nordmark 	ASSERT(ulpd != NULL);
1520bd670b35SErik Nordmark 	sctp->sctp_ulpd = ulpd;
1521bd670b35SErik Nordmark 
1522bd670b35SErik Nordmark 	ASSERT(upcalls != NULL);
1523bd670b35SErik Nordmark 	sctp->sctp_upcalls = upcalls;
1524bd670b35SErik Nordmark 	ASSERT(sbl != NULL);
1525bd670b35SErik Nordmark 	/* Fill in the socket buffer limits for sctpsockfs */
1526bd670b35SErik Nordmark 	sbl->sbl_txlowat = connp->conn_sndlowat;
1527bd670b35SErik Nordmark 	sbl->sbl_txbuf = connp->conn_sndbuf;
1528bd670b35SErik Nordmark 	sbl->sbl_rxbuf = sctp->sctp_rwnd;
1529bd670b35SErik Nordmark 	sbl->sbl_rxlowat = SCTP_RECV_LOWATER;
15307c478bd9Sstevel@tonic-gate 
15317c478bd9Sstevel@tonic-gate 	/* Insert this in the global list. */
1532f4b3ec61Sdh 	SCTP_LINK(sctp, sctps);
15337c478bd9Sstevel@tonic-gate 
15347c478bd9Sstevel@tonic-gate 	return (sctp);
15357c478bd9Sstevel@tonic-gate }
15367c478bd9Sstevel@tonic-gate 
1537f4b3ec61Sdh /* Run at module load time */
1538f4b3ec61Sdh void
1539f4b3ec61Sdh sctp_ddi_g_init(void)
1540f4b3ec61Sdh {
15417c478bd9Sstevel@tonic-gate 	/* Create sctp_t/conn_t cache */
15427c478bd9Sstevel@tonic-gate 	sctp_conn_cache_init();
15437c478bd9Sstevel@tonic-gate 
15447c478bd9Sstevel@tonic-gate 	/* Create the faddr cache */
15457c478bd9Sstevel@tonic-gate 	sctp_faddr_init();
15467c478bd9Sstevel@tonic-gate 
15477c478bd9Sstevel@tonic-gate 	/* Create the sets cache */
15487c478bd9Sstevel@tonic-gate 	sctp_sets_init();
15497c478bd9Sstevel@tonic-gate 
15507c478bd9Sstevel@tonic-gate 	/* Create the PR-SCTP sets cache */
15517c478bd9Sstevel@tonic-gate 	sctp_ftsn_sets_init();
15527c478bd9Sstevel@tonic-gate 
1553f4b3ec61Sdh 	/* Initialize tables used for CRC calculation */
1554f4b3ec61Sdh 	sctp_crc32_init();
1555f4b3ec61Sdh 
1556f4b3ec61Sdh 	/*
1557f4b3ec61Sdh 	 * We want to be informed each time a stack is created or
1558f4b3ec61Sdh 	 * destroyed in the kernel, so we can maintain the
1559f4b3ec61Sdh 	 * set of sctp_stack_t's.
1560f4b3ec61Sdh 	 */
1561bd670b35SErik Nordmark 	netstack_register(NS_SCTP, sctp_stack_init, NULL, sctp_stack_fini);
1562f4b3ec61Sdh }
1563f4b3ec61Sdh 
1564f4b3ec61Sdh static void *
1565f4b3ec61Sdh sctp_stack_init(netstackid_t stackid, netstack_t *ns)
1566f4b3ec61Sdh {
1567f4b3ec61Sdh 	sctp_stack_t	*sctps;
1568f4b3ec61Sdh 
1569f4b3ec61Sdh 	sctps = kmem_zalloc(sizeof (*sctps), KM_SLEEP);
1570f4b3ec61Sdh 	sctps->sctps_netstack = ns;
1571f4b3ec61Sdh 
1572f4b3ec61Sdh 	/* Initialize locks */
1573f4b3ec61Sdh 	mutex_init(&sctps->sctps_g_lock, NULL, MUTEX_DEFAULT, NULL);
1574f4b3ec61Sdh 	mutex_init(&sctps->sctps_epriv_port_lock, NULL, MUTEX_DEFAULT, NULL);
1575f4b3ec61Sdh 	sctps->sctps_g_num_epriv_ports = SCTP_NUM_EPRIV_PORTS;
1576f4b3ec61Sdh 	sctps->sctps_g_epriv_ports[0] = 2049;
1577f4b3ec61Sdh 	sctps->sctps_g_epriv_ports[1] = 4045;
1578f4b3ec61Sdh 
1579f4b3ec61Sdh 	/* Initialize SCTP hash arrays. */
1580f4b3ec61Sdh 	sctp_hash_init(sctps);
1581f4b3ec61Sdh 
1582f4b3ec61Sdh 	if (!sctp_nd_init(sctps)) {
1583f4b3ec61Sdh 		sctp_nd_free(sctps);
1584f4b3ec61Sdh 	}
1585f4b3ec61Sdh 
15867c478bd9Sstevel@tonic-gate 	/* Initialize the recvq taskq. */
1587f4b3ec61Sdh 	sctp_rq_tq_init(sctps);
15887c478bd9Sstevel@tonic-gate 
15897c478bd9Sstevel@tonic-gate 	/* saddr init */
1590f4b3ec61Sdh 	sctp_saddr_init(sctps);
15917c478bd9Sstevel@tonic-gate 
15927c478bd9Sstevel@tonic-gate 	/* Global SCTP PCB list. */
1593f4b3ec61Sdh 	list_create(&sctps->sctps_g_list, sizeof (sctp_t),
15947c478bd9Sstevel@tonic-gate 	    offsetof(sctp_t, sctp_list));
15957c478bd9Sstevel@tonic-gate 
15967c478bd9Sstevel@tonic-gate 	/* Initialize sctp kernel stats. */
1597f4b3ec61Sdh 	sctps->sctps_mibkp = sctp_kstat_init(stackid);
1598f4b3ec61Sdh 	sctps->sctps_kstat =
1599f4b3ec61Sdh 	    sctp_kstat2_init(stackid, &sctps->sctps_statistics);
1600f4b3ec61Sdh 
1601f4b3ec61Sdh 	return (sctps);
16027c478bd9Sstevel@tonic-gate }
16037c478bd9Sstevel@tonic-gate 
1604f4b3ec61Sdh /*
1605f4b3ec61Sdh  * Called when the module is about to be unloaded.
1606f4b3ec61Sdh  */
16077c478bd9Sstevel@tonic-gate void
1608f4b3ec61Sdh sctp_ddi_g_destroy(void)
16097c478bd9Sstevel@tonic-gate {
16107c478bd9Sstevel@tonic-gate 	/* Destroy sctp_t/conn_t caches */
16117c478bd9Sstevel@tonic-gate 	sctp_conn_cache_fini();
16127c478bd9Sstevel@tonic-gate 
16137c478bd9Sstevel@tonic-gate 	/* Destroy the faddr cache */
16147c478bd9Sstevel@tonic-gate 	sctp_faddr_fini();
16157c478bd9Sstevel@tonic-gate 
16167c478bd9Sstevel@tonic-gate 	/* Destroy the sets cache */
16177c478bd9Sstevel@tonic-gate 	sctp_sets_fini();
16187c478bd9Sstevel@tonic-gate 
16197c478bd9Sstevel@tonic-gate 	/* Destroy the PR-SCTP sets cache */
16207c478bd9Sstevel@tonic-gate 	sctp_ftsn_sets_fini();
16217c478bd9Sstevel@tonic-gate 
1622f4b3ec61Sdh 	netstack_unregister(NS_SCTP);
1623f4b3ec61Sdh }
1624f4b3ec61Sdh 
1625f4b3ec61Sdh /*
1626f4b3ec61Sdh  * Free the SCTP stack instance.
1627f4b3ec61Sdh  */
1628f4b3ec61Sdh static void
1629f4b3ec61Sdh sctp_stack_fini(netstackid_t stackid, void *arg)
1630f4b3ec61Sdh {
1631f4b3ec61Sdh 	sctp_stack_t *sctps = (sctp_stack_t *)arg;
1632f4b3ec61Sdh 
1633f4b3ec61Sdh 	sctp_nd_free(sctps);
1634f4b3ec61Sdh 
16357c478bd9Sstevel@tonic-gate 	/* Destroy the recvq taskqs. */
1636f4b3ec61Sdh 	sctp_rq_tq_fini(sctps);
16377c478bd9Sstevel@tonic-gate 
16387c478bd9Sstevel@tonic-gate 	/* Destroy saddr  */
1639f4b3ec61Sdh 	sctp_saddr_fini(sctps);
16407c478bd9Sstevel@tonic-gate 
16417c478bd9Sstevel@tonic-gate 	/* Global SCTP PCB list. */
1642f4b3ec61Sdh 	list_destroy(&sctps->sctps_g_list);
16437c478bd9Sstevel@tonic-gate 
16447c478bd9Sstevel@tonic-gate 	/* Destroy SCTP hash arrays. */
1645f4b3ec61Sdh 	sctp_hash_destroy(sctps);
1646f4b3ec61Sdh 
1647f4b3ec61Sdh 	/* Destroy SCTP kernel stats. */
1648f4b3ec61Sdh 	sctp_kstat2_fini(stackid, sctps->sctps_kstat);
1649f4b3ec61Sdh 	sctps->sctps_kstat = NULL;
1650f4b3ec61Sdh 	bzero(&sctps->sctps_statistics, sizeof (sctps->sctps_statistics));
1651f4b3ec61Sdh 
1652f4b3ec61Sdh 	sctp_kstat_fini(stackid, sctps->sctps_mibkp);
1653f4b3ec61Sdh 	sctps->sctps_mibkp = NULL;
1654f4b3ec61Sdh 
1655f4b3ec61Sdh 	mutex_destroy(&sctps->sctps_g_lock);
1656f4b3ec61Sdh 	mutex_destroy(&sctps->sctps_epriv_port_lock);
16577c478bd9Sstevel@tonic-gate 
1658f4b3ec61Sdh 	kmem_free(sctps, sizeof (*sctps));
16597c478bd9Sstevel@tonic-gate }
16607c478bd9Sstevel@tonic-gate 
16617c478bd9Sstevel@tonic-gate void
1662f4b3ec61Sdh sctp_display_all(sctp_stack_t *sctps)
16637c478bd9Sstevel@tonic-gate {
16647c478bd9Sstevel@tonic-gate 	sctp_t *sctp_walker;
16657c478bd9Sstevel@tonic-gate 
1666f4b3ec61Sdh 	mutex_enter(&sctps->sctps_g_lock);
1667bd670b35SErik Nordmark 	for (sctp_walker = list_head(&sctps->sctps_g_list);
1668bd670b35SErik Nordmark 	    sctp_walker != NULL;
1669f4b3ec61Sdh 	    sctp_walker = (sctp_t *)list_next(&sctps->sctps_g_list,
1670f4b3ec61Sdh 	    sctp_walker)) {
16717c478bd9Sstevel@tonic-gate 		(void) sctp_display(sctp_walker, NULL);
16727c478bd9Sstevel@tonic-gate 	}
1673f4b3ec61Sdh 	mutex_exit(&sctps->sctps_g_lock);
16747c478bd9Sstevel@tonic-gate }
16757c478bd9Sstevel@tonic-gate 
16767c478bd9Sstevel@tonic-gate static void
1677f4b3ec61Sdh sctp_rq_tq_init(sctp_stack_t *sctps)
16787c478bd9Sstevel@tonic-gate {
1679f4b3ec61Sdh 	sctps->sctps_recvq_tq_list_max_sz = 16;
1680f4b3ec61Sdh 	sctps->sctps_recvq_tq_list_cur_sz = 1;
16817c478bd9Sstevel@tonic-gate 	/*
16827c478bd9Sstevel@tonic-gate 	 * Initialize the recvq_tq_list and create the first recvq taskq.
16837c478bd9Sstevel@tonic-gate 	 * What to do if it fails?
16847c478bd9Sstevel@tonic-gate 	 */
1685f4b3ec61Sdh 	sctps->sctps_recvq_tq_list =
1686f4b3ec61Sdh 	    kmem_zalloc(sctps->sctps_recvq_tq_list_max_sz * sizeof (taskq_t *),
1687b34b8d1aSkcpoon 	    KM_SLEEP);
1688f4b3ec61Sdh 	sctps->sctps_recvq_tq_list[0] = taskq_create("sctp_def_recvq_taskq",
16897c478bd9Sstevel@tonic-gate 	    MIN(sctp_recvq_tq_thr_max, MAX(sctp_recvq_tq_thr_min, ncpus)),
16907c478bd9Sstevel@tonic-gate 	    minclsyspri, sctp_recvq_tq_task_min, sctp_recvq_tq_task_max,
16917c478bd9Sstevel@tonic-gate 	    TASKQ_PREPOPULATE);
1692f4b3ec61Sdh 	mutex_init(&sctps->sctps_rq_tq_lock, NULL, MUTEX_DEFAULT, NULL);
16937c478bd9Sstevel@tonic-gate }
16947c478bd9Sstevel@tonic-gate 
16957c478bd9Sstevel@tonic-gate static void
1696f4b3ec61Sdh sctp_rq_tq_fini(sctp_stack_t *sctps)
16977c478bd9Sstevel@tonic-gate {
16987c478bd9Sstevel@tonic-gate 	int i;
16997c478bd9Sstevel@tonic-gate 
1700f4b3ec61Sdh 	for (i = 0; i < sctps->sctps_recvq_tq_list_cur_sz; i++) {
1701f4b3ec61Sdh 		ASSERT(sctps->sctps_recvq_tq_list[i] != NULL);
1702f4b3ec61Sdh 		taskq_destroy(sctps->sctps_recvq_tq_list[i]);
17037c478bd9Sstevel@tonic-gate 	}
1704f4b3ec61Sdh 	kmem_free(sctps->sctps_recvq_tq_list,
1705f4b3ec61Sdh 	    sctps->sctps_recvq_tq_list_max_sz * sizeof (taskq_t *));
1706f4b3ec61Sdh 	sctps->sctps_recvq_tq_list = NULL;
17077c478bd9Sstevel@tonic-gate }
17087c478bd9Sstevel@tonic-gate 
17097c478bd9Sstevel@tonic-gate /* Add another taskq for a new ill. */
17107c478bd9Sstevel@tonic-gate void
1711f4b3ec61Sdh sctp_inc_taskq(sctp_stack_t *sctps)
17127c478bd9Sstevel@tonic-gate {
17137c478bd9Sstevel@tonic-gate 	taskq_t *tq;
17147c478bd9Sstevel@tonic-gate 	char tq_name[TASKQ_NAMELEN];
17157c478bd9Sstevel@tonic-gate 
1716f4b3ec61Sdh 	mutex_enter(&sctps->sctps_rq_tq_lock);
1717f4b3ec61Sdh 	if (sctps->sctps_recvq_tq_list_cur_sz + 1 >
1718f4b3ec61Sdh 	    sctps->sctps_recvq_tq_list_max_sz) {
1719f4b3ec61Sdh 		mutex_exit(&sctps->sctps_rq_tq_lock);
17207c478bd9Sstevel@tonic-gate 		cmn_err(CE_NOTE, "Cannot create more SCTP recvq taskq");
17217c478bd9Sstevel@tonic-gate 		return;
17227c478bd9Sstevel@tonic-gate 	}
17237c478bd9Sstevel@tonic-gate 
17247c478bd9Sstevel@tonic-gate 	(void) snprintf(tq_name, sizeof (tq_name), "sctp_recvq_taskq_%u",
1725f4b3ec61Sdh 	    sctps->sctps_recvq_tq_list_cur_sz);
17267c478bd9Sstevel@tonic-gate 	tq = taskq_create(tq_name,
17277c478bd9Sstevel@tonic-gate 	    MIN(sctp_recvq_tq_thr_max, MAX(sctp_recvq_tq_thr_min, ncpus)),
17287c478bd9Sstevel@tonic-gate 	    minclsyspri, sctp_recvq_tq_task_min, sctp_recvq_tq_task_max,
17297c478bd9Sstevel@tonic-gate 	    TASKQ_PREPOPULATE);
17307c478bd9Sstevel@tonic-gate 	if (tq == NULL) {
1731f4b3ec61Sdh 		mutex_exit(&sctps->sctps_rq_tq_lock);
17327c478bd9Sstevel@tonic-gate 		cmn_err(CE_NOTE, "SCTP recvq taskq creation failed");
17337c478bd9Sstevel@tonic-gate 		return;
17347c478bd9Sstevel@tonic-gate 	}
1735f4b3ec61Sdh 	ASSERT(sctps->sctps_recvq_tq_list[
1736b34b8d1aSkcpoon 	    sctps->sctps_recvq_tq_list_cur_sz] == NULL);
1737f4b3ec61Sdh 	sctps->sctps_recvq_tq_list[sctps->sctps_recvq_tq_list_cur_sz] = tq;
1738f4b3ec61Sdh 	atomic_add_32(&sctps->sctps_recvq_tq_list_cur_sz, 1);
1739f4b3ec61Sdh 	mutex_exit(&sctps->sctps_rq_tq_lock);
17407c478bd9Sstevel@tonic-gate }
17417c478bd9Sstevel@tonic-gate 
17427c478bd9Sstevel@tonic-gate #ifdef DEBUG
17437c478bd9Sstevel@tonic-gate uint32_t recvq_loop_cnt = 0;
17447c478bd9Sstevel@tonic-gate uint32_t recvq_call = 0;
17457c478bd9Sstevel@tonic-gate #endif
17467c478bd9Sstevel@tonic-gate 
17477c478bd9Sstevel@tonic-gate /*
17487c478bd9Sstevel@tonic-gate  * Find the next recvq_tq to use.  This routine will go thru all the
17497c478bd9Sstevel@tonic-gate  * taskqs until it can dispatch a job for the sctp.  If this fails,
17507c478bd9Sstevel@tonic-gate  * it will create a new taskq and try it.
17517c478bd9Sstevel@tonic-gate  */
17527c478bd9Sstevel@tonic-gate static boolean_t
17537c478bd9Sstevel@tonic-gate sctp_find_next_tq(sctp_t *sctp)
17547c478bd9Sstevel@tonic-gate {
17557c478bd9Sstevel@tonic-gate 	int next_tq, try;
17567c478bd9Sstevel@tonic-gate 	taskq_t *tq;
1757f4b3ec61Sdh 	sctp_stack_t	*sctps = sctp->sctp_sctps;
17587c478bd9Sstevel@tonic-gate 
17597c478bd9Sstevel@tonic-gate 	/*
17607c478bd9Sstevel@tonic-gate 	 * Note that since we don't hold a lock on sctp_rq_tq_lock for
17617c478bd9Sstevel@tonic-gate 	 * performance reason, recvq_ta_list_cur_sz can be changed during
17627c478bd9Sstevel@tonic-gate 	 * this loop.  The problem this will create is that the loop may
17637c478bd9Sstevel@tonic-gate 	 * not have tried all the recvq_tq.  This should be OK.
17647c478bd9Sstevel@tonic-gate 	 */
1765f4b3ec61Sdh 	next_tq = atomic_add_32_nv(&sctps->sctps_recvq_tq_list_cur, 1) %
1766f4b3ec61Sdh 	    sctps->sctps_recvq_tq_list_cur_sz;
1767f4b3ec61Sdh 	for (try = 0; try < sctps->sctps_recvq_tq_list_cur_sz; try++) {
1768f4b3ec61Sdh 		tq = sctps->sctps_recvq_tq_list[next_tq];
17697c478bd9Sstevel@tonic-gate 		if (taskq_dispatch(tq, sctp_process_recvq, sctp,
17707c478bd9Sstevel@tonic-gate 		    TQ_NOSLEEP) != NULL) {
17717c478bd9Sstevel@tonic-gate 			sctp->sctp_recvq_tq = tq;
17727c478bd9Sstevel@tonic-gate 			return (B_TRUE);
17737c478bd9Sstevel@tonic-gate 		}
1774f4b3ec61Sdh 		next_tq = (next_tq + 1) % sctps->sctps_recvq_tq_list_cur_sz;
17757c478bd9Sstevel@tonic-gate 	}
17767c478bd9Sstevel@tonic-gate 
17777c478bd9Sstevel@tonic-gate 	/*
17787c478bd9Sstevel@tonic-gate 	 * Create one more taskq and try it.  Note that sctp_inc_taskq()
17797c478bd9Sstevel@tonic-gate 	 * may not have created another taskq if the number of recvq
17807c478bd9Sstevel@tonic-gate 	 * taskqs is at the maximum.  We are probably in a pretty bad
17817c478bd9Sstevel@tonic-gate 	 * shape if this actually happens...
17827c478bd9Sstevel@tonic-gate 	 */
1783f4b3ec61Sdh 	sctp_inc_taskq(sctps);
1784f4b3ec61Sdh 	tq = sctps->sctps_recvq_tq_list[sctps->sctps_recvq_tq_list_cur_sz - 1];
17857c478bd9Sstevel@tonic-gate 	if (taskq_dispatch(tq, sctp_process_recvq, sctp, TQ_NOSLEEP) != NULL) {
17867c478bd9Sstevel@tonic-gate 		sctp->sctp_recvq_tq = tq;
17877c478bd9Sstevel@tonic-gate 		return (B_TRUE);
17887c478bd9Sstevel@tonic-gate 	}
1789f4b3ec61Sdh 	SCTP_KSTAT(sctps, sctp_find_next_tq);
17907c478bd9Sstevel@tonic-gate 	return (B_FALSE);
17917c478bd9Sstevel@tonic-gate }
17927c478bd9Sstevel@tonic-gate 
17937c478bd9Sstevel@tonic-gate /*
17947c478bd9Sstevel@tonic-gate  * To add a message to the recvq.  Note that the sctp_timer_fire()
17957c478bd9Sstevel@tonic-gate  * routine also uses this function to add the timer message to the
17967c478bd9Sstevel@tonic-gate  * receive queue for later processing.  And it should be the only
17977c478bd9Sstevel@tonic-gate  * caller of sctp_add_recvq() which sets the try_harder argument
17987c478bd9Sstevel@tonic-gate  * to B_TRUE.
17997c478bd9Sstevel@tonic-gate  *
18007c478bd9Sstevel@tonic-gate  * If the try_harder argument is B_TRUE, this routine sctp_find_next_tq()
18017c478bd9Sstevel@tonic-gate  * will try very hard to dispatch the task.  Refer to the comment
18027c478bd9Sstevel@tonic-gate  * for that routine on how it does that.
1803bd670b35SErik Nordmark  *
1804bd670b35SErik Nordmark  * On failure the message has been freed i.e., this routine always consumes the
1805bd670b35SErik Nordmark  * message. It bumps ipIfStatsInDiscards and and uses ip_drop_input to drop.
18067c478bd9Sstevel@tonic-gate  */
1807bd670b35SErik Nordmark void
1808bd670b35SErik Nordmark sctp_add_recvq(sctp_t *sctp, mblk_t *mp, boolean_t caller_hold_lock,
1809bd670b35SErik Nordmark     ip_recv_attr_t *ira)
18107c478bd9Sstevel@tonic-gate {
1811bd670b35SErik Nordmark 	mblk_t	*attrmp;
1812bd670b35SErik Nordmark 	ip_stack_t	*ipst = sctp->sctp_sctps->sctps_netstack->netstack_ip;
1813bd670b35SErik Nordmark 
1814bd670b35SErik Nordmark 	ASSERT(ira->ira_ill == NULL);
1815bd670b35SErik Nordmark 
18167c478bd9Sstevel@tonic-gate 	if (!caller_hold_lock)
18177c478bd9Sstevel@tonic-gate 		mutex_enter(&sctp->sctp_recvq_lock);
18187c478bd9Sstevel@tonic-gate 
18197c478bd9Sstevel@tonic-gate 	/* If the taskq dispatch has not been scheduled, do it now. */
18207c478bd9Sstevel@tonic-gate 	if (sctp->sctp_recvq_tq == NULL) {
18217c478bd9Sstevel@tonic-gate 		ASSERT(sctp->sctp_recvq == NULL);
18227c478bd9Sstevel@tonic-gate 		if (!sctp_find_next_tq(sctp)) {
18237c478bd9Sstevel@tonic-gate 			if (!caller_hold_lock)
18247c478bd9Sstevel@tonic-gate 				mutex_exit(&sctp->sctp_recvq_lock);
1825bd670b35SErik Nordmark 			BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsInDiscards);
1826bd670b35SErik Nordmark 			ip_drop_input("ipIfStatsInDiscards", mp, NULL);
1827bd670b35SErik Nordmark 			freemsg(mp);
1828bd670b35SErik Nordmark 			return;
18297c478bd9Sstevel@tonic-gate 		}
18307c478bd9Sstevel@tonic-gate 		/* Make sure the sctp_t will not go away. */
18317c478bd9Sstevel@tonic-gate 		SCTP_REFHOLD(sctp);
18327c478bd9Sstevel@tonic-gate 	}
18337c478bd9Sstevel@tonic-gate 
1834bd670b35SErik Nordmark 	attrmp = ip_recv_attr_to_mblk(ira);
1835bd670b35SErik Nordmark 	if (attrmp == NULL) {
1836bd670b35SErik Nordmark 		if (!caller_hold_lock)
1837bd670b35SErik Nordmark 			mutex_exit(&sctp->sctp_recvq_lock);
1838bd670b35SErik Nordmark 		BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsInDiscards);
1839bd670b35SErik Nordmark 		ip_drop_input("ipIfStatsInDiscards", mp, NULL);
1840bd670b35SErik Nordmark 		freemsg(mp);
1841bd670b35SErik Nordmark 		return;
1842bd670b35SErik Nordmark 	}
1843bd670b35SErik Nordmark 	ASSERT(attrmp->b_cont == NULL);
1844bd670b35SErik Nordmark 	attrmp->b_cont = mp;
1845bd670b35SErik Nordmark 	mp = attrmp;
1846bd670b35SErik Nordmark 
18477c478bd9Sstevel@tonic-gate 	if (sctp->sctp_recvq == NULL) {
18487c478bd9Sstevel@tonic-gate 		sctp->sctp_recvq = mp;
18497c478bd9Sstevel@tonic-gate 		sctp->sctp_recvq_tail = mp;
18507c478bd9Sstevel@tonic-gate 	} else {
18517c478bd9Sstevel@tonic-gate 		sctp->sctp_recvq_tail->b_next = mp;
18527c478bd9Sstevel@tonic-gate 		sctp->sctp_recvq_tail = mp;
18537c478bd9Sstevel@tonic-gate 	}
18547c478bd9Sstevel@tonic-gate 
18557c478bd9Sstevel@tonic-gate 	if (!caller_hold_lock)
18567c478bd9Sstevel@tonic-gate 		mutex_exit(&sctp->sctp_recvq_lock);
18577c478bd9Sstevel@tonic-gate }
18587c478bd9Sstevel@tonic-gate 
18597c478bd9Sstevel@tonic-gate static void
18607c478bd9Sstevel@tonic-gate sctp_process_recvq(void *arg)
18617c478bd9Sstevel@tonic-gate {
18627c478bd9Sstevel@tonic-gate 	sctp_t		*sctp = (sctp_t *)arg;
18637c478bd9Sstevel@tonic-gate 	mblk_t		*mp;
18647c478bd9Sstevel@tonic-gate #ifdef DEBUG
18657c478bd9Sstevel@tonic-gate 	uint32_t	loop_cnt = 0;
18667c478bd9Sstevel@tonic-gate #endif
1867bd670b35SErik Nordmark 	ip_recv_attr_t	iras;
18687c478bd9Sstevel@tonic-gate 
18697c478bd9Sstevel@tonic-gate #ifdef	_BIG_ENDIAN
18707c478bd9Sstevel@tonic-gate #define	IPVER(ip6h)	((((uint32_t *)ip6h)[0] >> 28) & 0x7)
18717c478bd9Sstevel@tonic-gate #else
18727c478bd9Sstevel@tonic-gate #define	IPVER(ip6h)	((((uint32_t *)ip6h)[0] >> 4) & 0x7)
18737c478bd9Sstevel@tonic-gate #endif
18747c478bd9Sstevel@tonic-gate 
18757c478bd9Sstevel@tonic-gate 	RUN_SCTP(sctp);
18767c478bd9Sstevel@tonic-gate 	mutex_enter(&sctp->sctp_recvq_lock);
18777c478bd9Sstevel@tonic-gate 
18787c478bd9Sstevel@tonic-gate #ifdef DEBUG
18797c478bd9Sstevel@tonic-gate 	recvq_call++;
18807c478bd9Sstevel@tonic-gate #endif
18817c478bd9Sstevel@tonic-gate 	/*
18827c478bd9Sstevel@tonic-gate 	 * Note that while we are in this loop, other thread can put
18837c478bd9Sstevel@tonic-gate 	 * new packets in the receive queue.  We may be looping for
18847c478bd9Sstevel@tonic-gate 	 * quite a while.
18857c478bd9Sstevel@tonic-gate 	 */
18867c478bd9Sstevel@tonic-gate 	while ((mp = sctp->sctp_recvq) != NULL) {
1887bd670b35SErik Nordmark 		mblk_t *data_mp;
1888bd670b35SErik Nordmark 
18897c478bd9Sstevel@tonic-gate 		sctp->sctp_recvq = mp->b_next;
18907c478bd9Sstevel@tonic-gate 		mutex_exit(&sctp->sctp_recvq_lock);
18917c478bd9Sstevel@tonic-gate 		mp->b_next = NULL;
18927c478bd9Sstevel@tonic-gate #ifdef DEBUG
18937c478bd9Sstevel@tonic-gate 		loop_cnt++;
18947c478bd9Sstevel@tonic-gate #endif
18957c478bd9Sstevel@tonic-gate 		mp->b_prev = NULL;
18967c478bd9Sstevel@tonic-gate 
1897bd670b35SErik Nordmark 		data_mp = mp->b_cont;
1898bd670b35SErik Nordmark 		mp->b_cont = NULL;
1899bd670b35SErik Nordmark 		if (!ip_recv_attr_from_mblk(mp, &iras)) {
1900bd670b35SErik Nordmark 			ip_drop_input("ip_recv_attr_from_mblk", mp, NULL);
1901bd670b35SErik Nordmark 			freemsg(mp);
1902bd670b35SErik Nordmark 			ira_cleanup(&iras, B_TRUE);
1903bd670b35SErik Nordmark 			continue;
1904bd670b35SErik Nordmark 		}
1905bd670b35SErik Nordmark 
1906bd670b35SErik Nordmark 		if (iras.ira_flags & IRAF_ICMP_ERROR)
1907bd670b35SErik Nordmark 			sctp_icmp_error(sctp, data_mp);
1908bd670b35SErik Nordmark 		else
1909bd670b35SErik Nordmark 			sctp_input_data(sctp, data_mp, &iras);
1910bd670b35SErik Nordmark 
1911bd670b35SErik Nordmark 		ira_cleanup(&iras, B_TRUE);
19127c478bd9Sstevel@tonic-gate 		mutex_enter(&sctp->sctp_recvq_lock);
19137c478bd9Sstevel@tonic-gate 	}
19147c478bd9Sstevel@tonic-gate 
19157c478bd9Sstevel@tonic-gate 	sctp->sctp_recvq_tail = NULL;
19167c478bd9Sstevel@tonic-gate 	sctp->sctp_recvq_tq = NULL;
19177c478bd9Sstevel@tonic-gate 
19187c478bd9Sstevel@tonic-gate 	mutex_exit(&sctp->sctp_recvq_lock);
19197c478bd9Sstevel@tonic-gate 
19207c478bd9Sstevel@tonic-gate 	WAKE_SCTP(sctp);
19217c478bd9Sstevel@tonic-gate 
19227c478bd9Sstevel@tonic-gate #ifdef DEBUG
19237c478bd9Sstevel@tonic-gate 	if (loop_cnt > recvq_loop_cnt)
19247c478bd9Sstevel@tonic-gate 		recvq_loop_cnt = loop_cnt;
19257c478bd9Sstevel@tonic-gate #endif
19267c478bd9Sstevel@tonic-gate 	/* Now it can go away. */
19277c478bd9Sstevel@tonic-gate 	SCTP_REFRELE(sctp);
19287c478bd9Sstevel@tonic-gate }
19297c478bd9Sstevel@tonic-gate 
19307c478bd9Sstevel@tonic-gate /* ARGSUSED */
19317c478bd9Sstevel@tonic-gate static int
19327c478bd9Sstevel@tonic-gate sctp_conn_cache_constructor(void *buf, void *cdrarg, int kmflags)
19337c478bd9Sstevel@tonic-gate {
1934bd670b35SErik Nordmark 	conn_t	*connp = (conn_t *)buf;
1935bd670b35SErik Nordmark 	sctp_t	*sctp = (sctp_t *)&connp[1];
1936*92baa190SGeorge Shepherd 	int	cnt;
19377c478bd9Sstevel@tonic-gate 
1938bd670b35SErik Nordmark 	bzero(connp, sizeof (conn_t));
19397c478bd9Sstevel@tonic-gate 	bzero(buf, (char *)&sctp[1] - (char *)buf);
19407c478bd9Sstevel@tonic-gate 
19417c478bd9Sstevel@tonic-gate 	mutex_init(&sctp->sctp_reflock, NULL, MUTEX_DEFAULT, NULL);
19427c478bd9Sstevel@tonic-gate 	mutex_init(&sctp->sctp_lock, NULL, MUTEX_DEFAULT, NULL);
19437c478bd9Sstevel@tonic-gate 	mutex_init(&sctp->sctp_recvq_lock, NULL, MUTEX_DEFAULT, NULL);
19447c478bd9Sstevel@tonic-gate 	cv_init(&sctp->sctp_cv, NULL, CV_DEFAULT, NULL);
1945*92baa190SGeorge Shepherd 	for (cnt = 0; cnt < SCTP_IPIF_HASH; cnt++) {
1946*92baa190SGeorge Shepherd 		rw_init(&sctp->sctp_saddrs[cnt].ipif_hash_lock, NULL,
1947*92baa190SGeorge Shepherd 		    RW_DEFAULT, NULL);
1948*92baa190SGeorge Shepherd 	}
19497c478bd9Sstevel@tonic-gate 
1950bd670b35SErik Nordmark 	mutex_init(&connp->conn_lock, NULL, MUTEX_DEFAULT, NULL);
1951bd670b35SErik Nordmark 	cv_init(&connp->conn_cv, NULL, CV_DEFAULT, NULL);
1952bd670b35SErik Nordmark 	connp->conn_flags = IPCL_SCTPCONN;
1953bd670b35SErik Nordmark 	connp->conn_proto = IPPROTO_SCTP;
1954bd670b35SErik Nordmark 	connp->conn_sctp = sctp;
1955bd670b35SErik Nordmark 	sctp->sctp_connp = connp;
1956bd670b35SErik Nordmark 	rw_init(&connp->conn_ilg_lock, NULL, RW_DEFAULT, NULL);
1957bd670b35SErik Nordmark 
1958bd670b35SErik Nordmark 	connp->conn_ixa = kmem_zalloc(sizeof (ip_xmit_attr_t), kmflags);
1959bd670b35SErik Nordmark 	if (connp->conn_ixa == NULL) {
1960bd670b35SErik Nordmark 		return (ENOMEM);
1961bd670b35SErik Nordmark 	}
1962bd670b35SErik Nordmark 	connp->conn_ixa->ixa_refcnt = 1;
1963bd670b35SErik Nordmark 	connp->conn_ixa->ixa_protocol = connp->conn_proto;
1964bd670b35SErik Nordmark 	connp->conn_ixa->ixa_xmit_hint = CONN_TO_XMIT_HINT(connp);
19657c478bd9Sstevel@tonic-gate 	return (0);
19667c478bd9Sstevel@tonic-gate }
19677c478bd9Sstevel@tonic-gate 
19687c478bd9Sstevel@tonic-gate /* ARGSUSED */
19697c478bd9Sstevel@tonic-gate static void
19707c478bd9Sstevel@tonic-gate sctp_conn_cache_destructor(void *buf, void *cdrarg)
19717c478bd9Sstevel@tonic-gate {
1972bd670b35SErik Nordmark 	conn_t	*connp = (conn_t *)buf;
1973bd670b35SErik Nordmark 	sctp_t	*sctp = (sctp_t *)&connp[1];
1974*92baa190SGeorge Shepherd 	int	cnt;
19757c478bd9Sstevel@tonic-gate 
1976bd670b35SErik Nordmark 	ASSERT(sctp->sctp_connp == connp);
19777c478bd9Sstevel@tonic-gate 	ASSERT(!MUTEX_HELD(&sctp->sctp_lock));
19787c478bd9Sstevel@tonic-gate 	ASSERT(!MUTEX_HELD(&sctp->sctp_reflock));
19797c478bd9Sstevel@tonic-gate 	ASSERT(!MUTEX_HELD(&sctp->sctp_recvq_lock));
19807c478bd9Sstevel@tonic-gate 
19817c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_conn_hash_next == NULL);
19827c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_conn_hash_prev == NULL);
19837c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_listen_hash_next == NULL);
19847c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_listen_hash_prev == NULL);
19857c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_listen_tfp == NULL);
19867c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_conn_tfp == NULL);
19877c478bd9Sstevel@tonic-gate 
19887c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_faddrs == NULL);
19897c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_nsaddrs == 0);
19907c478bd9Sstevel@tonic-gate 
19917c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_ulpd == NULL);
19927c478bd9Sstevel@tonic-gate 
19937c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_lastfaddr == NULL);
19947c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_primary == NULL);
19957c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_current == NULL);
19967c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_lastdata == NULL);
19977c478bd9Sstevel@tonic-gate 
19987c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_xmit_head == NULL);
19997c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_xmit_tail == NULL);
20007c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_xmit_unsent == NULL);
20017c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_xmit_unsent_tail == NULL);
20027c478bd9Sstevel@tonic-gate 
20037c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_ostrcntrs == NULL);
20047c478bd9Sstevel@tonic-gate 
20057c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_sack_info == NULL);
20067c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_ack_mp == NULL);
20077c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_instr == NULL);
20087c478bd9Sstevel@tonic-gate 
20097c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_iphc == NULL);
20107c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_iphc6 == NULL);
20117c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_ipha == NULL);
20127c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_ip6h == NULL);
20137c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_sctph == NULL);
20147c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_sctph6 == NULL);
20157c478bd9Sstevel@tonic-gate 
20167c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_cookie_mp == NULL);
20177c478bd9Sstevel@tonic-gate 
20187c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_refcnt == 0);
20197c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_timer_mp == NULL);
20207c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_connp->conn_ref == 0);
20217c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_heartbeat_mp == NULL);
20227c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_ptpbhn == NULL && sctp->sctp_bind_hash == NULL);
20237c478bd9Sstevel@tonic-gate 
20247c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_shutdown_faddr == NULL);
20257c478bd9Sstevel@tonic-gate 
20267c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_cxmit_list == NULL);
20277c478bd9Sstevel@tonic-gate 
20287c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_recvq == NULL);
20297c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_recvq_tail == NULL);
20307c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_recvq_tq == NULL);
20317c478bd9Sstevel@tonic-gate 
2032121e5416Skcpoon 	/*
2033121e5416Skcpoon 	 * sctp_pad_mp can be NULL if the memory allocation fails
2034121e5416Skcpoon 	 * in sctp_init_values() and the conn_t is freed.
2035121e5416Skcpoon 	 */
2036121e5416Skcpoon 	if (sctp->sctp_pad_mp != NULL) {
2037121e5416Skcpoon 		freeb(sctp->sctp_pad_mp);
2038121e5416Skcpoon 		sctp->sctp_pad_mp = NULL;
2039121e5416Skcpoon 	}
2040121e5416Skcpoon 
20417c478bd9Sstevel@tonic-gate 	mutex_destroy(&sctp->sctp_reflock);
20427c478bd9Sstevel@tonic-gate 	mutex_destroy(&sctp->sctp_lock);
20437c478bd9Sstevel@tonic-gate 	mutex_destroy(&sctp->sctp_recvq_lock);
20447c478bd9Sstevel@tonic-gate 	cv_destroy(&sctp->sctp_cv);
2045*92baa190SGeorge Shepherd 	for (cnt = 0; cnt < SCTP_IPIF_HASH; cnt++) {
2046*92baa190SGeorge Shepherd 		rw_destroy(&sctp->sctp_saddrs[cnt].ipif_hash_lock);
2047*92baa190SGeorge Shepherd 	}
20487c478bd9Sstevel@tonic-gate 
2049bd670b35SErik Nordmark 	mutex_destroy(&connp->conn_lock);
2050bd670b35SErik Nordmark 	cv_destroy(&connp->conn_cv);
2051bd670b35SErik Nordmark 	rw_destroy(&connp->conn_ilg_lock);
2052bd670b35SErik Nordmark 
2053bd670b35SErik Nordmark 	/* Can be NULL if constructor failed */
2054bd670b35SErik Nordmark 	if (connp->conn_ixa != NULL) {
2055bd670b35SErik Nordmark 		ASSERT(connp->conn_ixa->ixa_refcnt == 1);
2056bd670b35SErik Nordmark 		ASSERT(connp->conn_ixa->ixa_ire == NULL);
2057bd670b35SErik Nordmark 		ASSERT(connp->conn_ixa->ixa_nce == NULL);
2058bd670b35SErik Nordmark 		ixa_refrele(connp->conn_ixa);
2059bd670b35SErik Nordmark 	}
20607c478bd9Sstevel@tonic-gate }
20617c478bd9Sstevel@tonic-gate 
20627c478bd9Sstevel@tonic-gate static void
20637c478bd9Sstevel@tonic-gate sctp_conn_cache_init()
20647c478bd9Sstevel@tonic-gate {
20657c478bd9Sstevel@tonic-gate 	sctp_conn_cache = kmem_cache_create("sctp_conn_cache",
20667c478bd9Sstevel@tonic-gate 	    sizeof (sctp_t) + sizeof (conn_t), 0, sctp_conn_cache_constructor,
20677c478bd9Sstevel@tonic-gate 	    sctp_conn_cache_destructor, NULL, NULL, NULL, 0);
20687c478bd9Sstevel@tonic-gate }
20697c478bd9Sstevel@tonic-gate 
20707c478bd9Sstevel@tonic-gate static void
20717c478bd9Sstevel@tonic-gate sctp_conn_cache_fini()
20727c478bd9Sstevel@tonic-gate {
20737c478bd9Sstevel@tonic-gate 	kmem_cache_destroy(sctp_conn_cache);
20747c478bd9Sstevel@tonic-gate }
2075121e5416Skcpoon 
2076121e5416Skcpoon void
2077121e5416Skcpoon sctp_conn_init(conn_t *connp)
2078121e5416Skcpoon {
2079bd670b35SErik Nordmark 	ASSERT(connp->conn_flags == IPCL_SCTPCONN);
2080121e5416Skcpoon 	connp->conn_rq = connp->conn_wq = NULL;
2081bd670b35SErik Nordmark 	connp->conn_ixa->ixa_flags |= IXAF_SET_ULP_CKSUM | IXAF_VERIFY_SOURCE |
2082bd670b35SErik Nordmark 	    IXAF_VERIFY_PMTU;
2083bd670b35SErik Nordmark 
2084bd670b35SErik Nordmark 	ASSERT(connp->conn_proto == IPPROTO_SCTP);
2085bd670b35SErik Nordmark 	ASSERT(connp->conn_ixa->ixa_protocol == connp->conn_proto);
2086121e5416Skcpoon 	connp->conn_state_flags |= CONN_INCIPIENT;
2087bd670b35SErik Nordmark 
2088bd670b35SErik Nordmark 	ASSERT(connp->conn_sctp != NULL);
2089bd670b35SErik Nordmark 
2090bd670b35SErik Nordmark 	/*
2091bd670b35SErik Nordmark 	 * Register sctp_notify to listen to capability changes detected by IP.
2092bd670b35SErik Nordmark 	 * This upcall is made in the context of the call to conn_ip_output
2093bd670b35SErik Nordmark 	 * thus it holds whatever locks sctp holds across conn_ip_output.
2094bd670b35SErik Nordmark 	 */
2095bd670b35SErik Nordmark 	connp->conn_ixa->ixa_notify = sctp_notify;
2096bd670b35SErik Nordmark 	connp->conn_ixa->ixa_notify_cookie = connp->conn_sctp;
2097121e5416Skcpoon }
2098121e5416Skcpoon 
2099121e5416Skcpoon static void
2100121e5416Skcpoon sctp_conn_clear(conn_t *connp)
2101121e5416Skcpoon {
2102121e5416Skcpoon 	/* Clean up conn_t stuff */
2103bd670b35SErik Nordmark 	if (connp->conn_latch != NULL) {
2104bd670b35SErik Nordmark 		IPLATCH_REFRELE(connp->conn_latch);
2105bd670b35SErik Nordmark 		connp->conn_latch = NULL;
2106bd670b35SErik Nordmark 	}
2107bd670b35SErik Nordmark 	if (connp->conn_latch_in_policy != NULL) {
2108bd670b35SErik Nordmark 		IPPOL_REFRELE(connp->conn_latch_in_policy);
2109bd670b35SErik Nordmark 		connp->conn_latch_in_policy = NULL;
2110bd670b35SErik Nordmark 	}
2111bd670b35SErik Nordmark 	if (connp->conn_latch_in_action != NULL) {
2112bd670b35SErik Nordmark 		IPACT_REFRELE(connp->conn_latch_in_action);
2113bd670b35SErik Nordmark 		connp->conn_latch_in_action = NULL;
2114bd670b35SErik Nordmark 	}
2115bd670b35SErik Nordmark 	if (connp->conn_policy != NULL) {
2116121e5416Skcpoon 		IPPH_REFRELE(connp->conn_policy, connp->conn_netstack);
2117bd670b35SErik Nordmark 		connp->conn_policy = NULL;
2118bd670b35SErik Nordmark 	}
2119bd670b35SErik Nordmark 	if (connp->conn_ipsec_opt_mp != NULL) {
2120121e5416Skcpoon 		freemsg(connp->conn_ipsec_opt_mp);
2121bd670b35SErik Nordmark 		connp->conn_ipsec_opt_mp = NULL;
2122bd670b35SErik Nordmark 	}
2123121e5416Skcpoon 	netstack_rele(connp->conn_netstack);
2124bd670b35SErik Nordmark 	connp->conn_netstack = NULL;
2125bd670b35SErik Nordmark 
2126bd670b35SErik Nordmark 	/* Leave conn_ixa and other constructed fields in place */
2127bd670b35SErik Nordmark 	ipcl_conn_cleanup(connp);
2128121e5416Skcpoon }
2129