xref: /illumos-gate/usr/src/uts/common/inet/sctp/sctp.c (revision 5dddb8ba)
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
5*5dddb8baSkcpoon  * Common Development and Distribution License (the "License").
6*5dddb8baSkcpoon  * 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  */
21*5dddb8baSkcpoon 
227c478bd9Sstevel@tonic-gate /*
23*5dddb8baSkcpoon  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #include <sys/types.h>
307c478bd9Sstevel@tonic-gate #include <sys/stream.h>
317c478bd9Sstevel@tonic-gate #include <sys/strsubr.h>
327c478bd9Sstevel@tonic-gate #include <sys/stropts.h>
337c478bd9Sstevel@tonic-gate #include <sys/strsun.h>
347c478bd9Sstevel@tonic-gate #define	_SUN_TPI_VERSION 2
357c478bd9Sstevel@tonic-gate #include <sys/tihdr.h>
367c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
377c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
387c478bd9Sstevel@tonic-gate #include <sys/xti_inet.h>
397c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
407c478bd9Sstevel@tonic-gate #include <sys/debug.h>
417c478bd9Sstevel@tonic-gate #include <sys/vtrace.h>
427c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
437c478bd9Sstevel@tonic-gate #include <sys/cpuvar.h>
447c478bd9Sstevel@tonic-gate #include <sys/random.h>
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>
597c478bd9Sstevel@tonic-gate #include <inet/ip6.h>
607c478bd9Sstevel@tonic-gate #include <inet/mi.h>
617c478bd9Sstevel@tonic-gate #include <inet/mib2.h>
627c478bd9Sstevel@tonic-gate #include <inet/nd.h>
637c478bd9Sstevel@tonic-gate #include <inet/optcom.h>
647c478bd9Sstevel@tonic-gate #include <inet/ipclassifier.h>
657c478bd9Sstevel@tonic-gate #include <inet/ipsec_impl.h>
667c478bd9Sstevel@tonic-gate #include <inet/sctp_ip.h>
677c478bd9Sstevel@tonic-gate #include <inet/sctp_crc32.h>
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate #include "sctp_impl.h"
707c478bd9Sstevel@tonic-gate #include "sctp_addr.h"
711d8c4025Svi #include "sctp_asconf.h"
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate extern major_t SCTP6_MAJ;
747c478bd9Sstevel@tonic-gate extern major_t SCTP_MAJ;
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 extern mblk_t *sctp_pad_mp;	/* pad unaligned data chunks */
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate static void	sctp_closei_local(sctp_t *sctp);
827c478bd9Sstevel@tonic-gate static int	sctp_init_values(sctp_t *, sctp_t *, int);
837c478bd9Sstevel@tonic-gate void		sctp_display_all();
847c478bd9Sstevel@tonic-gate static void	sctp_icmp_error_ipv6(sctp_t *sctp, mblk_t *mp);
857c478bd9Sstevel@tonic-gate static void	sctp_process_recvq(void *);
867c478bd9Sstevel@tonic-gate static void	sctp_rq_tq_init(void);
877c478bd9Sstevel@tonic-gate static void	sctp_rq_tq_fini(void);
887c478bd9Sstevel@tonic-gate static void	sctp_conn_cache_init();
897c478bd9Sstevel@tonic-gate static void	sctp_conn_cache_fini();
907c478bd9Sstevel@tonic-gate static int	sctp_conn_cache_constructor();
917c478bd9Sstevel@tonic-gate static void	sctp_conn_cache_destructor();
927c478bd9Sstevel@tonic-gate void		sctp_inc_taskq(void);
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate /*
957c478bd9Sstevel@tonic-gate  * SCTP receive queue taskq
967c478bd9Sstevel@tonic-gate  *
977c478bd9Sstevel@tonic-gate  * At SCTP initialization time, a default taskq is created for
987c478bd9Sstevel@tonic-gate  * servicing packets received when the interrupt thread cannot
997c478bd9Sstevel@tonic-gate  * get a hold on the sctp_t.  The number of taskq can be increased in
1007c478bd9Sstevel@tonic-gate  * sctp_find_next_tq() when an existing taskq cannot be dispatched.
1017c478bd9Sstevel@tonic-gate  * The taskqs are never removed.  But the max number of taskq which
1027c478bd9Sstevel@tonic-gate  * can be created is controlled by sctp_recvq_tq_list_max_sz.  Note
1037c478bd9Sstevel@tonic-gate  * that SCTP recvq taskq is not tied to any specific CPU or ill.
1047c478bd9Sstevel@tonic-gate  *
1057c478bd9Sstevel@tonic-gate  * Those taskqs are stored in an array recvq_tq_list.  And they are
1067c478bd9Sstevel@tonic-gate  * used in a round robin fashion.  The current taskq being used is
1077c478bd9Sstevel@tonic-gate  * determined by recvq_tq_list_cur.
1087c478bd9Sstevel@tonic-gate  */
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate /* This lock protects the SCTP recvq_tq_list array and recvq_tq_list_cur_sz. */
1117c478bd9Sstevel@tonic-gate static kmutex_t	sctp_rq_tq_lock;
1127c478bd9Sstevel@tonic-gate int		sctp_recvq_tq_list_max_sz = 16;
1137c478bd9Sstevel@tonic-gate static taskq_t	**recvq_tq_list;
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate /* Current number of recvq taskq.  At least 1 for the default taskq. */
1167c478bd9Sstevel@tonic-gate static uint32_t	recvq_tq_list_cur_sz = 1;
1177c478bd9Sstevel@tonic-gate static uint32_t	recvq_tq_list_cur = 0;
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate /* The minimum number of threads for each taskq. */
1207c478bd9Sstevel@tonic-gate int		sctp_recvq_tq_thr_min = 4;
1217c478bd9Sstevel@tonic-gate /* The maximum number of threads for each taskq. */
1227c478bd9Sstevel@tonic-gate int		sctp_recvq_tq_thr_max = 16;
1237c478bd9Sstevel@tonic-gate /* The minimum number of tasks for each taskq. */
1247c478bd9Sstevel@tonic-gate int		sctp_recvq_tq_task_min = 5;
1257c478bd9Sstevel@tonic-gate /* The maxiimum number of tasks for each taskq. */
1267c478bd9Sstevel@tonic-gate int		sctp_recvq_tq_task_max = 50;
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate /*
1297c478bd9Sstevel@tonic-gate  * Default queue used for sending packets.  No need to have lock for it
1307c478bd9Sstevel@tonic-gate  * as it should never be changed.
1317c478bd9Sstevel@tonic-gate  */
1327c478bd9Sstevel@tonic-gate queue_t	*sctp_g_q;
1337c478bd9Sstevel@tonic-gate int sctp_g_q_fd;
1347c478bd9Sstevel@tonic-gate /* The default sctp_t for responding out of the blue packets. */
1357c478bd9Sstevel@tonic-gate sctp_t *gsctp;
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate /* Protected by sctp_g_lock */
1387c478bd9Sstevel@tonic-gate list_t	sctp_g_list;	/* SCTP instance data chain */
1397c478bd9Sstevel@tonic-gate kmutex_t sctp_g_lock;
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate /*  sctp_t/conn_t kmem cache */
1427c478bd9Sstevel@tonic-gate struct kmem_cache	*sctp_conn_cache;
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate #define	SCTP_CONDEMNED(sctp)				\
1457c478bd9Sstevel@tonic-gate 	mutex_enter(&(sctp)->sctp_reflock);		\
1467c478bd9Sstevel@tonic-gate 	((sctp)->sctp_condemned = B_TRUE);		\
147*5dddb8baSkcpoon 	mutex_exit(&(sctp)->sctp_reflock);
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate /* Link/unlink a sctp_t to/from the global list. */
1507c478bd9Sstevel@tonic-gate #define	SCTP_LINK(sctp)					\
1517c478bd9Sstevel@tonic-gate 	mutex_enter(&sctp_g_lock);			\
1527c478bd9Sstevel@tonic-gate 	list_insert_tail(&sctp_g_list, (sctp));		\
1537c478bd9Sstevel@tonic-gate 	mutex_exit(&sctp_g_lock);
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate #define	SCTP_UNLINK(sctp)				\
1567c478bd9Sstevel@tonic-gate 	mutex_enter(&sctp_g_lock);			\
1577c478bd9Sstevel@tonic-gate 	ASSERT((sctp)->sctp_condemned);			\
1587c478bd9Sstevel@tonic-gate 	list_remove(&sctp_g_list, (sctp));		\
1597c478bd9Sstevel@tonic-gate 	mutex_exit(&sctp_g_lock);
1607c478bd9Sstevel@tonic-gate 
1611d8c4025Svi /*
1621d8c4025Svi  * Hooks for Sun Cluster. On non-clustered nodes these will remain NULL.
1631d8c4025Svi  * PSARC/2005/602.
1641d8c4025Svi  */
1651d8c4025Svi void (*cl_sctp_listen)(sa_family_t, uchar_t *, uint_t, in_port_t) = NULL;
1661d8c4025Svi void (*cl_sctp_unlisten)(sa_family_t, uchar_t *, uint_t, in_port_t) = NULL;
1671d8c4025Svi void (*cl_sctp_connect)(sa_family_t, uchar_t *, uint_t, in_port_t,
1681d8c4025Svi     uchar_t *, uint_t, in_port_t, boolean_t, cl_sctp_handle_t) = NULL;
1691d8c4025Svi void (*cl_sctp_disconnect)(sa_family_t, cl_sctp_handle_t) = NULL;
1701d8c4025Svi void (*cl_sctp_assoc_change)(sa_family_t, uchar_t *, size_t, uint_t,
1711d8c4025Svi     uchar_t *, size_t, uint_t, int, cl_sctp_handle_t) = NULL;
1721d8c4025Svi void (*cl_sctp_check_addrs)(sa_family_t, in_port_t, uchar_t **, size_t,
1731d8c4025Svi     uint_t *, boolean_t) = NULL;
1747c478bd9Sstevel@tonic-gate /*
1757c478bd9Sstevel@tonic-gate  * Return the version number of the SCTP kernel interface.
1767c478bd9Sstevel@tonic-gate  */
1777c478bd9Sstevel@tonic-gate int
1787c478bd9Sstevel@tonic-gate sctp_itf_ver(int cl_ver)
1797c478bd9Sstevel@tonic-gate {
1807c478bd9Sstevel@tonic-gate 	if (cl_ver != SCTP_ITF_VER)
1817c478bd9Sstevel@tonic-gate 		return (-1);
1827c478bd9Sstevel@tonic-gate 	return (SCTP_ITF_VER);
1837c478bd9Sstevel@tonic-gate }
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate /*
1867c478bd9Sstevel@tonic-gate  * Called when we need a new sctp instantiation but don't really have a
1877c478bd9Sstevel@tonic-gate  * new q to hang it off of. Copy the priv flag from the passed in structure.
1887c478bd9Sstevel@tonic-gate  */
1897c478bd9Sstevel@tonic-gate sctp_t *
1907c478bd9Sstevel@tonic-gate sctp_create_eager(sctp_t *psctp)
1917c478bd9Sstevel@tonic-gate {
1927c478bd9Sstevel@tonic-gate 	sctp_t	*sctp;
1937c478bd9Sstevel@tonic-gate 	mblk_t	*ack_mp, *hb_mp;
1947c478bd9Sstevel@tonic-gate 	conn_t	*connp, *pconnp;
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate 	if ((connp = ipcl_conn_create(IPCL_SCTPCONN, KM_NOSLEEP)) == NULL)
1977c478bd9Sstevel@tonic-gate 		return (NULL);
1987c478bd9Sstevel@tonic-gate 	sctp = CONN2SCTP(connp);
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate 	if ((ack_mp = sctp_timer_alloc(sctp, sctp_ack_timer)) == NULL ||
2017c478bd9Sstevel@tonic-gate 	    (hb_mp = sctp_timer_alloc(sctp, sctp_heartbeat_timer)) == NULL) {
2027c478bd9Sstevel@tonic-gate 		if (ack_mp != NULL)
2037c478bd9Sstevel@tonic-gate 			freeb(ack_mp);
2047c478bd9Sstevel@tonic-gate 		kmem_cache_free(sctp_conn_cache, connp);
2057c478bd9Sstevel@tonic-gate 		return (NULL);
2067c478bd9Sstevel@tonic-gate 	}
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate 	sctp->sctp_ack_mp = ack_mp;
2097c478bd9Sstevel@tonic-gate 	sctp->sctp_heartbeat_mp = hb_mp;
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate 	/* Inherit information from the "parent" */
2127c478bd9Sstevel@tonic-gate 	sctp->sctp_ipversion = psctp->sctp_ipversion;
2137c478bd9Sstevel@tonic-gate 	sctp->sctp_family = psctp->sctp_family;
2147c478bd9Sstevel@tonic-gate 	pconnp = psctp->sctp_connp;
2157c478bd9Sstevel@tonic-gate 	connp->conn_af_isv6 = pconnp->conn_af_isv6;
2167c478bd9Sstevel@tonic-gate 	connp->conn_pkt_isv6 = pconnp->conn_pkt_isv6;
2177c478bd9Sstevel@tonic-gate 	connp->conn_ipv6_v6only = pconnp->conn_ipv6_v6only;
2187c478bd9Sstevel@tonic-gate 	if (sctp_init_values(sctp, psctp, KM_NOSLEEP) != 0) {
2197c478bd9Sstevel@tonic-gate 		freeb(ack_mp);
2207c478bd9Sstevel@tonic-gate 		freeb(hb_mp);
2217c478bd9Sstevel@tonic-gate 		kmem_cache_free(sctp_conn_cache, connp);
2227c478bd9Sstevel@tonic-gate 		return (NULL);
2237c478bd9Sstevel@tonic-gate 	}
2247c478bd9Sstevel@tonic-gate 	if (pconnp->conn_cred != NULL) {
2257c478bd9Sstevel@tonic-gate 		connp->conn_cred = pconnp->conn_cred;
2267c478bd9Sstevel@tonic-gate 		crhold(connp->conn_cred);
2277c478bd9Sstevel@tonic-gate 	}
2287c478bd9Sstevel@tonic-gate 	connp->conn_zoneid = psctp->sctp_zoneid;
2297c478bd9Sstevel@tonic-gate 	sctp->sctp_mss = psctp->sctp_mss;
2307c478bd9Sstevel@tonic-gate 	sctp->sctp_detached = B_TRUE;
2317c478bd9Sstevel@tonic-gate 	/*
2327c478bd9Sstevel@tonic-gate 	 * Link to the global as soon as possible so that this sctp_t
2337c478bd9Sstevel@tonic-gate 	 * can be found.
2347c478bd9Sstevel@tonic-gate 	 */
2357c478bd9Sstevel@tonic-gate 	SCTP_LINK(sctp);
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate 	return (sctp);
2387c478bd9Sstevel@tonic-gate }
2397c478bd9Sstevel@tonic-gate 
2407c478bd9Sstevel@tonic-gate /*
2417c478bd9Sstevel@tonic-gate  * We are dying for some reason.  Try to do it gracefully.
2427c478bd9Sstevel@tonic-gate  */
2437c478bd9Sstevel@tonic-gate void
2447c478bd9Sstevel@tonic-gate sctp_clean_death(sctp_t *sctp, int err)
2457c478bd9Sstevel@tonic-gate {
2467c478bd9Sstevel@tonic-gate 	ASSERT(sctp != NULL);
2477c478bd9Sstevel@tonic-gate 	ASSERT((sctp->sctp_family == AF_INET &&
2487c478bd9Sstevel@tonic-gate 	    sctp->sctp_ipversion == IPV4_VERSION) ||
2497c478bd9Sstevel@tonic-gate 	    (sctp->sctp_family == AF_INET6 &&
2507c478bd9Sstevel@tonic-gate 	    (sctp->sctp_ipversion == IPV4_VERSION ||
2517c478bd9Sstevel@tonic-gate 	    sctp->sctp_ipversion == IPV6_VERSION)));
2527c478bd9Sstevel@tonic-gate 
2537c478bd9Sstevel@tonic-gate 	dprint(3, ("sctp_clean_death %p, state %d\n", sctp, sctp->sctp_state));
2547c478bd9Sstevel@tonic-gate 
2557c478bd9Sstevel@tonic-gate 	sctp->sctp_client_errno = err;
2567c478bd9Sstevel@tonic-gate 	/*
2577c478bd9Sstevel@tonic-gate 	 * Check to see if we need to notify upper layer.
2587c478bd9Sstevel@tonic-gate 	 */
2597c478bd9Sstevel@tonic-gate 	if ((sctp->sctp_state >= SCTPS_COOKIE_WAIT) &&
2607c478bd9Sstevel@tonic-gate 	    !SCTP_IS_DETACHED(sctp)) {
2617c478bd9Sstevel@tonic-gate 		if (sctp->sctp_xmit_head || sctp->sctp_xmit_unsent) {
2627c478bd9Sstevel@tonic-gate 			sctp_regift_xmitlist(sctp);
2637c478bd9Sstevel@tonic-gate 		}
2647c478bd9Sstevel@tonic-gate 		if (sctp->sctp_ulp_disconnected(sctp->sctp_ulpd, err)) {
2657c478bd9Sstevel@tonic-gate 			/*
2667c478bd9Sstevel@tonic-gate 			 * Socket is gone, detach.
2677c478bd9Sstevel@tonic-gate 			 */
2687c478bd9Sstevel@tonic-gate 			sctp->sctp_detached = B_TRUE;
2697c478bd9Sstevel@tonic-gate 			sctp->sctp_ulpd = NULL;
2707c478bd9Sstevel@tonic-gate 			bzero(&sctp->sctp_upcalls, sizeof (sctp_upcalls_t));
2717c478bd9Sstevel@tonic-gate 		}
2727c478bd9Sstevel@tonic-gate 	}
2737c478bd9Sstevel@tonic-gate 
2747c478bd9Sstevel@tonic-gate 	/* Remove this sctp from all hashes. */
2757c478bd9Sstevel@tonic-gate 	sctp_closei_local(sctp);
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate 	/*
2787c478bd9Sstevel@tonic-gate 	 * If the sctp_t is detached, we need to finish freeing up
2797c478bd9Sstevel@tonic-gate 	 * the resources.  At this point, ip_fanout_sctp() should have
2807c478bd9Sstevel@tonic-gate 	 * a hold on this sctp_t.  Some thread doing snmp stuff can
2817c478bd9Sstevel@tonic-gate 	 * have a hold.  And a taskq can also have a hold waiting to
2827c478bd9Sstevel@tonic-gate 	 * work.  sctp_unlink() the sctp_t from the global list so
2837c478bd9Sstevel@tonic-gate 	 * that no new thread can find it.  Then do a SCTP_REFRELE().
2847c478bd9Sstevel@tonic-gate 	 * The sctp_t will be freed after all those threads are done.
2857c478bd9Sstevel@tonic-gate 	 */
2867c478bd9Sstevel@tonic-gate 	if (SCTP_IS_DETACHED(sctp)) {
2877c478bd9Sstevel@tonic-gate 		SCTP_CONDEMNED(sctp);
2887c478bd9Sstevel@tonic-gate 		SCTP_REFRELE(sctp);
2897c478bd9Sstevel@tonic-gate 	}
2907c478bd9Sstevel@tonic-gate }
2917c478bd9Sstevel@tonic-gate 
2927c478bd9Sstevel@tonic-gate /*
2937c478bd9Sstevel@tonic-gate  * Called by upper layer when it wants to close this association.
2947c478bd9Sstevel@tonic-gate  * Depending on the state of this assoication, we need to do
2957c478bd9Sstevel@tonic-gate  * different things.
2967c478bd9Sstevel@tonic-gate  *
2977c478bd9Sstevel@tonic-gate  * If the state is below COOKIE_ECHOED or it is COOKIE_ECHOED but with
2987c478bd9Sstevel@tonic-gate  * no sent data, just remove this sctp from all the hashes.  This
2997c478bd9Sstevel@tonic-gate  * makes sure that all packets from the other end will go to the default
3007c478bd9Sstevel@tonic-gate  * sctp handling.  The upper layer will then do a sctp_close() to clean
3017c478bd9Sstevel@tonic-gate  * up.
3027c478bd9Sstevel@tonic-gate  *
3037c478bd9Sstevel@tonic-gate  * Otherwise, check and see if SO_LINGER is set.  If it is set, check
3047c478bd9Sstevel@tonic-gate  * the value.  If the value is 0, consider this an abortive close.  Send
3057c478bd9Sstevel@tonic-gate  * an ABORT message and kill the associatiion.
3067c478bd9Sstevel@tonic-gate  *
3077c478bd9Sstevel@tonic-gate  */
3087c478bd9Sstevel@tonic-gate int
3097c478bd9Sstevel@tonic-gate sctp_disconnect(sctp_t *sctp)
3107c478bd9Sstevel@tonic-gate {
3117c478bd9Sstevel@tonic-gate 	int	error = 0;
3127c478bd9Sstevel@tonic-gate 	sctp_faddr_t *fp;
3137c478bd9Sstevel@tonic-gate 
3147c478bd9Sstevel@tonic-gate 	dprint(3, ("sctp_disconnect %p, state %d\n", sctp, sctp->sctp_state));
3157c478bd9Sstevel@tonic-gate 
3167c478bd9Sstevel@tonic-gate 	RUN_SCTP(sctp);
3177c478bd9Sstevel@tonic-gate 
3187c478bd9Sstevel@tonic-gate 	switch (sctp->sctp_state) {
3197c478bd9Sstevel@tonic-gate 	case SCTPS_IDLE:
3207c478bd9Sstevel@tonic-gate 	case SCTPS_BOUND:
3217c478bd9Sstevel@tonic-gate 	case SCTPS_LISTEN:
3227c478bd9Sstevel@tonic-gate 		break;
3237c478bd9Sstevel@tonic-gate 	case SCTPS_COOKIE_WAIT:
3247c478bd9Sstevel@tonic-gate 	case SCTPS_COOKIE_ECHOED:
3257c478bd9Sstevel@tonic-gate 		/*
3267c478bd9Sstevel@tonic-gate 		 * Close during the connect 3-way handshake
3277c478bd9Sstevel@tonic-gate 		 * but here there may or may not be pending data
3287c478bd9Sstevel@tonic-gate 		 * already on queue. Process almost same as in
3297c478bd9Sstevel@tonic-gate 		 * the ESTABLISHED state.
3307c478bd9Sstevel@tonic-gate 		 */
3317c478bd9Sstevel@tonic-gate 		if (sctp->sctp_xmit_head == NULL &&
3327c478bd9Sstevel@tonic-gate 		    sctp->sctp_xmit_unsent == NULL) {
3337c478bd9Sstevel@tonic-gate 			break;
3347c478bd9Sstevel@tonic-gate 		}
3357c478bd9Sstevel@tonic-gate 		/* FALLTHRU */
3367c478bd9Sstevel@tonic-gate 	default:
3377c478bd9Sstevel@tonic-gate 		/*
3387c478bd9Sstevel@tonic-gate 		 * If SO_LINGER has set a zero linger time, abort the
3397c478bd9Sstevel@tonic-gate 		 * connection with a reset.
3407c478bd9Sstevel@tonic-gate 		 */
3417c478bd9Sstevel@tonic-gate 		if (sctp->sctp_linger && sctp->sctp_lingertime == 0) {
3427c478bd9Sstevel@tonic-gate 			sctp_user_abort(sctp, NULL, B_FALSE);
3437c478bd9Sstevel@tonic-gate 			break;
3447c478bd9Sstevel@tonic-gate 		}
3457c478bd9Sstevel@tonic-gate 
3461d8c4025Svi 		/*
3471d8c4025Svi 		 * In there is unread data, send an ABORT
3481d8c4025Svi 		 */
3491d8c4025Svi 		if (sctp->sctp_rxqueued > 0 || sctp->sctp_irwnd >
3501d8c4025Svi 		    sctp->sctp_rwnd) {
3511d8c4025Svi 			sctp_user_abort(sctp, NULL, B_FALSE);
3521d8c4025Svi 			break;
3531d8c4025Svi 		}
3547c478bd9Sstevel@tonic-gate 		/*
3557c478bd9Sstevel@tonic-gate 		 * Transmit the shutdown before detaching the sctp_t.
3567c478bd9Sstevel@tonic-gate 		 * After sctp_detach returns this queue/perimeter
3577c478bd9Sstevel@tonic-gate 		 * no longer owns the sctp_t thus others can modify it.
3587c478bd9Sstevel@tonic-gate 		 */
3597c478bd9Sstevel@tonic-gate 		sctp_send_shutdown(sctp, 0);
3607c478bd9Sstevel@tonic-gate 
3617c478bd9Sstevel@tonic-gate 		/* Pass gathered wisdom to IP for keeping */
3627c478bd9Sstevel@tonic-gate 		for (fp = sctp->sctp_faddrs; fp != NULL; fp = fp->next)
3637c478bd9Sstevel@tonic-gate 			sctp_faddr2ire(sctp, fp);
3647c478bd9Sstevel@tonic-gate 
3657c478bd9Sstevel@tonic-gate 		/*
3667c478bd9Sstevel@tonic-gate 		 * If lingering on close then wait until the shutdown
3677c478bd9Sstevel@tonic-gate 		 * is complete, or the SO_LINGER time passes, or an
3687c478bd9Sstevel@tonic-gate 		 * ABORT is sent/received.  Note that sctp_disconnect()
3697c478bd9Sstevel@tonic-gate 		 * can be called more than once.  Make sure that only
3707c478bd9Sstevel@tonic-gate 		 * one thread waits.
3717c478bd9Sstevel@tonic-gate 		 */
3727c478bd9Sstevel@tonic-gate 		if (sctp->sctp_linger && sctp->sctp_lingertime > 0 &&
3737c478bd9Sstevel@tonic-gate 		    sctp->sctp_state >= SCTPS_ESTABLISHED &&
3747c478bd9Sstevel@tonic-gate 		    !sctp->sctp_lingering) {
3757c478bd9Sstevel@tonic-gate 			clock_t stoptime;	/* in ticks */
3767c478bd9Sstevel@tonic-gate 			clock_t ret;
3777c478bd9Sstevel@tonic-gate 
3787c478bd9Sstevel@tonic-gate 			/*
3797c478bd9Sstevel@tonic-gate 			 * Process the sendq to send the SHUTDOWN out
3807c478bd9Sstevel@tonic-gate 			 * before waiting.
3817c478bd9Sstevel@tonic-gate 			 */
3827c478bd9Sstevel@tonic-gate 			sctp_process_sendq(sctp);
3837c478bd9Sstevel@tonic-gate 
3847c478bd9Sstevel@tonic-gate 			sctp->sctp_lingering = 1;
3857c478bd9Sstevel@tonic-gate 			sctp->sctp_client_errno = 0;
3867c478bd9Sstevel@tonic-gate 			stoptime = lbolt + sctp->sctp_lingertime;
3877c478bd9Sstevel@tonic-gate 
3887c478bd9Sstevel@tonic-gate 			mutex_enter(&sctp->sctp_lock);
3897c478bd9Sstevel@tonic-gate 			sctp->sctp_running = B_FALSE;
3907c478bd9Sstevel@tonic-gate 			while (sctp->sctp_state >= SCTPS_ESTABLISHED &&
3917c478bd9Sstevel@tonic-gate 			    sctp->sctp_client_errno == 0) {
3927c478bd9Sstevel@tonic-gate 				cv_broadcast(&sctp->sctp_cv);
3937c478bd9Sstevel@tonic-gate 				ret = cv_timedwait_sig(&sctp->sctp_cv,
3947c478bd9Sstevel@tonic-gate 				    &sctp->sctp_lock, stoptime);
3957c478bd9Sstevel@tonic-gate 				if (ret < 0) {
3967c478bd9Sstevel@tonic-gate 					/* Stoptime has reached. */
3977c478bd9Sstevel@tonic-gate 					sctp->sctp_client_errno = EWOULDBLOCK;
3987c478bd9Sstevel@tonic-gate 					break;
3997c478bd9Sstevel@tonic-gate 				} else if (ret == 0) {
4007c478bd9Sstevel@tonic-gate 					/* Got a signal. */
4017c478bd9Sstevel@tonic-gate 					break;
4027c478bd9Sstevel@tonic-gate 				}
4037c478bd9Sstevel@tonic-gate 			}
4047c478bd9Sstevel@tonic-gate 			error = sctp->sctp_client_errno;
4057c478bd9Sstevel@tonic-gate 			sctp->sctp_client_errno = 0;
4067c478bd9Sstevel@tonic-gate 			mutex_exit(&sctp->sctp_lock);
4077c478bd9Sstevel@tonic-gate 		}
4087c478bd9Sstevel@tonic-gate 
4097c478bd9Sstevel@tonic-gate 		WAKE_SCTP(sctp);
4107c478bd9Sstevel@tonic-gate 		sctp_process_sendq(sctp);
4117c478bd9Sstevel@tonic-gate 		return (error);
4127c478bd9Sstevel@tonic-gate 	}
4137c478bd9Sstevel@tonic-gate 
4147c478bd9Sstevel@tonic-gate 
4157c478bd9Sstevel@tonic-gate 	/* Remove this sctp from all hashes so nobody can find it. */
4167c478bd9Sstevel@tonic-gate 	sctp_closei_local(sctp);
4177c478bd9Sstevel@tonic-gate 	WAKE_SCTP(sctp);
4187c478bd9Sstevel@tonic-gate 	return (error);
4197c478bd9Sstevel@tonic-gate }
4207c478bd9Sstevel@tonic-gate 
4217c478bd9Sstevel@tonic-gate void
4227c478bd9Sstevel@tonic-gate sctp_close(sctp_t *sctp)
4237c478bd9Sstevel@tonic-gate {
4247c478bd9Sstevel@tonic-gate 	dprint(3, ("sctp_close %p, state %d\n", sctp, sctp->sctp_state));
4257c478bd9Sstevel@tonic-gate 
4267c478bd9Sstevel@tonic-gate 	RUN_SCTP(sctp);
4277c478bd9Sstevel@tonic-gate 	sctp->sctp_detached = 1;
4287c478bd9Sstevel@tonic-gate 	sctp->sctp_ulpd = NULL;
4297c478bd9Sstevel@tonic-gate 	bzero(&sctp->sctp_upcalls, sizeof (sctp_upcalls_t));
4307c478bd9Sstevel@tonic-gate 	bzero(&sctp->sctp_events, sizeof (sctp->sctp_events));
4317c478bd9Sstevel@tonic-gate 
4327c478bd9Sstevel@tonic-gate 	/* If the graceful shutdown has not been completed, just return. */
4337c478bd9Sstevel@tonic-gate 	if (sctp->sctp_state != SCTPS_IDLE) {
4347c478bd9Sstevel@tonic-gate 		WAKE_SCTP(sctp);
4357c478bd9Sstevel@tonic-gate 		return;
4367c478bd9Sstevel@tonic-gate 	}
4377c478bd9Sstevel@tonic-gate 
4387c478bd9Sstevel@tonic-gate 	/*
4397c478bd9Sstevel@tonic-gate 	 * Since sctp_t is in SCTPS_IDLE state, so the only thread which
4407c478bd9Sstevel@tonic-gate 	 * can have a hold on the sctp_t is doing snmp stuff.  Just do
4417c478bd9Sstevel@tonic-gate 	 * a SCTP_REFRELE() here after the SCTP_UNLINK().  It will
4427c478bd9Sstevel@tonic-gate 	 * be freed when the other thread is done.
4437c478bd9Sstevel@tonic-gate 	 */
4447c478bd9Sstevel@tonic-gate 	SCTP_CONDEMNED(sctp);
4457c478bd9Sstevel@tonic-gate 	WAKE_SCTP(sctp);
4467c478bd9Sstevel@tonic-gate 	SCTP_REFRELE(sctp);
4477c478bd9Sstevel@tonic-gate }
4487c478bd9Sstevel@tonic-gate 
4497c478bd9Sstevel@tonic-gate /*
4507c478bd9Sstevel@tonic-gate  * Unlink from global list and do the eager close.
4517c478bd9Sstevel@tonic-gate  * Remove the refhold implicit in being on the global list.
4527c478bd9Sstevel@tonic-gate  */
4537c478bd9Sstevel@tonic-gate void
4547c478bd9Sstevel@tonic-gate sctp_close_eager(sctp_t *sctp)
4557c478bd9Sstevel@tonic-gate {
4567c478bd9Sstevel@tonic-gate 	SCTP_CONDEMNED(sctp);
4577c478bd9Sstevel@tonic-gate 	sctp_closei_local(sctp);
4587c478bd9Sstevel@tonic-gate 	SCTP_REFRELE(sctp);
4597c478bd9Sstevel@tonic-gate }
4607c478bd9Sstevel@tonic-gate 
4617c478bd9Sstevel@tonic-gate /*
4627c478bd9Sstevel@tonic-gate  * The sctp_t is going away. Remove it from all lists and set it
4637c478bd9Sstevel@tonic-gate  * to SCTPS_IDLE. The caller has to remove it from the
4647c478bd9Sstevel@tonic-gate  * global list. The freeing up of memory is deferred until
4657c478bd9Sstevel@tonic-gate  * sctp_free(). This is needed since a thread in sctp_input() might have
4667c478bd9Sstevel@tonic-gate  * done a SCTP_REFHOLD on this structure before it was removed from the
4677c478bd9Sstevel@tonic-gate  * hashes.
4687c478bd9Sstevel@tonic-gate  */
4697c478bd9Sstevel@tonic-gate static void
4707c478bd9Sstevel@tonic-gate sctp_closei_local(sctp_t *sctp)
4717c478bd9Sstevel@tonic-gate {
4727c478bd9Sstevel@tonic-gate 	mblk_t	*mp;
4737c478bd9Sstevel@tonic-gate 	ire_t	*ire = NULL;
4747c478bd9Sstevel@tonic-gate 	conn_t	*connp = sctp->sctp_connp;
4757c478bd9Sstevel@tonic-gate 
4767c478bd9Sstevel@tonic-gate 	/* Stop and free the timers */
4777c478bd9Sstevel@tonic-gate 	sctp_free_faddr_timers(sctp);
4787c478bd9Sstevel@tonic-gate 	if ((mp = sctp->sctp_heartbeat_mp) != NULL) {
4797c478bd9Sstevel@tonic-gate 		sctp_timer_free(mp);
4807c478bd9Sstevel@tonic-gate 		sctp->sctp_heartbeat_mp = NULL;
4817c478bd9Sstevel@tonic-gate 	}
4827c478bd9Sstevel@tonic-gate 	if ((mp = sctp->sctp_ack_mp) != NULL) {
4837c478bd9Sstevel@tonic-gate 		sctp_timer_free(mp);
4847c478bd9Sstevel@tonic-gate 		sctp->sctp_ack_mp = NULL;
4857c478bd9Sstevel@tonic-gate 	}
4867c478bd9Sstevel@tonic-gate 
4877c478bd9Sstevel@tonic-gate 	/* Set the CONN_CLOSING flag so that IP will not cache IRE again. */
4887c478bd9Sstevel@tonic-gate 	mutex_enter(&connp->conn_lock);
4897c478bd9Sstevel@tonic-gate 	connp->conn_state_flags |= CONN_CLOSING;
4907c478bd9Sstevel@tonic-gate 	ire = connp->conn_ire_cache;
4917c478bd9Sstevel@tonic-gate 	connp->conn_ire_cache = NULL;
4927c478bd9Sstevel@tonic-gate 	mutex_exit(&connp->conn_lock);
4937c478bd9Sstevel@tonic-gate 	if (ire != NULL)
4947c478bd9Sstevel@tonic-gate 		IRE_REFRELE_NOTR(ire);
4957c478bd9Sstevel@tonic-gate 
4967c478bd9Sstevel@tonic-gate 	/* Remove from all hashes. */
4977c478bd9Sstevel@tonic-gate 	sctp_bind_hash_remove(sctp);
4987c478bd9Sstevel@tonic-gate 	sctp_conn_hash_remove(sctp);
4997c478bd9Sstevel@tonic-gate 	sctp_listen_hash_remove(sctp);
5007c478bd9Sstevel@tonic-gate 	sctp->sctp_state = SCTPS_IDLE;
5017c478bd9Sstevel@tonic-gate 
5027c478bd9Sstevel@tonic-gate 	/*
5037c478bd9Sstevel@tonic-gate 	 * Clean up the recvq as much as possible.  All those packets
5047c478bd9Sstevel@tonic-gate 	 * will be silently dropped as this sctp_t is now in idle state.
5057c478bd9Sstevel@tonic-gate 	 */
5067c478bd9Sstevel@tonic-gate 	mutex_enter(&sctp->sctp_recvq_lock);
5077c478bd9Sstevel@tonic-gate 	while ((mp = sctp->sctp_recvq) != NULL) {
5087c478bd9Sstevel@tonic-gate 		mblk_t *ipsec_mp;
5097c478bd9Sstevel@tonic-gate 
5107c478bd9Sstevel@tonic-gate 		sctp->sctp_recvq = mp->b_next;
5117c478bd9Sstevel@tonic-gate 		mp->b_next = NULL;
5127c478bd9Sstevel@tonic-gate 		if ((ipsec_mp = mp->b_prev) != NULL) {
5137c478bd9Sstevel@tonic-gate 			freeb(ipsec_mp);
5147c478bd9Sstevel@tonic-gate 			mp->b_prev = NULL;
5157c478bd9Sstevel@tonic-gate 		}
5167c478bd9Sstevel@tonic-gate 		freemsg(mp);
5177c478bd9Sstevel@tonic-gate 	}
5187c478bd9Sstevel@tonic-gate 	mutex_exit(&sctp->sctp_recvq_lock);
5197c478bd9Sstevel@tonic-gate }
5207c478bd9Sstevel@tonic-gate 
5217c478bd9Sstevel@tonic-gate /*
5227c478bd9Sstevel@tonic-gate  * Free memory associated with the sctp/ip header template.
5237c478bd9Sstevel@tonic-gate  */
5247c478bd9Sstevel@tonic-gate static void
5257c478bd9Sstevel@tonic-gate sctp_headers_free(sctp_t *sctp)
5267c478bd9Sstevel@tonic-gate {
5277c478bd9Sstevel@tonic-gate 	if (sctp->sctp_iphc != NULL) {
5287c478bd9Sstevel@tonic-gate 		kmem_free(sctp->sctp_iphc, sctp->sctp_iphc_len);
5297c478bd9Sstevel@tonic-gate 		sctp->sctp_iphc = NULL;
5307c478bd9Sstevel@tonic-gate 		sctp->sctp_ipha = NULL;
5317c478bd9Sstevel@tonic-gate 		sctp->sctp_hdr_len = 0;
5327c478bd9Sstevel@tonic-gate 		sctp->sctp_ip_hdr_len = 0;
5337c478bd9Sstevel@tonic-gate 		sctp->sctp_iphc_len = 0;
5347c478bd9Sstevel@tonic-gate 		sctp->sctp_sctph = NULL;
5357c478bd9Sstevel@tonic-gate 		sctp->sctp_hdr_len = 0;
5367c478bd9Sstevel@tonic-gate 	}
5377c478bd9Sstevel@tonic-gate 	if (sctp->sctp_iphc6 != NULL) {
5387c478bd9Sstevel@tonic-gate 		kmem_free(sctp->sctp_iphc6, sctp->sctp_iphc6_len);
5397c478bd9Sstevel@tonic-gate 		sctp->sctp_iphc6 = NULL;
5407c478bd9Sstevel@tonic-gate 		sctp->sctp_ip6h = NULL;
5417c478bd9Sstevel@tonic-gate 		sctp->sctp_hdr6_len = 0;
5427c478bd9Sstevel@tonic-gate 		sctp->sctp_ip_hdr6_len = 0;
5437c478bd9Sstevel@tonic-gate 		sctp->sctp_iphc6_len = 0;
5447c478bd9Sstevel@tonic-gate 		sctp->sctp_sctph6 = NULL;
5457c478bd9Sstevel@tonic-gate 		sctp->sctp_hdr6_len = 0;
5467c478bd9Sstevel@tonic-gate 	}
5477c478bd9Sstevel@tonic-gate }
5487c478bd9Sstevel@tonic-gate 
5497c478bd9Sstevel@tonic-gate static void
5507c478bd9Sstevel@tonic-gate sctp_free_xmit_data(sctp_t *sctp)
5517c478bd9Sstevel@tonic-gate {
5527c478bd9Sstevel@tonic-gate 	mblk_t	*ump = NULL;
5537c478bd9Sstevel@tonic-gate 	mblk_t	*nump;
5547c478bd9Sstevel@tonic-gate 	mblk_t	*mp;
5557c478bd9Sstevel@tonic-gate 	mblk_t	*nmp;
5567c478bd9Sstevel@tonic-gate 
5577c478bd9Sstevel@tonic-gate 	sctp->sctp_xmit_unacked = NULL;
5587c478bd9Sstevel@tonic-gate 	ump = sctp->sctp_xmit_head;
5597c478bd9Sstevel@tonic-gate 	sctp->sctp_xmit_tail = sctp->sctp_xmit_head = NULL;
5607c478bd9Sstevel@tonic-gate free_unsent:
5617c478bd9Sstevel@tonic-gate 	for (; ump != NULL; ump = nump) {
5627c478bd9Sstevel@tonic-gate 		for (mp = ump->b_cont; mp != NULL; mp = nmp) {
5637c478bd9Sstevel@tonic-gate 			nmp = mp->b_next;
5647c478bd9Sstevel@tonic-gate 			mp->b_next = NULL;
5657c478bd9Sstevel@tonic-gate 			mp->b_prev = NULL;
5667c478bd9Sstevel@tonic-gate 			freemsg(mp);
5677c478bd9Sstevel@tonic-gate 		}
5687c478bd9Sstevel@tonic-gate 		ASSERT(DB_REF(ump) == 1);
5697c478bd9Sstevel@tonic-gate 		nump = ump->b_next;
5707c478bd9Sstevel@tonic-gate 		ump->b_next = NULL;
5717c478bd9Sstevel@tonic-gate 		ump->b_prev = NULL;
5727c478bd9Sstevel@tonic-gate 		ump->b_cont = NULL;
5737c478bd9Sstevel@tonic-gate 		freeb(ump);
5747c478bd9Sstevel@tonic-gate 	}
5757c478bd9Sstevel@tonic-gate 	if ((ump = sctp->sctp_xmit_unsent) == NULL) {
5767c478bd9Sstevel@tonic-gate 		ASSERT(sctp->sctp_xmit_unsent_tail == NULL);
5777c478bd9Sstevel@tonic-gate 		return;
5787c478bd9Sstevel@tonic-gate 	}
5797c478bd9Sstevel@tonic-gate 	sctp->sctp_xmit_unsent = sctp->sctp_xmit_unsent_tail = NULL;
5807c478bd9Sstevel@tonic-gate 	goto free_unsent;
5817c478bd9Sstevel@tonic-gate }
5827c478bd9Sstevel@tonic-gate 
5837c478bd9Sstevel@tonic-gate /*
5847c478bd9Sstevel@tonic-gate  * Cleanup all the messages in the stream queue and the reassembly lists.
5857c478bd9Sstevel@tonic-gate  * If 'free' is true, then delete the streams as well.
5867c478bd9Sstevel@tonic-gate  */
5877c478bd9Sstevel@tonic-gate void
5887c478bd9Sstevel@tonic-gate sctp_instream_cleanup(sctp_t *sctp, boolean_t free)
5897c478bd9Sstevel@tonic-gate {
5907c478bd9Sstevel@tonic-gate 	int	i;
5917c478bd9Sstevel@tonic-gate 	mblk_t	*mp;
5927c478bd9Sstevel@tonic-gate 	mblk_t	*mp1;
5937c478bd9Sstevel@tonic-gate 
5947c478bd9Sstevel@tonic-gate 	if (sctp->sctp_instr != NULL) {
5957c478bd9Sstevel@tonic-gate 		/* walk thru and flush out anything remaining in the Q */
5967c478bd9Sstevel@tonic-gate 		for (i = 0; i < sctp->sctp_num_istr; i++) {
5977c478bd9Sstevel@tonic-gate 			mp = sctp->sctp_instr[i].istr_msgs;
5987c478bd9Sstevel@tonic-gate 			while (mp != NULL) {
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 				mp = mp1;
6037c478bd9Sstevel@tonic-gate 			}
6047c478bd9Sstevel@tonic-gate 			sctp->sctp_instr[i].istr_msgs = NULL;
6057c478bd9Sstevel@tonic-gate 			sctp_free_reass((sctp->sctp_instr) + i);
6067c478bd9Sstevel@tonic-gate 			sctp->sctp_instr[i].nextseq = 0;
6077c478bd9Sstevel@tonic-gate 		}
6087c478bd9Sstevel@tonic-gate 		if (free) {
6097c478bd9Sstevel@tonic-gate 			kmem_free(sctp->sctp_instr,
6107c478bd9Sstevel@tonic-gate 			    sizeof (*sctp->sctp_instr) * sctp->sctp_num_istr);
6117c478bd9Sstevel@tonic-gate 			sctp->sctp_instr = NULL;
6127c478bd9Sstevel@tonic-gate 			sctp->sctp_num_istr = 0;
6137c478bd9Sstevel@tonic-gate 		}
6147c478bd9Sstevel@tonic-gate 	}
6157c478bd9Sstevel@tonic-gate 	/* un-ordered fragments */
6167c478bd9Sstevel@tonic-gate 	if (sctp->sctp_uo_frags != NULL) {
6177c478bd9Sstevel@tonic-gate 		for (mp = sctp->sctp_uo_frags; mp != NULL; mp = mp1) {
6187c478bd9Sstevel@tonic-gate 			mp1 = mp->b_next;
6197c478bd9Sstevel@tonic-gate 			mp->b_next = mp->b_prev = NULL;
6207c478bd9Sstevel@tonic-gate 			freemsg(mp);
6217c478bd9Sstevel@tonic-gate 		}
6227c478bd9Sstevel@tonic-gate 	}
6237c478bd9Sstevel@tonic-gate }
6247c478bd9Sstevel@tonic-gate 
6257c478bd9Sstevel@tonic-gate /*
6267c478bd9Sstevel@tonic-gate  * Last reference to the sctp_t is gone. Free all memory associated with it.
6277c478bd9Sstevel@tonic-gate  * Called from SCTP_REFRELE. Called inline in sctp_close()
6287c478bd9Sstevel@tonic-gate  */
6297c478bd9Sstevel@tonic-gate void
6307c478bd9Sstevel@tonic-gate sctp_free(conn_t *connp)
6317c478bd9Sstevel@tonic-gate {
6327c478bd9Sstevel@tonic-gate 	sctp_t *sctp = CONN2SCTP(connp);
6337c478bd9Sstevel@tonic-gate 	ip6_pkt_t	*ipp;
6347c478bd9Sstevel@tonic-gate 	int		cnt;
6357c478bd9Sstevel@tonic-gate 
6367c478bd9Sstevel@tonic-gate 	/* Unlink it from the global list */
6377c478bd9Sstevel@tonic-gate 	SCTP_UNLINK(sctp);
6387c478bd9Sstevel@tonic-gate 
6397c478bd9Sstevel@tonic-gate 	ASSERT(connp->conn_ref == 0);
6407c478bd9Sstevel@tonic-gate 	ASSERT(connp->conn_ulp == IPPROTO_SCTP);
6417c478bd9Sstevel@tonic-gate 	ASSERT(!MUTEX_HELD(&sctp->sctp_reflock));
6427c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_refcnt == 0);
6437c478bd9Sstevel@tonic-gate 
6447c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_ptpbhn == NULL && sctp->sctp_bind_hash == NULL);
6457c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_conn_hash_next == NULL &&
6467c478bd9Sstevel@tonic-gate 	    sctp->sctp_conn_hash_prev == NULL);
6477c478bd9Sstevel@tonic-gate 
6487c478bd9Sstevel@tonic-gate 
6497c478bd9Sstevel@tonic-gate 	/* Free up all the resources. */
6507c478bd9Sstevel@tonic-gate 
6517c478bd9Sstevel@tonic-gate 	/* blow away sctp stream management */
6527c478bd9Sstevel@tonic-gate 	if (sctp->sctp_ostrcntrs != NULL) {
6537c478bd9Sstevel@tonic-gate 		kmem_free(sctp->sctp_ostrcntrs,
6547c478bd9Sstevel@tonic-gate 		    sizeof (uint16_t) * sctp->sctp_num_ostr);
6557c478bd9Sstevel@tonic-gate 		sctp->sctp_ostrcntrs = NULL;
6567c478bd9Sstevel@tonic-gate 	}
6577c478bd9Sstevel@tonic-gate 	sctp_instream_cleanup(sctp, B_TRUE);
6587c478bd9Sstevel@tonic-gate 
6597c478bd9Sstevel@tonic-gate 	/* Remove all data transfer resources. */
6607c478bd9Sstevel@tonic-gate 	sctp->sctp_istr_nmsgs = 0;
6617c478bd9Sstevel@tonic-gate 	sctp->sctp_rxqueued = 0;
6627c478bd9Sstevel@tonic-gate 	sctp_free_xmit_data(sctp);
6637c478bd9Sstevel@tonic-gate 	sctp->sctp_unacked = 0;
6647c478bd9Sstevel@tonic-gate 	sctp->sctp_unsent = 0;
6651d8c4025Svi 	if (sctp->sctp_cxmit_list != NULL)
6661d8c4025Svi 		sctp_asconf_free_cxmit(sctp, NULL);
6671d8c4025Svi 
6687c478bd9Sstevel@tonic-gate 	sctp->sctp_lastdata = NULL;
6697c478bd9Sstevel@tonic-gate 
6707c478bd9Sstevel@tonic-gate 	/* Clear out default xmit settings */
6717c478bd9Sstevel@tonic-gate 	sctp->sctp_def_stream = 0;
6727c478bd9Sstevel@tonic-gate 	sctp->sctp_def_flags = 0;
6737c478bd9Sstevel@tonic-gate 	sctp->sctp_def_ppid = 0;
6747c478bd9Sstevel@tonic-gate 	sctp->sctp_def_context = 0;
6757c478bd9Sstevel@tonic-gate 	sctp->sctp_def_timetolive = 0;
6767c478bd9Sstevel@tonic-gate 
6777c478bd9Sstevel@tonic-gate 	if (sctp->sctp_sack_info != NULL) {
6787c478bd9Sstevel@tonic-gate 		sctp_free_set(sctp->sctp_sack_info);
6797c478bd9Sstevel@tonic-gate 		sctp->sctp_sack_info = NULL;
6807c478bd9Sstevel@tonic-gate 	}
6817c478bd9Sstevel@tonic-gate 	sctp->sctp_sack_gaps = 0;
6827c478bd9Sstevel@tonic-gate 
6837c478bd9Sstevel@tonic-gate 	if (sctp->sctp_cookie_mp != NULL) {
6847c478bd9Sstevel@tonic-gate 		freemsg(sctp->sctp_cookie_mp);
6857c478bd9Sstevel@tonic-gate 		sctp->sctp_cookie_mp = NULL;
6867c478bd9Sstevel@tonic-gate 	}
6877c478bd9Sstevel@tonic-gate 
6887c478bd9Sstevel@tonic-gate 	/* Remove all the address resources. */
6897c478bd9Sstevel@tonic-gate 	sctp_zap_addrs(sctp);
6907c478bd9Sstevel@tonic-gate 	for (cnt = 0; cnt < SCTP_IPIF_HASH; cnt++) {
6917c478bd9Sstevel@tonic-gate 		ASSERT(sctp->sctp_saddrs[cnt].ipif_count == 0);
6927c478bd9Sstevel@tonic-gate 		list_destroy(&sctp->sctp_saddrs[cnt].sctp_ipif_list);
6937c478bd9Sstevel@tonic-gate 	}
6947c478bd9Sstevel@tonic-gate 
6957c478bd9Sstevel@tonic-gate 	ipp = &sctp->sctp_sticky_ipp;
6967c478bd9Sstevel@tonic-gate 	if (ipp->ipp_rthdrlen != 0) {
6977c478bd9Sstevel@tonic-gate 		kmem_free(ipp->ipp_rthdr, ipp->ipp_rthdrlen);
6987c478bd9Sstevel@tonic-gate 		ipp->ipp_rthdrlen = 0;
6997c478bd9Sstevel@tonic-gate 	}
7007c478bd9Sstevel@tonic-gate 
7017c478bd9Sstevel@tonic-gate 	if (ipp->ipp_dstoptslen != 0) {
7027c478bd9Sstevel@tonic-gate 		kmem_free(ipp->ipp_dstopts, ipp->ipp_dstoptslen);
7037c478bd9Sstevel@tonic-gate 		ipp->ipp_dstoptslen = 0;
7047c478bd9Sstevel@tonic-gate 	}
7057c478bd9Sstevel@tonic-gate 
7067c478bd9Sstevel@tonic-gate 	if (ipp->ipp_rtdstoptslen != 0) {
7077c478bd9Sstevel@tonic-gate 		kmem_free(ipp->ipp_rtdstopts, ipp->ipp_rtdstoptslen);
7087c478bd9Sstevel@tonic-gate 		ipp->ipp_rtdstoptslen = 0;
7097c478bd9Sstevel@tonic-gate 	}
7107c478bd9Sstevel@tonic-gate 
7117c478bd9Sstevel@tonic-gate 	if (ipp->ipp_hopoptslen != 0) {
7127c478bd9Sstevel@tonic-gate 		kmem_free(ipp->ipp_hopopts, ipp->ipp_hopoptslen);
7137c478bd9Sstevel@tonic-gate 		ipp->ipp_hopoptslen = 0;
7147c478bd9Sstevel@tonic-gate 	}
7157c478bd9Sstevel@tonic-gate 
7167c478bd9Sstevel@tonic-gate 	if (sctp->sctp_hopopts != NULL) {
7177c478bd9Sstevel@tonic-gate 		mi_free(sctp->sctp_hopopts);
7187c478bd9Sstevel@tonic-gate 		sctp->sctp_hopopts = NULL;
7197c478bd9Sstevel@tonic-gate 		sctp->sctp_hopoptslen = 0;
7207c478bd9Sstevel@tonic-gate 	}
7217c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_hopoptslen == 0);
7227c478bd9Sstevel@tonic-gate 	if (sctp->sctp_dstopts != NULL) {
7237c478bd9Sstevel@tonic-gate 		mi_free(sctp->sctp_dstopts);
7247c478bd9Sstevel@tonic-gate 		sctp->sctp_dstopts = NULL;
7257c478bd9Sstevel@tonic-gate 		sctp->sctp_dstoptslen = 0;
7267c478bd9Sstevel@tonic-gate 	}
7277c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_dstoptslen == 0);
7287c478bd9Sstevel@tonic-gate 	if (sctp->sctp_rtdstopts != NULL) {
7297c478bd9Sstevel@tonic-gate 		mi_free(sctp->sctp_rtdstopts);
7307c478bd9Sstevel@tonic-gate 		sctp->sctp_rtdstopts = NULL;
7317c478bd9Sstevel@tonic-gate 		sctp->sctp_rtdstoptslen = 0;
7327c478bd9Sstevel@tonic-gate 	}
7337c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_rtdstoptslen == 0);
7347c478bd9Sstevel@tonic-gate 	if (sctp->sctp_rthdr != NULL) {
7357c478bd9Sstevel@tonic-gate 		mi_free(sctp->sctp_rthdr);
7367c478bd9Sstevel@tonic-gate 		sctp->sctp_rthdr = NULL;
7377c478bd9Sstevel@tonic-gate 		sctp->sctp_rthdrlen = 0;
7387c478bd9Sstevel@tonic-gate 	}
7397c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_rthdrlen == 0);
7407c478bd9Sstevel@tonic-gate 	sctp_headers_free(sctp);
7417c478bd9Sstevel@tonic-gate 
7427c478bd9Sstevel@tonic-gate 	sctp->sctp_shutdown_faddr = NULL;
7437c478bd9Sstevel@tonic-gate 
7447c478bd9Sstevel@tonic-gate 	/* Clear all the bitfields. */
7457c478bd9Sstevel@tonic-gate 	bzero(&sctp->sctp_bits, sizeof (sctp->sctp_bits));
7467c478bd9Sstevel@tonic-gate 
7477c478bd9Sstevel@tonic-gate 	/* It is time to update the global statistics. */
7487c478bd9Sstevel@tonic-gate 	UPDATE_MIB(&sctp_mib, sctpOutSCTPPkts, sctp->sctp_opkts);
7497c478bd9Sstevel@tonic-gate 	UPDATE_MIB(&sctp_mib, sctpOutCtrlChunks, sctp->sctp_obchunks);
7507c478bd9Sstevel@tonic-gate 	UPDATE_MIB(&sctp_mib, sctpOutOrderChunks, sctp->sctp_odchunks);
7517c478bd9Sstevel@tonic-gate 	UPDATE_MIB(&sctp_mib, sctpOutUnorderChunks, sctp->sctp_oudchunks);
7527c478bd9Sstevel@tonic-gate 	UPDATE_MIB(&sctp_mib, sctpRetransChunks, sctp->sctp_rxtchunks);
7537c478bd9Sstevel@tonic-gate 	UPDATE_MIB(&sctp_mib, sctpInSCTPPkts, sctp->sctp_ipkts);
7547c478bd9Sstevel@tonic-gate 	UPDATE_MIB(&sctp_mib, sctpInCtrlChunks, sctp->sctp_ibchunks);
7557c478bd9Sstevel@tonic-gate 	UPDATE_MIB(&sctp_mib, sctpInOrderChunks, sctp->sctp_idchunks);
7567c478bd9Sstevel@tonic-gate 	UPDATE_MIB(&sctp_mib, sctpInUnorderChunks, sctp->sctp_iudchunks);
7577c478bd9Sstevel@tonic-gate 	UPDATE_MIB(&sctp_mib, sctpFragUsrMsgs, sctp->sctp_fragdmsgs);
7587c478bd9Sstevel@tonic-gate 	UPDATE_MIB(&sctp_mib, sctpReasmUsrMsgs, sctp->sctp_reassmsgs);
7597c478bd9Sstevel@tonic-gate 	sctp->sctp_opkts = 0;
7607c478bd9Sstevel@tonic-gate 	sctp->sctp_obchunks = 0;
7617c478bd9Sstevel@tonic-gate 	sctp->sctp_odchunks = 0;
7627c478bd9Sstevel@tonic-gate 	sctp->sctp_oudchunks = 0;
7637c478bd9Sstevel@tonic-gate 	sctp->sctp_rxtchunks = 0;
7647c478bd9Sstevel@tonic-gate 	sctp->sctp_ipkts = 0;
7657c478bd9Sstevel@tonic-gate 	sctp->sctp_ibchunks = 0;
7667c478bd9Sstevel@tonic-gate 	sctp->sctp_idchunks = 0;
7677c478bd9Sstevel@tonic-gate 	sctp->sctp_iudchunks = 0;
7687c478bd9Sstevel@tonic-gate 	sctp->sctp_fragdmsgs = 0;
7697c478bd9Sstevel@tonic-gate 	sctp->sctp_reassmsgs = 0;
7707c478bd9Sstevel@tonic-gate 
7717c478bd9Sstevel@tonic-gate 	sctp->sctp_autoclose = 0;
7727c478bd9Sstevel@tonic-gate 	sctp->sctp_tx_adaption_code = 0;
7737c478bd9Sstevel@tonic-gate 
7747c478bd9Sstevel@tonic-gate 	/* Clean up conn_t stuff */
7757c478bd9Sstevel@tonic-gate 	connp->conn_policy_cached = B_FALSE;
7767c478bd9Sstevel@tonic-gate 	if (connp->conn_latch != NULL) {
7777c478bd9Sstevel@tonic-gate 		IPLATCH_REFRELE(connp->conn_latch);
7787c478bd9Sstevel@tonic-gate 		connp->conn_latch = NULL;
7797c478bd9Sstevel@tonic-gate 	}
7807c478bd9Sstevel@tonic-gate 	if (connp->conn_policy != NULL) {
7817c478bd9Sstevel@tonic-gate 		IPPH_REFRELE(connp->conn_policy);
7827c478bd9Sstevel@tonic-gate 		connp->conn_policy = NULL;
7837c478bd9Sstevel@tonic-gate 	}
7847c478bd9Sstevel@tonic-gate 	if (connp->conn_ipsec_opt_mp != NULL) {
7857c478bd9Sstevel@tonic-gate 		freemsg(connp->conn_ipsec_opt_mp);
7867c478bd9Sstevel@tonic-gate 		connp->conn_ipsec_opt_mp = NULL;
7877c478bd9Sstevel@tonic-gate 	}
7887c478bd9Sstevel@tonic-gate 	if (connp->conn_cred != NULL) {
7897c478bd9Sstevel@tonic-gate 		crfree(connp->conn_cred);
7907c478bd9Sstevel@tonic-gate 		connp->conn_cred = NULL;
7917c478bd9Sstevel@tonic-gate 	}
7927c478bd9Sstevel@tonic-gate 
7937c478bd9Sstevel@tonic-gate 	kmem_cache_free(sctp_conn_cache, connp);
7947c478bd9Sstevel@tonic-gate }
7957c478bd9Sstevel@tonic-gate 
7967c478bd9Sstevel@tonic-gate /* Diagnostic routine used to return a string associated with the sctp state. */
7977c478bd9Sstevel@tonic-gate char *
7987c478bd9Sstevel@tonic-gate sctp_display(sctp_t *sctp, char *sup_buf)
7997c478bd9Sstevel@tonic-gate {
8007c478bd9Sstevel@tonic-gate 	char	*buf;
8017c478bd9Sstevel@tonic-gate 	char	buf1[30];
8027c478bd9Sstevel@tonic-gate 	static char	priv_buf[INET6_ADDRSTRLEN * 2 + 80];
8037c478bd9Sstevel@tonic-gate 	char	*cp;
8047c478bd9Sstevel@tonic-gate 
8057c478bd9Sstevel@tonic-gate 	if (sctp == NULL)
8067c478bd9Sstevel@tonic-gate 		return ("NULL_SCTP");
8077c478bd9Sstevel@tonic-gate 
8087c478bd9Sstevel@tonic-gate 	buf = (sup_buf != NULL) ? sup_buf : priv_buf;
8097c478bd9Sstevel@tonic-gate 
8107c478bd9Sstevel@tonic-gate 	switch (sctp->sctp_state) {
8117c478bd9Sstevel@tonic-gate 	case SCTPS_IDLE:
8127c478bd9Sstevel@tonic-gate 		cp = "SCTP_IDLE";
8137c478bd9Sstevel@tonic-gate 		break;
8147c478bd9Sstevel@tonic-gate 	case SCTPS_BOUND:
8157c478bd9Sstevel@tonic-gate 		cp = "SCTP_BOUND";
8167c478bd9Sstevel@tonic-gate 		break;
8177c478bd9Sstevel@tonic-gate 	case SCTPS_LISTEN:
8187c478bd9Sstevel@tonic-gate 		cp = "SCTP_LISTEN";
8197c478bd9Sstevel@tonic-gate 		break;
8207c478bd9Sstevel@tonic-gate 	case SCTPS_COOKIE_WAIT:
8217c478bd9Sstevel@tonic-gate 		cp = "SCTP_COOKIE_WAIT";
8227c478bd9Sstevel@tonic-gate 		break;
8237c478bd9Sstevel@tonic-gate 	case SCTPS_COOKIE_ECHOED:
8247c478bd9Sstevel@tonic-gate 		cp = "SCTP_COOKIE_ECHOED";
8257c478bd9Sstevel@tonic-gate 		break;
8267c478bd9Sstevel@tonic-gate 	case SCTPS_ESTABLISHED:
8277c478bd9Sstevel@tonic-gate 		cp = "SCTP_ESTABLISHED";
8287c478bd9Sstevel@tonic-gate 		break;
8297c478bd9Sstevel@tonic-gate 	case SCTPS_SHUTDOWN_PENDING:
8307c478bd9Sstevel@tonic-gate 		cp = "SCTP_SHUTDOWN_PENDING";
8317c478bd9Sstevel@tonic-gate 		break;
8327c478bd9Sstevel@tonic-gate 	case SCTPS_SHUTDOWN_SENT:
8337c478bd9Sstevel@tonic-gate 		cp = "SCTPS_SHUTDOWN_SENT";
8347c478bd9Sstevel@tonic-gate 		break;
8357c478bd9Sstevel@tonic-gate 	case SCTPS_SHUTDOWN_RECEIVED:
8367c478bd9Sstevel@tonic-gate 		cp = "SCTPS_SHUTDOWN_RECEIVED";
8377c478bd9Sstevel@tonic-gate 		break;
8387c478bd9Sstevel@tonic-gate 	case SCTPS_SHUTDOWN_ACK_SENT:
8397c478bd9Sstevel@tonic-gate 		cp = "SCTPS_SHUTDOWN_ACK_SENT";
8407c478bd9Sstevel@tonic-gate 		break;
8417c478bd9Sstevel@tonic-gate 	default:
8427c478bd9Sstevel@tonic-gate 		(void) mi_sprintf(buf1, "SCTPUnkState(%d)", sctp->sctp_state);
8437c478bd9Sstevel@tonic-gate 		cp = buf1;
8447c478bd9Sstevel@tonic-gate 		break;
8457c478bd9Sstevel@tonic-gate 	}
8467c478bd9Sstevel@tonic-gate 	(void) mi_sprintf(buf, "[%u, %u] %s",
8477c478bd9Sstevel@tonic-gate 	    ntohs(sctp->sctp_lport), ntohs(sctp->sctp_fport), cp);
8487c478bd9Sstevel@tonic-gate 
8497c478bd9Sstevel@tonic-gate 	return (buf);
8507c478bd9Sstevel@tonic-gate }
8517c478bd9Sstevel@tonic-gate 
8527c478bd9Sstevel@tonic-gate /*
8537c478bd9Sstevel@tonic-gate  * Initialize protocol control block. If a parent exists, inherit
8547c478bd9Sstevel@tonic-gate  * all values set through setsockopt().
8557c478bd9Sstevel@tonic-gate  */
8567c478bd9Sstevel@tonic-gate static int
8577c478bd9Sstevel@tonic-gate sctp_init_values(sctp_t *sctp, sctp_t *psctp, int sleep)
8587c478bd9Sstevel@tonic-gate {
8597c478bd9Sstevel@tonic-gate 	int	err;
8607c478bd9Sstevel@tonic-gate 	int	cnt;
8617c478bd9Sstevel@tonic-gate 
8627c478bd9Sstevel@tonic-gate 	ASSERT((sctp->sctp_family == AF_INET &&
8637c478bd9Sstevel@tonic-gate 	    sctp->sctp_ipversion == IPV4_VERSION) ||
8647c478bd9Sstevel@tonic-gate 	    (sctp->sctp_family == AF_INET6 &&
8657c478bd9Sstevel@tonic-gate 	    (sctp->sctp_ipversion == IPV4_VERSION ||
8667c478bd9Sstevel@tonic-gate 	    sctp->sctp_ipversion == IPV6_VERSION)));
8677c478bd9Sstevel@tonic-gate 
8687c478bd9Sstevel@tonic-gate 	sctp->sctp_nsaddrs = 0;
8697c478bd9Sstevel@tonic-gate 	for (cnt = 0; cnt < SCTP_IPIF_HASH; cnt++) {
8707c478bd9Sstevel@tonic-gate 		sctp->sctp_saddrs[cnt].ipif_count = 0;
8717c478bd9Sstevel@tonic-gate 		list_create(&sctp->sctp_saddrs[cnt].sctp_ipif_list,
8727c478bd9Sstevel@tonic-gate 		    sizeof (sctp_saddr_ipif_t), offsetof(sctp_saddr_ipif_t,
8737c478bd9Sstevel@tonic-gate 		    saddr_ipif));
8747c478bd9Sstevel@tonic-gate 	}
8757c478bd9Sstevel@tonic-gate 	sctp->sctp_ports = 0;
8767c478bd9Sstevel@tonic-gate 	sctp->sctp_running = B_FALSE;
8777c478bd9Sstevel@tonic-gate 	sctp->sctp_state = SCTPS_IDLE;
8787c478bd9Sstevel@tonic-gate 
8797c478bd9Sstevel@tonic-gate 	sctp->sctp_refcnt = 1;
8807c478bd9Sstevel@tonic-gate 
8817c478bd9Sstevel@tonic-gate 	sctp->sctp_strikes = 0;
8827c478bd9Sstevel@tonic-gate 
8837c478bd9Sstevel@tonic-gate 	sctp->sctp_last_mtu_probe = lbolt64;
8847c478bd9Sstevel@tonic-gate 	sctp->sctp_mtu_probe_intvl = sctp_mtu_probe_interval;
8857c478bd9Sstevel@tonic-gate 
8867c478bd9Sstevel@tonic-gate 	sctp->sctp_sack_gaps = 0;
8877c478bd9Sstevel@tonic-gate 	sctp->sctp_sack_toggle = 2;
8887c478bd9Sstevel@tonic-gate 
8897c478bd9Sstevel@tonic-gate 	if (psctp != NULL) {
8907c478bd9Sstevel@tonic-gate 		/*
8917c478bd9Sstevel@tonic-gate 		 * Inherit from parent
8927c478bd9Sstevel@tonic-gate 		 */
8937c478bd9Sstevel@tonic-gate 		sctp->sctp_iphc = kmem_zalloc(psctp->sctp_iphc_len,
8947c478bd9Sstevel@tonic-gate 		    KM_NOSLEEP);
8957c478bd9Sstevel@tonic-gate 		if (sctp->sctp_iphc == NULL)
8967c478bd9Sstevel@tonic-gate 			return (ENOMEM);
8977c478bd9Sstevel@tonic-gate 		sctp->sctp_iphc_len = psctp->sctp_iphc_len;
8987c478bd9Sstevel@tonic-gate 		sctp->sctp_hdr_len = psctp->sctp_hdr_len;
8997c478bd9Sstevel@tonic-gate 
9007c478bd9Sstevel@tonic-gate 		sctp->sctp_iphc6 = kmem_zalloc(psctp->sctp_iphc6_len,
9017c478bd9Sstevel@tonic-gate 		    KM_NOSLEEP);
9027c478bd9Sstevel@tonic-gate 		if (sctp->sctp_iphc6 == NULL) {
9037c478bd9Sstevel@tonic-gate 			sctp->sctp_iphc6_len = 0;
9047c478bd9Sstevel@tonic-gate 			return (ENOMEM);
9057c478bd9Sstevel@tonic-gate 		}
9067c478bd9Sstevel@tonic-gate 		sctp->sctp_iphc6_len = psctp->sctp_iphc6_len;
9077c478bd9Sstevel@tonic-gate 		sctp->sctp_hdr6_len = psctp->sctp_hdr6_len;
9087c478bd9Sstevel@tonic-gate 
9097c478bd9Sstevel@tonic-gate 		sctp->sctp_ip_hdr_len = psctp->sctp_ip_hdr_len;
9107c478bd9Sstevel@tonic-gate 		sctp->sctp_ip_hdr6_len = psctp->sctp_ip_hdr6_len;
9117c478bd9Sstevel@tonic-gate 
9127c478bd9Sstevel@tonic-gate 		/*
9137c478bd9Sstevel@tonic-gate 		 * Copy the IP+SCTP header templates from listener
9147c478bd9Sstevel@tonic-gate 		 */
9157c478bd9Sstevel@tonic-gate 		bcopy(psctp->sctp_iphc, sctp->sctp_iphc,
9167c478bd9Sstevel@tonic-gate 		    psctp->sctp_hdr_len);
9177c478bd9Sstevel@tonic-gate 		sctp->sctp_ipha = (ipha_t *)sctp->sctp_iphc;
9187c478bd9Sstevel@tonic-gate 		sctp->sctp_sctph = (sctp_hdr_t *)(sctp->sctp_iphc +
9197c478bd9Sstevel@tonic-gate 		    sctp->sctp_ip_hdr_len);
9207c478bd9Sstevel@tonic-gate 
9217c478bd9Sstevel@tonic-gate 		bcopy(psctp->sctp_iphc6, sctp->sctp_iphc6,
9227c478bd9Sstevel@tonic-gate 		    psctp->sctp_hdr6_len);
9237c478bd9Sstevel@tonic-gate 		if (((ip6i_t *)(sctp->sctp_iphc6))->ip6i_nxt == IPPROTO_RAW) {
9247c478bd9Sstevel@tonic-gate 			sctp->sctp_ip6h = (ip6_t *)(sctp->sctp_iphc6 +
9257c478bd9Sstevel@tonic-gate 			    sizeof (ip6i_t));
9267c478bd9Sstevel@tonic-gate 		} else {
9277c478bd9Sstevel@tonic-gate 			sctp->sctp_ip6h = (ip6_t *)sctp->sctp_iphc6;
9287c478bd9Sstevel@tonic-gate 		}
9297c478bd9Sstevel@tonic-gate 		sctp->sctp_sctph6 = (sctp_hdr_t *)(sctp->sctp_iphc6 +
9307c478bd9Sstevel@tonic-gate 		    sctp->sctp_ip_hdr6_len);
9317c478bd9Sstevel@tonic-gate 
9327c478bd9Sstevel@tonic-gate 		sctp->sctp_cookie_lifetime = psctp->sctp_cookie_lifetime;
9337c478bd9Sstevel@tonic-gate 		sctp->sctp_xmit_lowater = psctp->sctp_xmit_lowater;
9347c478bd9Sstevel@tonic-gate 		sctp->sctp_xmit_hiwater = psctp->sctp_xmit_hiwater;
9357c478bd9Sstevel@tonic-gate 		sctp->sctp_cwnd_max = psctp->sctp_cwnd_max;
9367c478bd9Sstevel@tonic-gate 		sctp->sctp_rwnd = psctp->sctp_rwnd;
9371d8c4025Svi 		sctp->sctp_irwnd = psctp->sctp_rwnd;
9387c478bd9Sstevel@tonic-gate 
9397c478bd9Sstevel@tonic-gate 		sctp->sctp_rto_max = psctp->sctp_rto_max;
9407c478bd9Sstevel@tonic-gate 		sctp->sctp_init_rto_max = psctp->sctp_init_rto_max;
9417c478bd9Sstevel@tonic-gate 		sctp->sctp_rto_min = psctp->sctp_rto_min;
9427c478bd9Sstevel@tonic-gate 		sctp->sctp_rto_initial = psctp->sctp_rto_initial;
9437c478bd9Sstevel@tonic-gate 		sctp->sctp_pa_max_rxt = psctp->sctp_pa_max_rxt;
9447c478bd9Sstevel@tonic-gate 		sctp->sctp_pp_max_rxt = psctp->sctp_pp_max_rxt;
9457c478bd9Sstevel@tonic-gate 		sctp->sctp_max_init_rxt = psctp->sctp_max_init_rxt;
9467c478bd9Sstevel@tonic-gate 
9477c478bd9Sstevel@tonic-gate 		sctp->sctp_def_stream = psctp->sctp_def_stream;
9487c478bd9Sstevel@tonic-gate 		sctp->sctp_def_flags = psctp->sctp_def_flags;
9497c478bd9Sstevel@tonic-gate 		sctp->sctp_def_ppid = psctp->sctp_def_ppid;
9507c478bd9Sstevel@tonic-gate 		sctp->sctp_def_context = psctp->sctp_def_context;
9517c478bd9Sstevel@tonic-gate 		sctp->sctp_def_timetolive = psctp->sctp_def_timetolive;
9527c478bd9Sstevel@tonic-gate 
9537c478bd9Sstevel@tonic-gate 		sctp->sctp_num_istr = psctp->sctp_num_istr;
9547c478bd9Sstevel@tonic-gate 		sctp->sctp_num_ostr = psctp->sctp_num_ostr;
9557c478bd9Sstevel@tonic-gate 
9567c478bd9Sstevel@tonic-gate 		sctp->sctp_hb_interval = psctp->sctp_hb_interval;
9577c478bd9Sstevel@tonic-gate 		sctp->sctp_autoclose = psctp->sctp_autoclose;
9587c478bd9Sstevel@tonic-gate 		sctp->sctp_tx_adaption_code = psctp->sctp_tx_adaption_code;
9597c478bd9Sstevel@tonic-gate 
9607c478bd9Sstevel@tonic-gate 		/* xxx should be a better way to copy these flags xxx */
9617c478bd9Sstevel@tonic-gate 		sctp->sctp_debug = psctp->sctp_debug;
9627c478bd9Sstevel@tonic-gate 		sctp->sctp_dontroute = psctp->sctp_dontroute;
9637c478bd9Sstevel@tonic-gate 		sctp->sctp_useloopback = psctp->sctp_useloopback;
9647c478bd9Sstevel@tonic-gate 		sctp->sctp_broadcast = psctp->sctp_broadcast;
9657c478bd9Sstevel@tonic-gate 		sctp->sctp_reuseaddr = psctp->sctp_reuseaddr;
9667c478bd9Sstevel@tonic-gate 		sctp->sctp_bound_to_all = psctp->sctp_bound_to_all;
9677c478bd9Sstevel@tonic-gate 		sctp->sctp_cansleep = psctp->sctp_cansleep;
9687c478bd9Sstevel@tonic-gate 		sctp->sctp_send_adaption = psctp->sctp_send_adaption;
9697c478bd9Sstevel@tonic-gate 		sctp->sctp_ndelay = psctp->sctp_ndelay;
9707c478bd9Sstevel@tonic-gate 		sctp->sctp_events = psctp->sctp_events;
9717c478bd9Sstevel@tonic-gate 		sctp->sctp_ipv6_recvancillary = psctp->sctp_ipv6_recvancillary;
9727c478bd9Sstevel@tonic-gate 	} else {
9737c478bd9Sstevel@tonic-gate 		/*
9747c478bd9Sstevel@tonic-gate 		 * Initialize the header template
9757c478bd9Sstevel@tonic-gate 		 */
9767c478bd9Sstevel@tonic-gate 		if ((err = sctp_header_init_ipv4(sctp, sleep)) != 0) {
9777c478bd9Sstevel@tonic-gate 			return (err);
9787c478bd9Sstevel@tonic-gate 		}
9797c478bd9Sstevel@tonic-gate 		if ((err = sctp_header_init_ipv6(sctp, sleep)) != 0) {
9807c478bd9Sstevel@tonic-gate 			return (err);
9817c478bd9Sstevel@tonic-gate 		}
9827c478bd9Sstevel@tonic-gate 
9837c478bd9Sstevel@tonic-gate 		/*
9847c478bd9Sstevel@tonic-gate 		 * Set to system defaults
9857c478bd9Sstevel@tonic-gate 		 */
9867c478bd9Sstevel@tonic-gate 		sctp->sctp_cookie_lifetime = MSEC_TO_TICK(sctp_cookie_life);
9877c478bd9Sstevel@tonic-gate 		sctp->sctp_xmit_lowater = sctp_xmit_lowat;
9887c478bd9Sstevel@tonic-gate 		sctp->sctp_xmit_hiwater = sctp_xmit_hiwat;
9897c478bd9Sstevel@tonic-gate 		sctp->sctp_cwnd_max = sctp_cwnd_max_;
9907c478bd9Sstevel@tonic-gate 		sctp->sctp_rwnd = sctp_recv_hiwat;
9911d8c4025Svi 		sctp->sctp_irwnd = sctp->sctp_rwnd;
9927c478bd9Sstevel@tonic-gate 		sctp->sctp_rto_max = MSEC_TO_TICK(sctp_rto_maxg);
9937c478bd9Sstevel@tonic-gate 		sctp->sctp_init_rto_max = sctp->sctp_rto_max;
9947c478bd9Sstevel@tonic-gate 		sctp->sctp_rto_min = MSEC_TO_TICK(sctp_rto_ming);
9957c478bd9Sstevel@tonic-gate 		sctp->sctp_rto_initial = MSEC_TO_TICK(sctp_rto_initialg);
9967c478bd9Sstevel@tonic-gate 		sctp->sctp_pa_max_rxt = sctp_pa_max_retr;
9977c478bd9Sstevel@tonic-gate 		sctp->sctp_pp_max_rxt = sctp_pp_max_retr;
9987c478bd9Sstevel@tonic-gate 		sctp->sctp_max_init_rxt = sctp_max_init_retr;
9997c478bd9Sstevel@tonic-gate 
10007c478bd9Sstevel@tonic-gate 		sctp->sctp_num_istr = sctp_max_in_streams;
10017c478bd9Sstevel@tonic-gate 		sctp->sctp_num_ostr = sctp_initial_out_streams;
10027c478bd9Sstevel@tonic-gate 
10037c478bd9Sstevel@tonic-gate 		sctp->sctp_hb_interval = MSEC_TO_TICK(sctp_heartbeat_interval);
10047c478bd9Sstevel@tonic-gate 	}
10057c478bd9Sstevel@tonic-gate 	sctp->sctp_understands_asconf = B_TRUE;
10067c478bd9Sstevel@tonic-gate 	sctp->sctp_understands_addip = B_TRUE;
10077c478bd9Sstevel@tonic-gate 	sctp->sctp_prsctp_aware = B_FALSE;
10087c478bd9Sstevel@tonic-gate 
10097c478bd9Sstevel@tonic-gate 	sctp->sctp_connp->conn_ref = 1;
10107c478bd9Sstevel@tonic-gate 	sctp->sctp_connp->conn_fully_bound = B_FALSE;
10117c478bd9Sstevel@tonic-gate 
10127c478bd9Sstevel@tonic-gate 	sctp->sctp_prsctpdrop = 0;
10137c478bd9Sstevel@tonic-gate 	sctp->sctp_msgcount = 0;
10147c478bd9Sstevel@tonic-gate 
10157c478bd9Sstevel@tonic-gate 	return (0);
10167c478bd9Sstevel@tonic-gate }
10177c478bd9Sstevel@tonic-gate 
10187c478bd9Sstevel@tonic-gate /*
10197c478bd9Sstevel@tonic-gate  * Extracts the init tag from an INIT chunk and checks if it matches
10207c478bd9Sstevel@tonic-gate  * the sctp's verification tag. Returns 0 if it doesn't match, 1 if
10217c478bd9Sstevel@tonic-gate  * it does.
10227c478bd9Sstevel@tonic-gate  */
10237c478bd9Sstevel@tonic-gate static boolean_t
10247c478bd9Sstevel@tonic-gate sctp_icmp_verf(sctp_t *sctp, sctp_hdr_t *sh, mblk_t *mp)
10257c478bd9Sstevel@tonic-gate {
10267c478bd9Sstevel@tonic-gate 	sctp_chunk_hdr_t *sch;
10277c478bd9Sstevel@tonic-gate 	uint32_t verf, *vp;
10287c478bd9Sstevel@tonic-gate 
10297c478bd9Sstevel@tonic-gate 	sch = (sctp_chunk_hdr_t *)(sh + 1);
10307c478bd9Sstevel@tonic-gate 	vp = (uint32_t *)(sch + 1);
10317c478bd9Sstevel@tonic-gate 
10327c478bd9Sstevel@tonic-gate 	/* Need at least the data chunk hdr and the first 4 bytes of INIT */
10337c478bd9Sstevel@tonic-gate 	if ((unsigned char *)(vp + 1) > mp->b_wptr) {
10347c478bd9Sstevel@tonic-gate 		return (B_FALSE);
10357c478bd9Sstevel@tonic-gate 	}
10367c478bd9Sstevel@tonic-gate 
10377c478bd9Sstevel@tonic-gate 	bcopy(vp, &verf, sizeof (verf));
10387c478bd9Sstevel@tonic-gate 
10397c478bd9Sstevel@tonic-gate 	if (verf == sctp->sctp_lvtag) {
10407c478bd9Sstevel@tonic-gate 		return (B_TRUE);
10417c478bd9Sstevel@tonic-gate 	}
10427c478bd9Sstevel@tonic-gate 	return (B_FALSE);
10437c478bd9Sstevel@tonic-gate }
10447c478bd9Sstevel@tonic-gate 
10457c478bd9Sstevel@tonic-gate /*
10467c478bd9Sstevel@tonic-gate  * sctp_icmp_error is called by sctp_input() to process ICMP error messages
10477c478bd9Sstevel@tonic-gate  * passed up by IP.  The queue is the default queue.  We need to find a sctp_t
10487c478bd9Sstevel@tonic-gate  * that corresponds to the returned datagram.  Passes the message back in on
10497c478bd9Sstevel@tonic-gate  * the correct queue once it has located the connection.
10507c478bd9Sstevel@tonic-gate  * Assumes that IP has pulled up everything up to and including
10517c478bd9Sstevel@tonic-gate  * the ICMP header.
10527c478bd9Sstevel@tonic-gate  */
10537c478bd9Sstevel@tonic-gate void
10547c478bd9Sstevel@tonic-gate sctp_icmp_error(sctp_t *sctp, mblk_t *mp)
10557c478bd9Sstevel@tonic-gate {
10567c478bd9Sstevel@tonic-gate 	icmph_t *icmph;
10577c478bd9Sstevel@tonic-gate 	ipha_t	*ipha;
10587c478bd9Sstevel@tonic-gate 	int	iph_hdr_length;
10597c478bd9Sstevel@tonic-gate 	sctp_hdr_t *sctph;
10607c478bd9Sstevel@tonic-gate 	mblk_t *first_mp;
10617c478bd9Sstevel@tonic-gate 	uint32_t new_mtu;
10627c478bd9Sstevel@tonic-gate 	in6_addr_t dst;
10637c478bd9Sstevel@tonic-gate 	sctp_faddr_t *fp;
10647c478bd9Sstevel@tonic-gate 
10657c478bd9Sstevel@tonic-gate 	dprint(1, ("sctp_icmp_error: sctp=%p, mp=%p\n", sctp, mp));
10667c478bd9Sstevel@tonic-gate 
10677c478bd9Sstevel@tonic-gate 	first_mp = mp;
10687c478bd9Sstevel@tonic-gate 
10697c478bd9Sstevel@tonic-gate 	ipha = (ipha_t *)mp->b_rptr;
10707c478bd9Sstevel@tonic-gate 	if (IPH_HDR_VERSION(ipha) != IPV4_VERSION) {
10717c478bd9Sstevel@tonic-gate 		ASSERT(IPH_HDR_VERSION(ipha) == IPV6_VERSION);
10727c478bd9Sstevel@tonic-gate 		sctp_icmp_error_ipv6(sctp, first_mp);
10737c478bd9Sstevel@tonic-gate 		return;
10747c478bd9Sstevel@tonic-gate 	}
10757c478bd9Sstevel@tonic-gate 
10767c478bd9Sstevel@tonic-gate 	iph_hdr_length = IPH_HDR_LENGTH(ipha);
10777c478bd9Sstevel@tonic-gate 	icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length];
10787c478bd9Sstevel@tonic-gate 	ipha = (ipha_t *)&icmph[1];
10797c478bd9Sstevel@tonic-gate 	iph_hdr_length = IPH_HDR_LENGTH(ipha);
10807c478bd9Sstevel@tonic-gate 	sctph = (sctp_hdr_t *)((char *)ipha + iph_hdr_length);
10817c478bd9Sstevel@tonic-gate 	if ((uchar_t *)(sctph + 1) >= mp->b_wptr) {
10827c478bd9Sstevel@tonic-gate 		/* not enough data for SCTP header */
10837c478bd9Sstevel@tonic-gate 		freemsg(first_mp);
10847c478bd9Sstevel@tonic-gate 		return;
10857c478bd9Sstevel@tonic-gate 	}
10867c478bd9Sstevel@tonic-gate 
10877c478bd9Sstevel@tonic-gate 	switch (icmph->icmph_type) {
10887c478bd9Sstevel@tonic-gate 	case ICMP_DEST_UNREACHABLE:
10897c478bd9Sstevel@tonic-gate 		switch (icmph->icmph_code) {
10907c478bd9Sstevel@tonic-gate 		case ICMP_FRAGMENTATION_NEEDED:
10917c478bd9Sstevel@tonic-gate 			/*
10927c478bd9Sstevel@tonic-gate 			 * Reduce the MSS based on the new MTU.  This will
10937c478bd9Sstevel@tonic-gate 			 * eliminate any fragmentation locally.
10947c478bd9Sstevel@tonic-gate 			 * N.B.  There may well be some funny side-effects on
10957c478bd9Sstevel@tonic-gate 			 * the local send policy and the remote receive policy.
10967c478bd9Sstevel@tonic-gate 			 * Pending further research, we provide
10977c478bd9Sstevel@tonic-gate 			 * sctp_ignore_path_mtu just in case this proves
10987c478bd9Sstevel@tonic-gate 			 * disastrous somewhere.
10997c478bd9Sstevel@tonic-gate 			 *
11007c478bd9Sstevel@tonic-gate 			 * After updating the MSS, retransmit part of the
11017c478bd9Sstevel@tonic-gate 			 * dropped segment using the new mss by calling
11027c478bd9Sstevel@tonic-gate 			 * sctp_wput_slow().  Need to adjust all those
11037c478bd9Sstevel@tonic-gate 			 * params to make sure sctp_wput_slow() work properly.
11047c478bd9Sstevel@tonic-gate 			 */
11057c478bd9Sstevel@tonic-gate 			if (sctp_ignore_path_mtu)
11067c478bd9Sstevel@tonic-gate 				break;
11077c478bd9Sstevel@tonic-gate 
11087c478bd9Sstevel@tonic-gate 			/* find the offending faddr */
11097c478bd9Sstevel@tonic-gate 			IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, &dst);
11107c478bd9Sstevel@tonic-gate 			fp = sctp_lookup_faddr(sctp, &dst);
11117c478bd9Sstevel@tonic-gate 			if (fp == NULL) {
11127c478bd9Sstevel@tonic-gate 				break;
11137c478bd9Sstevel@tonic-gate 			}
11147c478bd9Sstevel@tonic-gate 
11157c478bd9Sstevel@tonic-gate 			new_mtu = ntohs(icmph->icmph_du_mtu);
11167c478bd9Sstevel@tonic-gate 
11177c478bd9Sstevel@tonic-gate 			if (new_mtu - sctp->sctp_hdr_len >= fp->sfa_pmss)
11187c478bd9Sstevel@tonic-gate 				break;
11197c478bd9Sstevel@tonic-gate 
11207c478bd9Sstevel@tonic-gate 			/*
11217c478bd9Sstevel@tonic-gate 			 * Make sure that sfa_pmss is a multiple of
11227c478bd9Sstevel@tonic-gate 			 * SCTP_ALIGN.
11237c478bd9Sstevel@tonic-gate 			 */
11247c478bd9Sstevel@tonic-gate 			fp->sfa_pmss = (new_mtu - sctp->sctp_hdr_len) &
11257c478bd9Sstevel@tonic-gate 				~(SCTP_ALIGN - 1);
11267c478bd9Sstevel@tonic-gate 			fp->pmtu_discovered = 1;
11277c478bd9Sstevel@tonic-gate 
11287c478bd9Sstevel@tonic-gate 			break;
11297c478bd9Sstevel@tonic-gate 		case ICMP_PORT_UNREACHABLE:
11307c478bd9Sstevel@tonic-gate 		case ICMP_PROTOCOL_UNREACHABLE:
11317c478bd9Sstevel@tonic-gate 			switch (sctp->sctp_state) {
11327c478bd9Sstevel@tonic-gate 			case SCTPS_COOKIE_WAIT:
11337c478bd9Sstevel@tonic-gate 			case SCTPS_COOKIE_ECHOED:
11347c478bd9Sstevel@tonic-gate 				/* make sure the verification tag matches */
11357c478bd9Sstevel@tonic-gate 				if (!sctp_icmp_verf(sctp, sctph, mp)) {
11367c478bd9Sstevel@tonic-gate 					break;
11377c478bd9Sstevel@tonic-gate 				}
11387c478bd9Sstevel@tonic-gate 				BUMP_MIB(&sctp_mib, sctpAborted);
11397c478bd9Sstevel@tonic-gate 				sctp_clean_death(sctp, ECONNREFUSED);
11407c478bd9Sstevel@tonic-gate 				break;
11417c478bd9Sstevel@tonic-gate 			}
11427c478bd9Sstevel@tonic-gate 			break;
11437c478bd9Sstevel@tonic-gate 		case ICMP_HOST_UNREACHABLE:
11447c478bd9Sstevel@tonic-gate 		case ICMP_NET_UNREACHABLE:
11457c478bd9Sstevel@tonic-gate 			/* Record the error in case we finally time out. */
11467c478bd9Sstevel@tonic-gate 			sctp->sctp_client_errno = (icmph->icmph_code ==
11477c478bd9Sstevel@tonic-gate 			    ICMP_HOST_UNREACHABLE) ? EHOSTUNREACH : ENETUNREACH;
11487c478bd9Sstevel@tonic-gate 			break;
11497c478bd9Sstevel@tonic-gate 		default:
11507c478bd9Sstevel@tonic-gate 			break;
11517c478bd9Sstevel@tonic-gate 		}
11527c478bd9Sstevel@tonic-gate 		break;
11537c478bd9Sstevel@tonic-gate 	case ICMP_SOURCE_QUENCH: {
11547c478bd9Sstevel@tonic-gate 		/* Reduce the sending rate as if we got a retransmit timeout */
11557c478bd9Sstevel@tonic-gate 		break;
11567c478bd9Sstevel@tonic-gate 	}
11577c478bd9Sstevel@tonic-gate 	}
11587c478bd9Sstevel@tonic-gate 	freemsg(first_mp);
11597c478bd9Sstevel@tonic-gate }
11607c478bd9Sstevel@tonic-gate 
11617c478bd9Sstevel@tonic-gate /*
11627c478bd9Sstevel@tonic-gate  * sctp_icmp_error_ipv6() is called by sctp_icmp_error() to process ICMPv6
11637c478bd9Sstevel@tonic-gate  * error messages passed up by IP.
11647c478bd9Sstevel@tonic-gate  * Assumes that IP has pulled up all the extension headers as well
11657c478bd9Sstevel@tonic-gate  * as the ICMPv6 header.
11667c478bd9Sstevel@tonic-gate  */
11677c478bd9Sstevel@tonic-gate static void
11687c478bd9Sstevel@tonic-gate sctp_icmp_error_ipv6(sctp_t *sctp, mblk_t *mp)
11697c478bd9Sstevel@tonic-gate {
11707c478bd9Sstevel@tonic-gate 	icmp6_t *icmp6;
11717c478bd9Sstevel@tonic-gate 	ip6_t	*ip6h;
11727c478bd9Sstevel@tonic-gate 	uint16_t	iph_hdr_length;
11737c478bd9Sstevel@tonic-gate 	sctp_hdr_t *sctpha;
11747c478bd9Sstevel@tonic-gate 	uint8_t	*nexthdrp;
11757c478bd9Sstevel@tonic-gate 	uint32_t new_mtu;
11767c478bd9Sstevel@tonic-gate 	sctp_faddr_t *fp;
11777c478bd9Sstevel@tonic-gate 
11787c478bd9Sstevel@tonic-gate 	ip6h = (ip6_t *)mp->b_rptr;
11797c478bd9Sstevel@tonic-gate 	iph_hdr_length = (ip6h->ip6_nxt != IPPROTO_SCTP) ?
11807c478bd9Sstevel@tonic-gate 	    ip_hdr_length_v6(mp, ip6h) : IPV6_HDR_LEN;
11817c478bd9Sstevel@tonic-gate 
11827c478bd9Sstevel@tonic-gate 	icmp6 = (icmp6_t *)&mp->b_rptr[iph_hdr_length];
11837c478bd9Sstevel@tonic-gate 	ip6h = (ip6_t *)&icmp6[1];
11847c478bd9Sstevel@tonic-gate 	if (!ip_hdr_length_nexthdr_v6(mp, ip6h, &iph_hdr_length, &nexthdrp)) {
11857c478bd9Sstevel@tonic-gate 		freemsg(mp);
11867c478bd9Sstevel@tonic-gate 		return;
11877c478bd9Sstevel@tonic-gate 	}
11887c478bd9Sstevel@tonic-gate 	ASSERT(*nexthdrp == IPPROTO_SCTP);
11897c478bd9Sstevel@tonic-gate 
11907c478bd9Sstevel@tonic-gate 	/* XXX need ifindex to find connection */
11917c478bd9Sstevel@tonic-gate 	sctpha = (sctp_hdr_t *)((char *)ip6h + iph_hdr_length);
11927c478bd9Sstevel@tonic-gate 	if ((uchar_t *)sctpha >= mp->b_wptr) {
11937c478bd9Sstevel@tonic-gate 		/* not enough data for SCTP header */
11947c478bd9Sstevel@tonic-gate 		freemsg(mp);
11957c478bd9Sstevel@tonic-gate 		return;
11967c478bd9Sstevel@tonic-gate 	}
11977c478bd9Sstevel@tonic-gate 	switch (icmp6->icmp6_type) {
11987c478bd9Sstevel@tonic-gate 	case ICMP6_PACKET_TOO_BIG:
11997c478bd9Sstevel@tonic-gate 		/*
12007c478bd9Sstevel@tonic-gate 		 * Reduce the MSS based on the new MTU.  This will
12017c478bd9Sstevel@tonic-gate 		 * eliminate any fragmentation locally.
12027c478bd9Sstevel@tonic-gate 		 * N.B.  There may well be some funny side-effects on
12037c478bd9Sstevel@tonic-gate 		 * the local send policy and the remote receive policy.
12047c478bd9Sstevel@tonic-gate 		 * Pending further research, we provide
12057c478bd9Sstevel@tonic-gate 		 * sctp_ignore_path_mtu just in case this proves
12067c478bd9Sstevel@tonic-gate 		 * disastrous somewhere.
12077c478bd9Sstevel@tonic-gate 		 *
12087c478bd9Sstevel@tonic-gate 		 * After updating the MSS, retransmit part of the
12097c478bd9Sstevel@tonic-gate 		 * dropped segment using the new mss by calling
12107c478bd9Sstevel@tonic-gate 		 * sctp_wput_slow().  Need to adjust all those
12117c478bd9Sstevel@tonic-gate 		 * params to make sure sctp_wput_slow() work properly.
12127c478bd9Sstevel@tonic-gate 		 */
12137c478bd9Sstevel@tonic-gate 		if (sctp_ignore_path_mtu)
12147c478bd9Sstevel@tonic-gate 			break;
12157c478bd9Sstevel@tonic-gate 
12167c478bd9Sstevel@tonic-gate 		/* find the offending faddr */
12177c478bd9Sstevel@tonic-gate 		fp = sctp_lookup_faddr(sctp, &ip6h->ip6_dst);
12187c478bd9Sstevel@tonic-gate 		if (fp == NULL) {
12197c478bd9Sstevel@tonic-gate 			break;
12207c478bd9Sstevel@tonic-gate 		}
12217c478bd9Sstevel@tonic-gate 
12227c478bd9Sstevel@tonic-gate 		new_mtu = ntohs(icmp6->icmp6_mtu);
12237c478bd9Sstevel@tonic-gate 
12247c478bd9Sstevel@tonic-gate 		if (new_mtu - sctp->sctp_hdr6_len >= fp->sfa_pmss)
12257c478bd9Sstevel@tonic-gate 			break;
12267c478bd9Sstevel@tonic-gate 
12277c478bd9Sstevel@tonic-gate 		/* Make sure that sfa_pmss is a multiple of SCTP_ALIGN. */
12287c478bd9Sstevel@tonic-gate 		fp->sfa_pmss = (new_mtu - sctp->sctp_hdr6_len) &
12297c478bd9Sstevel@tonic-gate 			~(SCTP_ALIGN - 1);
12307c478bd9Sstevel@tonic-gate 		fp->pmtu_discovered = 1;
12317c478bd9Sstevel@tonic-gate 
12327c478bd9Sstevel@tonic-gate 		break;
12337c478bd9Sstevel@tonic-gate 
12347c478bd9Sstevel@tonic-gate 	case ICMP6_DST_UNREACH:
12357c478bd9Sstevel@tonic-gate 		switch (icmp6->icmp6_code) {
12367c478bd9Sstevel@tonic-gate 		case ICMP6_DST_UNREACH_NOPORT:
12377c478bd9Sstevel@tonic-gate 			/* make sure the verification tag matches */
12387c478bd9Sstevel@tonic-gate 			if (!sctp_icmp_verf(sctp, sctpha, mp)) {
12397c478bd9Sstevel@tonic-gate 				break;
12407c478bd9Sstevel@tonic-gate 			}
12417c478bd9Sstevel@tonic-gate 			if (sctp->sctp_state == SCTPS_COOKIE_WAIT ||
12427c478bd9Sstevel@tonic-gate 			    sctp->sctp_state == SCTPS_COOKIE_ECHOED) {
12437c478bd9Sstevel@tonic-gate 				BUMP_MIB(&sctp_mib, sctpAborted);
12447c478bd9Sstevel@tonic-gate 				sctp_clean_death(sctp, ECONNREFUSED);
12457c478bd9Sstevel@tonic-gate 			}
12467c478bd9Sstevel@tonic-gate 			break;
12477c478bd9Sstevel@tonic-gate 
12487c478bd9Sstevel@tonic-gate 		case ICMP6_DST_UNREACH_ADMIN:
12497c478bd9Sstevel@tonic-gate 		case ICMP6_DST_UNREACH_NOROUTE:
12507c478bd9Sstevel@tonic-gate 		case ICMP6_DST_UNREACH_NOTNEIGHBOR:
12517c478bd9Sstevel@tonic-gate 		case ICMP6_DST_UNREACH_ADDR:
12527c478bd9Sstevel@tonic-gate 			/* Record the error in case we finally time out. */
12537c478bd9Sstevel@tonic-gate 			sctp->sctp_client_errno = EHOSTUNREACH;
12547c478bd9Sstevel@tonic-gate 			break;
12557c478bd9Sstevel@tonic-gate 		default:
12567c478bd9Sstevel@tonic-gate 			break;
12577c478bd9Sstevel@tonic-gate 		}
12587c478bd9Sstevel@tonic-gate 		break;
12597c478bd9Sstevel@tonic-gate 
12607c478bd9Sstevel@tonic-gate 	case ICMP6_PARAM_PROB:
12617c478bd9Sstevel@tonic-gate 		/* If this corresponds to an ICMP_PROTOCOL_UNREACHABLE */
12627c478bd9Sstevel@tonic-gate 		if (icmp6->icmp6_code == ICMP6_PARAMPROB_NEXTHEADER &&
12637c478bd9Sstevel@tonic-gate 		    (uchar_t *)ip6h + icmp6->icmp6_pptr ==
12647c478bd9Sstevel@tonic-gate 		    (uchar_t *)nexthdrp) {
12657c478bd9Sstevel@tonic-gate 			/* make sure the verification tag matches */
12667c478bd9Sstevel@tonic-gate 			if (!sctp_icmp_verf(sctp, sctpha, mp)) {
12677c478bd9Sstevel@tonic-gate 				break;
12687c478bd9Sstevel@tonic-gate 			}
12697c478bd9Sstevel@tonic-gate 			if (sctp->sctp_state == SCTPS_COOKIE_WAIT) {
12707c478bd9Sstevel@tonic-gate 				BUMP_MIB(&sctp_mib, sctpAborted);
12717c478bd9Sstevel@tonic-gate 				sctp_clean_death(sctp, ECONNREFUSED);
12727c478bd9Sstevel@tonic-gate 			}
12737c478bd9Sstevel@tonic-gate 			break;
12747c478bd9Sstevel@tonic-gate 		}
12757c478bd9Sstevel@tonic-gate 		break;
12767c478bd9Sstevel@tonic-gate 
12777c478bd9Sstevel@tonic-gate 	case ICMP6_TIME_EXCEEDED:
12787c478bd9Sstevel@tonic-gate 	default:
12797c478bd9Sstevel@tonic-gate 		break;
12807c478bd9Sstevel@tonic-gate 	}
12817c478bd9Sstevel@tonic-gate 	freemsg(mp);
12827c478bd9Sstevel@tonic-gate }
12837c478bd9Sstevel@tonic-gate 
12847c478bd9Sstevel@tonic-gate /*
12857c478bd9Sstevel@tonic-gate  * Called by sockfs to create a new sctp instance.
12867c478bd9Sstevel@tonic-gate  *
12877c478bd9Sstevel@tonic-gate  * If parent pointer is passed in, inherit settings from it.
12887c478bd9Sstevel@tonic-gate  */
12897c478bd9Sstevel@tonic-gate sctp_t *
12907c478bd9Sstevel@tonic-gate sctp_create(void *sctp_ulpd, sctp_t *parent, int family, int flags,
12917c478bd9Sstevel@tonic-gate     const sctp_upcalls_t *sctp_upcalls, sctp_sockbuf_limits_t *sbl,
12927c478bd9Sstevel@tonic-gate     cred_t *credp)
12937c478bd9Sstevel@tonic-gate {
12947c478bd9Sstevel@tonic-gate 	sctp_t		*sctp, *psctp;
12957c478bd9Sstevel@tonic-gate 	conn_t		*sctp_connp;
12967c478bd9Sstevel@tonic-gate 	mblk_t		*ack_mp, *hb_mp;
12977c478bd9Sstevel@tonic-gate 	int		sleep = flags & SCTP_CAN_BLOCK ? KM_SLEEP : KM_NOSLEEP;
12987c478bd9Sstevel@tonic-gate 
12997c478bd9Sstevel@tonic-gate 	/* User must supply a credential. */
13007c478bd9Sstevel@tonic-gate 	if (credp == NULL)
13017c478bd9Sstevel@tonic-gate 		return (NULL);
13027c478bd9Sstevel@tonic-gate 
13037c478bd9Sstevel@tonic-gate 	if ((sctp_connp = ipcl_conn_create(IPCL_SCTPCONN, sleep)) == NULL)
13047c478bd9Sstevel@tonic-gate 		return (NULL);
13057c478bd9Sstevel@tonic-gate 	psctp = (sctp_t *)parent;
13067c478bd9Sstevel@tonic-gate 
13077c478bd9Sstevel@tonic-gate 	sctp = CONN2SCTP(sctp_connp);
13087c478bd9Sstevel@tonic-gate 
13097c478bd9Sstevel@tonic-gate 	if ((ack_mp = sctp_timer_alloc(sctp, sctp_ack_timer)) == NULL ||
13107c478bd9Sstevel@tonic-gate 	    (hb_mp = sctp_timer_alloc(sctp, sctp_heartbeat_timer)) == NULL) {
13117c478bd9Sstevel@tonic-gate 		if (ack_mp != NULL)
13127c478bd9Sstevel@tonic-gate 			freeb(ack_mp);
13137c478bd9Sstevel@tonic-gate 		kmem_cache_free(sctp_conn_cache, sctp_connp);
13147c478bd9Sstevel@tonic-gate 		return (NULL);
13157c478bd9Sstevel@tonic-gate 	}
13167c478bd9Sstevel@tonic-gate 
13177c478bd9Sstevel@tonic-gate 	sctp->sctp_ack_mp = ack_mp;
13187c478bd9Sstevel@tonic-gate 	sctp->sctp_heartbeat_mp = hb_mp;
13197c478bd9Sstevel@tonic-gate 
13207c478bd9Sstevel@tonic-gate 	switch (family) {
13217c478bd9Sstevel@tonic-gate 	case AF_INET6:
13227c478bd9Sstevel@tonic-gate 		sctp_connp->conn_af_isv6 = B_TRUE;
13237c478bd9Sstevel@tonic-gate 		sctp->sctp_ipversion = IPV6_VERSION;
13247c478bd9Sstevel@tonic-gate 		sctp->sctp_family = AF_INET6;
13257c478bd9Sstevel@tonic-gate 		break;
13267c478bd9Sstevel@tonic-gate 
13277c478bd9Sstevel@tonic-gate 	case AF_INET:
13287c478bd9Sstevel@tonic-gate 		sctp_connp->conn_af_isv6 = B_FALSE;
13297c478bd9Sstevel@tonic-gate 		sctp_connp->conn_pkt_isv6 = B_FALSE;
13307c478bd9Sstevel@tonic-gate 		sctp->sctp_ipversion = IPV4_VERSION;
13317c478bd9Sstevel@tonic-gate 		sctp->sctp_family = AF_INET;
13327c478bd9Sstevel@tonic-gate 		break;
13337c478bd9Sstevel@tonic-gate 	default:
13347c478bd9Sstevel@tonic-gate 		ASSERT(0);
13357c478bd9Sstevel@tonic-gate 		break;
13367c478bd9Sstevel@tonic-gate 	}
13377c478bd9Sstevel@tonic-gate 	if (sctp_init_values(sctp, psctp, sleep) != 0) {
13387c478bd9Sstevel@tonic-gate 		freeb(ack_mp);
13397c478bd9Sstevel@tonic-gate 		freeb(hb_mp);
13407c478bd9Sstevel@tonic-gate 		kmem_cache_free(sctp_conn_cache, sctp_connp);
13417c478bd9Sstevel@tonic-gate 		return (NULL);
13427c478bd9Sstevel@tonic-gate 	}
13437c478bd9Sstevel@tonic-gate 	sctp->sctp_cansleep = ((flags & SCTP_CAN_BLOCK) == SCTP_CAN_BLOCK);
13447c478bd9Sstevel@tonic-gate 
13457c478bd9Sstevel@tonic-gate 	sctp->sctp_mss = sctp_initial_mtu - ((family == AF_INET6) ?
13467c478bd9Sstevel@tonic-gate 		sctp->sctp_hdr6_len : sctp->sctp_hdr_len);
13477c478bd9Sstevel@tonic-gate 
13487c478bd9Sstevel@tonic-gate 	if (psctp != NULL) {
13497c478bd9Sstevel@tonic-gate 		RUN_SCTP(psctp);
13507c478bd9Sstevel@tonic-gate 		/*
13517c478bd9Sstevel@tonic-gate 		 * Inherit local address list, local port. Parent is either
13527c478bd9Sstevel@tonic-gate 		 * in SCTPS_BOUND, or SCTPS_LISTEN state.
13537c478bd9Sstevel@tonic-gate 		 */
13547c478bd9Sstevel@tonic-gate 		ASSERT((psctp->sctp_state == SCTPS_BOUND) ||
13557c478bd9Sstevel@tonic-gate 		    (psctp->sctp_state == SCTPS_LISTEN));
13567c478bd9Sstevel@tonic-gate 		if (sctp_dup_saddrs(psctp, sctp, sleep)) {
13577c478bd9Sstevel@tonic-gate 			WAKE_SCTP(psctp);
13587c478bd9Sstevel@tonic-gate 			freeb(ack_mp);
13597c478bd9Sstevel@tonic-gate 			freeb(hb_mp);
13607c478bd9Sstevel@tonic-gate 			sctp_headers_free(sctp);
13617c478bd9Sstevel@tonic-gate 			kmem_cache_free(sctp_conn_cache, sctp_connp);
13627c478bd9Sstevel@tonic-gate 			return (NULL);
13637c478bd9Sstevel@tonic-gate 		}
13647c478bd9Sstevel@tonic-gate 
13657c478bd9Sstevel@tonic-gate 		/*
13667c478bd9Sstevel@tonic-gate 		 * If the parent is specified, it'll be immediatelly
13677c478bd9Sstevel@tonic-gate 		 * followed by sctp_connect(). So don't add this guy to
13687c478bd9Sstevel@tonic-gate 		 * bind hash.
13697c478bd9Sstevel@tonic-gate 		 */
13707c478bd9Sstevel@tonic-gate 		sctp->sctp_lport = psctp->sctp_lport;
13717c478bd9Sstevel@tonic-gate 		sctp->sctp_state = SCTPS_BOUND;
13727c478bd9Sstevel@tonic-gate 		sctp->sctp_zoneid = psctp->sctp_zoneid;
13737c478bd9Sstevel@tonic-gate 		WAKE_SCTP(psctp);
13747c478bd9Sstevel@tonic-gate 	} else {
13757c478bd9Sstevel@tonic-gate 		sctp->sctp_zoneid = getzoneid();
13767c478bd9Sstevel@tonic-gate 	}
13777c478bd9Sstevel@tonic-gate 
13787c478bd9Sstevel@tonic-gate 	sctp_connp->conn_cred = credp;
13797c478bd9Sstevel@tonic-gate 	crhold(credp);
13807c478bd9Sstevel@tonic-gate 
13817c478bd9Sstevel@tonic-gate 	/* Initialize SCTP instance values,  our verf tag must never be 0 */
13827c478bd9Sstevel@tonic-gate 	(void) random_get_pseudo_bytes((uint8_t *)&sctp->sctp_lvtag,
13837c478bd9Sstevel@tonic-gate 	    sizeof (sctp->sctp_lvtag));
13847c478bd9Sstevel@tonic-gate 	if (sctp->sctp_lvtag == 0)
13857c478bd9Sstevel@tonic-gate 		sctp->sctp_lvtag = (uint32_t)gethrtime();
13867c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_lvtag != 0);
13877c478bd9Sstevel@tonic-gate 
13887c478bd9Sstevel@tonic-gate 	sctp->sctp_ltsn = sctp->sctp_lvtag + 1;
13897c478bd9Sstevel@tonic-gate 	sctp->sctp_lcsn = sctp->sctp_ltsn;
13907c478bd9Sstevel@tonic-gate 	sctp->sctp_recovery_tsn = sctp->sctp_lastack_rxd = sctp->sctp_ltsn - 1;
13917c478bd9Sstevel@tonic-gate 	sctp->sctp_adv_pap = sctp->sctp_lastack_rxd;
13927c478bd9Sstevel@tonic-gate 
13937c478bd9Sstevel@tonic-gate 	/* Information required by upper layer */
13947c478bd9Sstevel@tonic-gate 	if (sctp_ulpd != NULL) {
13957c478bd9Sstevel@tonic-gate 		sctp->sctp_ulpd = sctp_ulpd;
13967c478bd9Sstevel@tonic-gate 
13977c478bd9Sstevel@tonic-gate 		ASSERT(sctp_upcalls != NULL);
13987c478bd9Sstevel@tonic-gate 		bcopy(sctp_upcalls, &sctp->sctp_upcalls,
13997c478bd9Sstevel@tonic-gate 		    sizeof (sctp_upcalls_t));
14007c478bd9Sstevel@tonic-gate 		ASSERT(sbl != NULL);
14017c478bd9Sstevel@tonic-gate 		/* Fill in the socket buffer limits for sctpsockfs */
14027c478bd9Sstevel@tonic-gate 		sbl->sbl_txlowat = sctp->sctp_xmit_lowater;
14037c478bd9Sstevel@tonic-gate 		sbl->sbl_txbuf = sctp->sctp_xmit_hiwater;
14047c478bd9Sstevel@tonic-gate 		sbl->sbl_rxbuf = sctp->sctp_rwnd;
14057c478bd9Sstevel@tonic-gate 		sbl->sbl_rxlowat = SCTP_RECV_LOWATER;
14067c478bd9Sstevel@tonic-gate 	}
14077c478bd9Sstevel@tonic-gate 	/* If no sctp_ulpd, must be creating the default sctp */
14087c478bd9Sstevel@tonic-gate 	ASSERT(sctp_ulpd != NULL || gsctp == NULL);
14097c478bd9Sstevel@tonic-gate 
14107c478bd9Sstevel@tonic-gate 	/* Insert this in the global list. */
14117c478bd9Sstevel@tonic-gate 	SCTP_LINK(sctp);
14127c478bd9Sstevel@tonic-gate 
14137c478bd9Sstevel@tonic-gate 	return (sctp);
14147c478bd9Sstevel@tonic-gate }
14157c478bd9Sstevel@tonic-gate 
14167c478bd9Sstevel@tonic-gate void
14177c478bd9Sstevel@tonic-gate sctp_ddi_init(void)
14187c478bd9Sstevel@tonic-gate {
14197c478bd9Sstevel@tonic-gate 	/* Initialize locks */
14207c478bd9Sstevel@tonic-gate 	mutex_init(&sctp_g_lock, NULL, MUTEX_DEFAULT, NULL);
14217c478bd9Sstevel@tonic-gate 	mutex_init(&sctp_epriv_port_lock, NULL, MUTEX_DEFAULT, NULL);
14227c478bd9Sstevel@tonic-gate 
14237c478bd9Sstevel@tonic-gate 	/* Initialize SCTP hash arrays. */
14247c478bd9Sstevel@tonic-gate 	sctp_hash_init();
14257c478bd9Sstevel@tonic-gate 
14267c478bd9Sstevel@tonic-gate 	sctp_pad_mp = allocb(SCTP_ALIGN, BPRI_MED);
14277c478bd9Sstevel@tonic-gate 	bzero(sctp_pad_mp->b_rptr, SCTP_ALIGN);
14287c478bd9Sstevel@tonic-gate 	ASSERT(sctp_pad_mp);
14297c478bd9Sstevel@tonic-gate 
14307c478bd9Sstevel@tonic-gate 	if (!sctp_nd_init()) {
14317c478bd9Sstevel@tonic-gate 		sctp_nd_free();
14327c478bd9Sstevel@tonic-gate 	}
14337c478bd9Sstevel@tonic-gate 
14347c478bd9Sstevel@tonic-gate 	/* Create sctp_t/conn_t cache */
14357c478bd9Sstevel@tonic-gate 	sctp_conn_cache_init();
14367c478bd9Sstevel@tonic-gate 
14377c478bd9Sstevel@tonic-gate 	/* Create the faddr cache */
14387c478bd9Sstevel@tonic-gate 	sctp_faddr_init();
14397c478bd9Sstevel@tonic-gate 
14407c478bd9Sstevel@tonic-gate 	/* Create the sets cache */
14417c478bd9Sstevel@tonic-gate 	sctp_sets_init();
14427c478bd9Sstevel@tonic-gate 
14437c478bd9Sstevel@tonic-gate 	/* Create the PR-SCTP sets cache */
14447c478bd9Sstevel@tonic-gate 	sctp_ftsn_sets_init();
14457c478bd9Sstevel@tonic-gate 
14467c478bd9Sstevel@tonic-gate 	/* Initialize the recvq taskq. */
14477c478bd9Sstevel@tonic-gate 	sctp_rq_tq_init();
14487c478bd9Sstevel@tonic-gate 
14497c478bd9Sstevel@tonic-gate 	/* saddr init */
14507c478bd9Sstevel@tonic-gate 	sctp_saddr_init();
14517c478bd9Sstevel@tonic-gate 
14527c478bd9Sstevel@tonic-gate 	/* Global SCTP PCB list. */
14537c478bd9Sstevel@tonic-gate 	list_create(&sctp_g_list, sizeof (sctp_t),
14547c478bd9Sstevel@tonic-gate 	    offsetof(sctp_t, sctp_list));
14557c478bd9Sstevel@tonic-gate 
14567c478bd9Sstevel@tonic-gate 	/* Initialize tables used for CRC calculation */
14577c478bd9Sstevel@tonic-gate 	sctp_crc32_init();
14587c478bd9Sstevel@tonic-gate 
14597c478bd9Sstevel@tonic-gate 	/* Initialize sctp kernel stats. */
14607c478bd9Sstevel@tonic-gate 	sctp_kstat_init();
14617c478bd9Sstevel@tonic-gate }
14627c478bd9Sstevel@tonic-gate 
14637c478bd9Sstevel@tonic-gate void
14647c478bd9Sstevel@tonic-gate sctp_ddi_destroy(void)
14657c478bd9Sstevel@tonic-gate {
14667c478bd9Sstevel@tonic-gate 	sctp_nd_free();
14677c478bd9Sstevel@tonic-gate 
14687c478bd9Sstevel@tonic-gate 	/* Destroy sctp_t/conn_t caches */
14697c478bd9Sstevel@tonic-gate 	sctp_conn_cache_fini();
14707c478bd9Sstevel@tonic-gate 
14717c478bd9Sstevel@tonic-gate 	/* Destroy the faddr cache */
14727c478bd9Sstevel@tonic-gate 	sctp_faddr_fini();
14737c478bd9Sstevel@tonic-gate 
14747c478bd9Sstevel@tonic-gate 	/* Destroy the sets cache */
14757c478bd9Sstevel@tonic-gate 	sctp_sets_fini();
14767c478bd9Sstevel@tonic-gate 
14777c478bd9Sstevel@tonic-gate 	/* Destroy the PR-SCTP sets cache */
14787c478bd9Sstevel@tonic-gate 	sctp_ftsn_sets_fini();
14797c478bd9Sstevel@tonic-gate 
14807c478bd9Sstevel@tonic-gate 	/* Destroy the recvq taskqs. */
14817c478bd9Sstevel@tonic-gate 	sctp_rq_tq_fini();
14827c478bd9Sstevel@tonic-gate 
14837c478bd9Sstevel@tonic-gate 	/* Destroy saddr  */
14847c478bd9Sstevel@tonic-gate 	sctp_saddr_fini();
14857c478bd9Sstevel@tonic-gate 
14867c478bd9Sstevel@tonic-gate 	/* Global SCTP PCB list. */
14877c478bd9Sstevel@tonic-gate 	list_destroy(&sctp_g_list);
14887c478bd9Sstevel@tonic-gate 
14897c478bd9Sstevel@tonic-gate 	/* Destroy SCTP hash arrays. */
14907c478bd9Sstevel@tonic-gate 	sctp_hash_destroy();
14917c478bd9Sstevel@tonic-gate 
14927c478bd9Sstevel@tonic-gate 	/* Destroy SCTP kenrel stats. */
14937c478bd9Sstevel@tonic-gate 	sctp_kstat_fini();
14947c478bd9Sstevel@tonic-gate 
14957c478bd9Sstevel@tonic-gate 	mutex_destroy(&sctp_g_lock);
14967c478bd9Sstevel@tonic-gate 	mutex_destroy(&sctp_epriv_port_lock);
14977c478bd9Sstevel@tonic-gate }
14987c478bd9Sstevel@tonic-gate 
14997c478bd9Sstevel@tonic-gate void
15007c478bd9Sstevel@tonic-gate sctp_display_all()
15017c478bd9Sstevel@tonic-gate {
15027c478bd9Sstevel@tonic-gate 	sctp_t *sctp_walker;
15037c478bd9Sstevel@tonic-gate 
15047c478bd9Sstevel@tonic-gate 	mutex_enter(&sctp_g_lock);
15057c478bd9Sstevel@tonic-gate 	for (sctp_walker = gsctp; sctp_walker != NULL;
15067c478bd9Sstevel@tonic-gate 		sctp_walker = (sctp_t *)list_next(&sctp_g_list, sctp_walker)) {
15077c478bd9Sstevel@tonic-gate 		(void) sctp_display(sctp_walker, NULL);
15087c478bd9Sstevel@tonic-gate 	}
15097c478bd9Sstevel@tonic-gate 	mutex_exit(&sctp_g_lock);
15107c478bd9Sstevel@tonic-gate }
15117c478bd9Sstevel@tonic-gate 
15127c478bd9Sstevel@tonic-gate static void
15137c478bd9Sstevel@tonic-gate sctp_rq_tq_init(void)
15147c478bd9Sstevel@tonic-gate {
15157c478bd9Sstevel@tonic-gate 	/*
15167c478bd9Sstevel@tonic-gate 	 * Initialize the recvq_tq_list and create the first recvq taskq.
15177c478bd9Sstevel@tonic-gate 	 * What to do if it fails?
15187c478bd9Sstevel@tonic-gate 	 */
15197c478bd9Sstevel@tonic-gate 	recvq_tq_list = kmem_zalloc(sctp_recvq_tq_list_max_sz *
15207c478bd9Sstevel@tonic-gate 	    sizeof (taskq_t *), KM_SLEEP);
15217c478bd9Sstevel@tonic-gate 	recvq_tq_list[0] = taskq_create("sctp_def_recvq_taskq",
15227c478bd9Sstevel@tonic-gate 	    MIN(sctp_recvq_tq_thr_max, MAX(sctp_recvq_tq_thr_min, ncpus)),
15237c478bd9Sstevel@tonic-gate 	    minclsyspri, sctp_recvq_tq_task_min, sctp_recvq_tq_task_max,
15247c478bd9Sstevel@tonic-gate 	    TASKQ_PREPOPULATE);
15257c478bd9Sstevel@tonic-gate 	mutex_init(&sctp_rq_tq_lock, NULL, MUTEX_DEFAULT, NULL);
15267c478bd9Sstevel@tonic-gate }
15277c478bd9Sstevel@tonic-gate 
15287c478bd9Sstevel@tonic-gate static void
15297c478bd9Sstevel@tonic-gate sctp_rq_tq_fini(void)
15307c478bd9Sstevel@tonic-gate {
15317c478bd9Sstevel@tonic-gate 	int i;
15327c478bd9Sstevel@tonic-gate 
15337c478bd9Sstevel@tonic-gate 	for (i = 0; i < recvq_tq_list_cur_sz; i++) {
15347c478bd9Sstevel@tonic-gate 		ASSERT(recvq_tq_list[i] != NULL);
15357c478bd9Sstevel@tonic-gate 		taskq_destroy(recvq_tq_list[i]);
15367c478bd9Sstevel@tonic-gate 	}
15377c478bd9Sstevel@tonic-gate 	kmem_free(recvq_tq_list, sctp_recvq_tq_list_max_sz *
15387c478bd9Sstevel@tonic-gate 	    sizeof (taskq_t *));
15397c478bd9Sstevel@tonic-gate }
15407c478bd9Sstevel@tonic-gate 
15417c478bd9Sstevel@tonic-gate /* Add another taskq for a new ill. */
15427c478bd9Sstevel@tonic-gate void
15437c478bd9Sstevel@tonic-gate sctp_inc_taskq(void)
15447c478bd9Sstevel@tonic-gate {
15457c478bd9Sstevel@tonic-gate 	taskq_t *tq;
15467c478bd9Sstevel@tonic-gate 	char tq_name[TASKQ_NAMELEN];
15477c478bd9Sstevel@tonic-gate 
15487c478bd9Sstevel@tonic-gate 	mutex_enter(&sctp_rq_tq_lock);
15497c478bd9Sstevel@tonic-gate 	if (recvq_tq_list_cur_sz + 1 > sctp_recvq_tq_list_max_sz) {
15507c478bd9Sstevel@tonic-gate 		mutex_exit(&sctp_rq_tq_lock);
15517c478bd9Sstevel@tonic-gate 		cmn_err(CE_NOTE, "Cannot create more SCTP recvq taskq");
15527c478bd9Sstevel@tonic-gate 		return;
15537c478bd9Sstevel@tonic-gate 	}
15547c478bd9Sstevel@tonic-gate 
15557c478bd9Sstevel@tonic-gate 	(void) snprintf(tq_name, sizeof (tq_name), "sctp_recvq_taskq_%u",
15567c478bd9Sstevel@tonic-gate 	    recvq_tq_list_cur_sz);
15577c478bd9Sstevel@tonic-gate 	tq = taskq_create(tq_name,
15587c478bd9Sstevel@tonic-gate 	    MIN(sctp_recvq_tq_thr_max, MAX(sctp_recvq_tq_thr_min, ncpus)),
15597c478bd9Sstevel@tonic-gate 	    minclsyspri, sctp_recvq_tq_task_min, sctp_recvq_tq_task_max,
15607c478bd9Sstevel@tonic-gate 	    TASKQ_PREPOPULATE);
15617c478bd9Sstevel@tonic-gate 	if (tq == NULL) {
15627c478bd9Sstevel@tonic-gate 		mutex_exit(&sctp_rq_tq_lock);
15637c478bd9Sstevel@tonic-gate 		cmn_err(CE_NOTE, "SCTP recvq taskq creation failed");
15647c478bd9Sstevel@tonic-gate 		return;
15657c478bd9Sstevel@tonic-gate 	}
15667c478bd9Sstevel@tonic-gate 	ASSERT(recvq_tq_list[recvq_tq_list_cur_sz] == NULL);
15677c478bd9Sstevel@tonic-gate 	recvq_tq_list[recvq_tq_list_cur_sz] = tq;
15687c478bd9Sstevel@tonic-gate 	atomic_add_32(&recvq_tq_list_cur_sz, 1);
15697c478bd9Sstevel@tonic-gate 	mutex_exit(&sctp_rq_tq_lock);
15707c478bd9Sstevel@tonic-gate }
15717c478bd9Sstevel@tonic-gate 
15727c478bd9Sstevel@tonic-gate #ifdef DEBUG
15737c478bd9Sstevel@tonic-gate uint32_t sendq_loop_cnt = 0;
15747c478bd9Sstevel@tonic-gate uint32_t sendq_collision = 0;
15757c478bd9Sstevel@tonic-gate uint32_t sendq_empty = 0;
15767c478bd9Sstevel@tonic-gate #endif
15777c478bd9Sstevel@tonic-gate 
15787c478bd9Sstevel@tonic-gate void
15797c478bd9Sstevel@tonic-gate sctp_add_sendq(sctp_t *sctp, mblk_t *mp)
15807c478bd9Sstevel@tonic-gate {
15817c478bd9Sstevel@tonic-gate 	mutex_enter(&sctp->sctp_sendq_lock);
15827c478bd9Sstevel@tonic-gate 	if (sctp->sctp_sendq == NULL) {
15837c478bd9Sstevel@tonic-gate 		sctp->sctp_sendq = mp;
15847c478bd9Sstevel@tonic-gate 		sctp->sctp_sendq_tail = mp;
15857c478bd9Sstevel@tonic-gate 	} else {
15867c478bd9Sstevel@tonic-gate 		sctp->sctp_sendq_tail->b_next = mp;
15877c478bd9Sstevel@tonic-gate 		sctp->sctp_sendq_tail = mp;
15887c478bd9Sstevel@tonic-gate 	}
15897c478bd9Sstevel@tonic-gate 	mutex_exit(&sctp->sctp_sendq_lock);
15907c478bd9Sstevel@tonic-gate }
15917c478bd9Sstevel@tonic-gate 
15927c478bd9Sstevel@tonic-gate void
15937c478bd9Sstevel@tonic-gate sctp_process_sendq(sctp_t *sctp)
15947c478bd9Sstevel@tonic-gate {
15957c478bd9Sstevel@tonic-gate 	mblk_t *mp;
15967c478bd9Sstevel@tonic-gate #ifdef DEBUG
15977c478bd9Sstevel@tonic-gate 	uint32_t loop_cnt = 0;
15987c478bd9Sstevel@tonic-gate #endif
15997c478bd9Sstevel@tonic-gate 
16007c478bd9Sstevel@tonic-gate 	mutex_enter(&sctp->sctp_sendq_lock);
16017c478bd9Sstevel@tonic-gate 	if (sctp->sctp_sendq == NULL || sctp->sctp_sendq_sending) {
16027c478bd9Sstevel@tonic-gate #ifdef DEBUG
16037c478bd9Sstevel@tonic-gate 		if (sctp->sctp_sendq == NULL)
16047c478bd9Sstevel@tonic-gate 			sendq_empty++;
16057c478bd9Sstevel@tonic-gate 		else
16067c478bd9Sstevel@tonic-gate 			sendq_collision++;
16077c478bd9Sstevel@tonic-gate #endif
16087c478bd9Sstevel@tonic-gate 		mutex_exit(&sctp->sctp_sendq_lock);
16097c478bd9Sstevel@tonic-gate 		return;
16107c478bd9Sstevel@tonic-gate 	}
16117c478bd9Sstevel@tonic-gate 	sctp->sctp_sendq_sending = B_TRUE;
16127c478bd9Sstevel@tonic-gate 
16137c478bd9Sstevel@tonic-gate 	/*
16147c478bd9Sstevel@tonic-gate 	 * Note that while we are in this loop, other thread can put
16157c478bd9Sstevel@tonic-gate 	 * new packets in the receive queue.  We may be looping for
16167c478bd9Sstevel@tonic-gate 	 * quite a while.  This is OK even for an interrupt thread.
16177c478bd9Sstevel@tonic-gate 	 * The reason is that SCTP should only able to send a limited
16187c478bd9Sstevel@tonic-gate 	 * number of packets out in a burst.  So the number of times
16197c478bd9Sstevel@tonic-gate 	 * we go through this loop should not be many.
16207c478bd9Sstevel@tonic-gate 	 */
16217c478bd9Sstevel@tonic-gate 	while ((mp = sctp->sctp_sendq) != NULL) {
16227c478bd9Sstevel@tonic-gate 		sctp->sctp_sendq = mp->b_next;
16237c478bd9Sstevel@tonic-gate 		ASSERT(sctp->sctp_connp->conn_ref > 0);
16247c478bd9Sstevel@tonic-gate 		mutex_exit(&sctp->sctp_sendq_lock);
16257c478bd9Sstevel@tonic-gate 		mp->b_next = NULL;
16267c478bd9Sstevel@tonic-gate 		CONN_INC_REF(sctp->sctp_connp);
16277c478bd9Sstevel@tonic-gate 		mp->b_flag |= MSGHASREF;
16287c478bd9Sstevel@tonic-gate 		/* If we don't have sctp_current, default to IPv4 */
16297c478bd9Sstevel@tonic-gate 		IP_PUT(mp, sctp->sctp_connp, sctp->sctp_current == NULL ?
16307c478bd9Sstevel@tonic-gate 		    B_TRUE : sctp->sctp_current->isv4);
16317c478bd9Sstevel@tonic-gate 		BUMP_LOCAL(sctp->sctp_opkts);
16327c478bd9Sstevel@tonic-gate #ifdef DEBUG
16337c478bd9Sstevel@tonic-gate 		loop_cnt++;
16347c478bd9Sstevel@tonic-gate #endif
16357c478bd9Sstevel@tonic-gate 		mutex_enter(&sctp->sctp_sendq_lock);
16367c478bd9Sstevel@tonic-gate 	}
16377c478bd9Sstevel@tonic-gate 
16387c478bd9Sstevel@tonic-gate 	sctp->sctp_sendq_tail = NULL;
16397c478bd9Sstevel@tonic-gate 	sctp->sctp_sendq_sending = B_FALSE;
16407c478bd9Sstevel@tonic-gate #ifdef DEBUG
16417c478bd9Sstevel@tonic-gate 	if (loop_cnt > sendq_loop_cnt)
16427c478bd9Sstevel@tonic-gate 		sendq_loop_cnt = loop_cnt;
16437c478bd9Sstevel@tonic-gate #endif
16447c478bd9Sstevel@tonic-gate 	mutex_exit(&sctp->sctp_sendq_lock);
16457c478bd9Sstevel@tonic-gate }
16467c478bd9Sstevel@tonic-gate 
16477c478bd9Sstevel@tonic-gate #ifdef DEBUG
16487c478bd9Sstevel@tonic-gate uint32_t recvq_loop_cnt = 0;
16497c478bd9Sstevel@tonic-gate uint32_t recvq_call = 0;
16507c478bd9Sstevel@tonic-gate #endif
16517c478bd9Sstevel@tonic-gate 
16527c478bd9Sstevel@tonic-gate /*
16537c478bd9Sstevel@tonic-gate  * Find the next recvq_tq to use.  This routine will go thru all the
16547c478bd9Sstevel@tonic-gate  * taskqs until it can dispatch a job for the sctp.  If this fails,
16557c478bd9Sstevel@tonic-gate  * it will create a new taskq and try it.
16567c478bd9Sstevel@tonic-gate  */
16577c478bd9Sstevel@tonic-gate static boolean_t
16587c478bd9Sstevel@tonic-gate sctp_find_next_tq(sctp_t *sctp)
16597c478bd9Sstevel@tonic-gate {
16607c478bd9Sstevel@tonic-gate 	int next_tq, try;
16617c478bd9Sstevel@tonic-gate 	taskq_t *tq;
16627c478bd9Sstevel@tonic-gate 
16637c478bd9Sstevel@tonic-gate 	/*
16647c478bd9Sstevel@tonic-gate 	 * Note that since we don't hold a lock on sctp_rq_tq_lock for
16657c478bd9Sstevel@tonic-gate 	 * performance reason, recvq_ta_list_cur_sz can be changed during
16667c478bd9Sstevel@tonic-gate 	 * this loop.  The problem this will create is that the loop may
16677c478bd9Sstevel@tonic-gate 	 * not have tried all the recvq_tq.  This should be OK.
16687c478bd9Sstevel@tonic-gate 	 */
16697c478bd9Sstevel@tonic-gate 	next_tq = atomic_add_32_nv(&recvq_tq_list_cur, 1) %
16707c478bd9Sstevel@tonic-gate 	    recvq_tq_list_cur_sz;
16717c478bd9Sstevel@tonic-gate 	for (try = 0; try < recvq_tq_list_cur_sz;
16727c478bd9Sstevel@tonic-gate 	    try++, next_tq = (next_tq + 1) % recvq_tq_list_cur_sz) {
16737c478bd9Sstevel@tonic-gate 		tq = recvq_tq_list[next_tq];
16747c478bd9Sstevel@tonic-gate 		if (taskq_dispatch(tq, sctp_process_recvq, sctp,
16757c478bd9Sstevel@tonic-gate 		    TQ_NOSLEEP) != NULL) {
16767c478bd9Sstevel@tonic-gate 			sctp->sctp_recvq_tq = tq;
16777c478bd9Sstevel@tonic-gate 			return (B_TRUE);
16787c478bd9Sstevel@tonic-gate 		}
16797c478bd9Sstevel@tonic-gate 	}
16807c478bd9Sstevel@tonic-gate 
16817c478bd9Sstevel@tonic-gate 	/*
16827c478bd9Sstevel@tonic-gate 	 * Create one more taskq and try it.  Note that sctp_inc_taskq()
16837c478bd9Sstevel@tonic-gate 	 * may not have created another taskq if the number of recvq
16847c478bd9Sstevel@tonic-gate 	 * taskqs is at the maximum.  We are probably in a pretty bad
16857c478bd9Sstevel@tonic-gate 	 * shape if this actually happens...
16867c478bd9Sstevel@tonic-gate 	 */
16877c478bd9Sstevel@tonic-gate 	sctp_inc_taskq();
16887c478bd9Sstevel@tonic-gate 	tq = recvq_tq_list[recvq_tq_list_cur_sz - 1];
16897c478bd9Sstevel@tonic-gate 	if (taskq_dispatch(tq, sctp_process_recvq, sctp, TQ_NOSLEEP) != NULL) {
16907c478bd9Sstevel@tonic-gate 		sctp->sctp_recvq_tq = tq;
16917c478bd9Sstevel@tonic-gate 		return (B_TRUE);
16927c478bd9Sstevel@tonic-gate 	}
16937c478bd9Sstevel@tonic-gate 	return (B_FALSE);
16947c478bd9Sstevel@tonic-gate }
16957c478bd9Sstevel@tonic-gate 
16967c478bd9Sstevel@tonic-gate /*
16977c478bd9Sstevel@tonic-gate  * To add a message to the recvq.  Note that the sctp_timer_fire()
16987c478bd9Sstevel@tonic-gate  * routine also uses this function to add the timer message to the
16997c478bd9Sstevel@tonic-gate  * receive queue for later processing.  And it should be the only
17007c478bd9Sstevel@tonic-gate  * caller of sctp_add_recvq() which sets the try_harder argument
17017c478bd9Sstevel@tonic-gate  * to B_TRUE.
17027c478bd9Sstevel@tonic-gate  *
17037c478bd9Sstevel@tonic-gate  * If the try_harder argument is B_TRUE, this routine sctp_find_next_tq()
17047c478bd9Sstevel@tonic-gate  * will try very hard to dispatch the task.  Refer to the comment
17057c478bd9Sstevel@tonic-gate  * for that routine on how it does that.
17067c478bd9Sstevel@tonic-gate  */
17077c478bd9Sstevel@tonic-gate boolean_t
17087c478bd9Sstevel@tonic-gate sctp_add_recvq(sctp_t *sctp, mblk_t *mp, boolean_t caller_hold_lock)
17097c478bd9Sstevel@tonic-gate {
17107c478bd9Sstevel@tonic-gate 	if (!caller_hold_lock)
17117c478bd9Sstevel@tonic-gate 		mutex_enter(&sctp->sctp_recvq_lock);
17127c478bd9Sstevel@tonic-gate 
17137c478bd9Sstevel@tonic-gate 	/* If the taskq dispatch has not been scheduled, do it now. */
17147c478bd9Sstevel@tonic-gate 	if (sctp->sctp_recvq_tq == NULL) {
17157c478bd9Sstevel@tonic-gate 		ASSERT(sctp->sctp_recvq == NULL);
17167c478bd9Sstevel@tonic-gate 		if (!sctp_find_next_tq(sctp)) {
17177c478bd9Sstevel@tonic-gate 			if (!caller_hold_lock)
17187c478bd9Sstevel@tonic-gate 				mutex_exit(&sctp->sctp_recvq_lock);
17197c478bd9Sstevel@tonic-gate 			return (B_FALSE);
17207c478bd9Sstevel@tonic-gate 		}
17217c478bd9Sstevel@tonic-gate 		/* Make sure the sctp_t will not go away. */
17227c478bd9Sstevel@tonic-gate 		SCTP_REFHOLD(sctp);
17237c478bd9Sstevel@tonic-gate 	}
17247c478bd9Sstevel@tonic-gate 
17257c478bd9Sstevel@tonic-gate 	if (sctp->sctp_recvq == NULL) {
17267c478bd9Sstevel@tonic-gate 		sctp->sctp_recvq = mp;
17277c478bd9Sstevel@tonic-gate 		sctp->sctp_recvq_tail = mp;
17287c478bd9Sstevel@tonic-gate 	} else {
17297c478bd9Sstevel@tonic-gate 		sctp->sctp_recvq_tail->b_next = mp;
17307c478bd9Sstevel@tonic-gate 		sctp->sctp_recvq_tail = mp;
17317c478bd9Sstevel@tonic-gate 	}
17327c478bd9Sstevel@tonic-gate 
17337c478bd9Sstevel@tonic-gate 	if (!caller_hold_lock)
17347c478bd9Sstevel@tonic-gate 		mutex_exit(&sctp->sctp_recvq_lock);
17357c478bd9Sstevel@tonic-gate 	return (B_TRUE);
17367c478bd9Sstevel@tonic-gate }
17377c478bd9Sstevel@tonic-gate 
17387c478bd9Sstevel@tonic-gate static void
17397c478bd9Sstevel@tonic-gate sctp_process_recvq(void *arg)
17407c478bd9Sstevel@tonic-gate {
17417c478bd9Sstevel@tonic-gate 	sctp_t		*sctp = (sctp_t *)arg;
17427c478bd9Sstevel@tonic-gate 	mblk_t		*mp;
17437c478bd9Sstevel@tonic-gate 	mblk_t		*ipsec_mp;
17447c478bd9Sstevel@tonic-gate #ifdef DEBUG
17457c478bd9Sstevel@tonic-gate 	uint32_t	loop_cnt = 0;
17467c478bd9Sstevel@tonic-gate #endif
17477c478bd9Sstevel@tonic-gate 
17487c478bd9Sstevel@tonic-gate #ifdef	_BIG_ENDIAN
17497c478bd9Sstevel@tonic-gate #define	IPVER(ip6h)	((((uint32_t *)ip6h)[0] >> 28) & 0x7)
17507c478bd9Sstevel@tonic-gate #else
17517c478bd9Sstevel@tonic-gate #define	IPVER(ip6h)	((((uint32_t *)ip6h)[0] >> 4) & 0x7)
17527c478bd9Sstevel@tonic-gate #endif
17537c478bd9Sstevel@tonic-gate 
17547c478bd9Sstevel@tonic-gate 	RUN_SCTP(sctp);
17557c478bd9Sstevel@tonic-gate 	mutex_enter(&sctp->sctp_recvq_lock);
17567c478bd9Sstevel@tonic-gate 
17577c478bd9Sstevel@tonic-gate #ifdef DEBUG
17587c478bd9Sstevel@tonic-gate 	recvq_call++;
17597c478bd9Sstevel@tonic-gate #endif
17607c478bd9Sstevel@tonic-gate 	/*
17617c478bd9Sstevel@tonic-gate 	 * Note that while we are in this loop, other thread can put
17627c478bd9Sstevel@tonic-gate 	 * new packets in the receive queue.  We may be looping for
17637c478bd9Sstevel@tonic-gate 	 * quite a while.
17647c478bd9Sstevel@tonic-gate 	 */
17657c478bd9Sstevel@tonic-gate 	while ((mp = sctp->sctp_recvq) != NULL) {
17667c478bd9Sstevel@tonic-gate 		sctp->sctp_recvq = mp->b_next;
17677c478bd9Sstevel@tonic-gate 		mutex_exit(&sctp->sctp_recvq_lock);
17687c478bd9Sstevel@tonic-gate 		mp->b_next = NULL;
17697c478bd9Sstevel@tonic-gate #ifdef DEBUG
17707c478bd9Sstevel@tonic-gate 		loop_cnt++;
17717c478bd9Sstevel@tonic-gate #endif
17727c478bd9Sstevel@tonic-gate 		ipsec_mp = mp->b_prev;
17737c478bd9Sstevel@tonic-gate 		mp->b_prev = NULL;
17747c478bd9Sstevel@tonic-gate 		sctp_input_data(sctp, mp, ipsec_mp);
17757c478bd9Sstevel@tonic-gate 
17767c478bd9Sstevel@tonic-gate 		mutex_enter(&sctp->sctp_recvq_lock);
17777c478bd9Sstevel@tonic-gate 	}
17787c478bd9Sstevel@tonic-gate 
17797c478bd9Sstevel@tonic-gate 	sctp->sctp_recvq_tail = NULL;
17807c478bd9Sstevel@tonic-gate 	sctp->sctp_recvq_tq = NULL;
17817c478bd9Sstevel@tonic-gate 
17827c478bd9Sstevel@tonic-gate 	mutex_exit(&sctp->sctp_recvq_lock);
17837c478bd9Sstevel@tonic-gate 
17847c478bd9Sstevel@tonic-gate 	WAKE_SCTP(sctp);
17857c478bd9Sstevel@tonic-gate 
17867c478bd9Sstevel@tonic-gate 	/* We may have sent something when processing the receive queue. */
17877c478bd9Sstevel@tonic-gate 	sctp_process_sendq(sctp);
17887c478bd9Sstevel@tonic-gate #ifdef DEBUG
17897c478bd9Sstevel@tonic-gate 	if (loop_cnt > recvq_loop_cnt)
17907c478bd9Sstevel@tonic-gate 		recvq_loop_cnt = loop_cnt;
17917c478bd9Sstevel@tonic-gate #endif
17927c478bd9Sstevel@tonic-gate 	/* Now it can go away. */
17937c478bd9Sstevel@tonic-gate 	SCTP_REFRELE(sctp);
17947c478bd9Sstevel@tonic-gate }
17957c478bd9Sstevel@tonic-gate 
17967c478bd9Sstevel@tonic-gate /* ARGSUSED */
17977c478bd9Sstevel@tonic-gate static int
17987c478bd9Sstevel@tonic-gate sctp_conn_cache_constructor(void *buf, void *cdrarg, int kmflags)
17997c478bd9Sstevel@tonic-gate {
18007c478bd9Sstevel@tonic-gate 	conn_t	*sctp_connp = (conn_t *)buf;
18017c478bd9Sstevel@tonic-gate 	sctp_t	*sctp = (sctp_t *)&sctp_connp[1];
18027c478bd9Sstevel@tonic-gate 
18037c478bd9Sstevel@tonic-gate 	bzero(buf, (char *)&sctp[1] - (char *)buf);
18047c478bd9Sstevel@tonic-gate 
18057c478bd9Sstevel@tonic-gate 	ASSERT(sctp_g_q != NULL);
18067c478bd9Sstevel@tonic-gate 	sctp->sctp_connp = sctp_connp;
18077c478bd9Sstevel@tonic-gate 	mutex_init(&sctp->sctp_reflock, NULL, MUTEX_DEFAULT, NULL);
18087c478bd9Sstevel@tonic-gate 	mutex_init(&sctp->sctp_lock, NULL, MUTEX_DEFAULT, NULL);
18097c478bd9Sstevel@tonic-gate 	mutex_init(&sctp->sctp_recvq_lock, NULL, MUTEX_DEFAULT, NULL);
18107c478bd9Sstevel@tonic-gate 	cv_init(&sctp->sctp_cv, NULL, CV_DEFAULT, NULL);
18117c478bd9Sstevel@tonic-gate 	mutex_init(&sctp->sctp_sendq_lock, NULL, MUTEX_DEFAULT, NULL);
18127c478bd9Sstevel@tonic-gate 
18137c478bd9Sstevel@tonic-gate 	sctp_connp->conn_rq = sctp_connp->conn_wq = NULL;
18147c478bd9Sstevel@tonic-gate 	sctp_connp->conn_multicast_loop = IP_DEFAULT_MULTICAST_LOOP;
18157c478bd9Sstevel@tonic-gate 	sctp_connp->conn_ulp = IPPROTO_SCTP;
18167c478bd9Sstevel@tonic-gate 	mutex_init(&sctp_connp->conn_lock, NULL, MUTEX_DEFAULT, NULL);
18177c478bd9Sstevel@tonic-gate 	cv_init(&sctp_connp->conn_cv, NULL, CV_DEFAULT, NULL);
18187c478bd9Sstevel@tonic-gate 
18197c478bd9Sstevel@tonic-gate 	return (0);
18207c478bd9Sstevel@tonic-gate }
18217c478bd9Sstevel@tonic-gate 
18227c478bd9Sstevel@tonic-gate /* ARGSUSED */
18237c478bd9Sstevel@tonic-gate static void
18247c478bd9Sstevel@tonic-gate sctp_conn_cache_destructor(void *buf, void *cdrarg)
18257c478bd9Sstevel@tonic-gate {
18267c478bd9Sstevel@tonic-gate 	conn_t	*sctp_connp = (conn_t *)buf;
18277c478bd9Sstevel@tonic-gate 	sctp_t	*sctp = (sctp_t *)&sctp_connp[1];
18287c478bd9Sstevel@tonic-gate 
18297c478bd9Sstevel@tonic-gate 	ASSERT(!MUTEX_HELD(&sctp->sctp_lock));
18307c478bd9Sstevel@tonic-gate 	ASSERT(!MUTEX_HELD(&sctp->sctp_reflock));
18317c478bd9Sstevel@tonic-gate 	ASSERT(!MUTEX_HELD(&sctp->sctp_recvq_lock));
18327c478bd9Sstevel@tonic-gate 	ASSERT(!MUTEX_HELD(&sctp->sctp_sendq_lock));
18337c478bd9Sstevel@tonic-gate 	ASSERT(!MUTEX_HELD(&sctp->sctp_connp->conn_lock));
18347c478bd9Sstevel@tonic-gate 
18357c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_conn_hash_next == NULL);
18367c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_conn_hash_prev == NULL);
18377c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_listen_hash_next == NULL);
18387c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_listen_hash_prev == NULL);
18397c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_listen_tfp == NULL);
18407c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_conn_tfp == NULL);
18417c478bd9Sstevel@tonic-gate 
18427c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_faddrs == NULL);
18437c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_nsaddrs == 0);
18447c478bd9Sstevel@tonic-gate 
18457c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_ulpd == NULL);
18467c478bd9Sstevel@tonic-gate 
18477c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_lastfaddr == NULL);
18487c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_primary == NULL);
18497c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_current == NULL);
18507c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_lastdata == NULL);
18517c478bd9Sstevel@tonic-gate 
18527c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_xmit_head == NULL);
18537c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_xmit_tail == NULL);
18547c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_xmit_unsent == NULL);
18557c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_xmit_unsent_tail == NULL);
18567c478bd9Sstevel@tonic-gate 
18577c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_ostrcntrs == NULL);
18587c478bd9Sstevel@tonic-gate 
18597c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_sack_info == NULL);
18607c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_ack_mp == NULL);
18617c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_instr == NULL);
18627c478bd9Sstevel@tonic-gate 
18637c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_iphc == NULL);
18647c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_iphc6 == NULL);
18657c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_ipha == NULL);
18667c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_ip6h == NULL);
18677c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_sctph == NULL);
18687c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_sctph6 == NULL);
18697c478bd9Sstevel@tonic-gate 
18707c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_cookie_mp == NULL);
18717c478bd9Sstevel@tonic-gate 
18727c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_refcnt == 0);
18737c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_timer_mp == NULL);
18747c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_connp->conn_ref == 0);
18757c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_heartbeat_mp == NULL);
18767c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_ptpbhn == NULL && sctp->sctp_bind_hash == NULL);
18777c478bd9Sstevel@tonic-gate 
18787c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_shutdown_faddr == NULL);
18797c478bd9Sstevel@tonic-gate 
18807c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_cxmit_list == NULL);
18817c478bd9Sstevel@tonic-gate 
18827c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_recvq == NULL);
18837c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_recvq_tail == NULL);
18847c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_recvq_tq == NULL);
18857c478bd9Sstevel@tonic-gate 
18867c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_sendq == NULL);
18877c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_sendq_tail == NULL);
18887c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_sendq_sending == B_FALSE);
18897c478bd9Sstevel@tonic-gate 
18907c478bd9Sstevel@tonic-gate 	mutex_destroy(&sctp->sctp_reflock);
18917c478bd9Sstevel@tonic-gate 	mutex_destroy(&sctp->sctp_lock);
18927c478bd9Sstevel@tonic-gate 	mutex_destroy(&sctp->sctp_recvq_lock);
18937c478bd9Sstevel@tonic-gate 	cv_destroy(&sctp->sctp_cv);
18947c478bd9Sstevel@tonic-gate 	mutex_destroy(&sctp->sctp_sendq_lock);
18957c478bd9Sstevel@tonic-gate 
18967c478bd9Sstevel@tonic-gate 	mutex_destroy(&sctp_connp->conn_lock);
18977c478bd9Sstevel@tonic-gate 	cv_destroy(&sctp_connp->conn_cv);
18987c478bd9Sstevel@tonic-gate }
18997c478bd9Sstevel@tonic-gate 
19007c478bd9Sstevel@tonic-gate static void
19017c478bd9Sstevel@tonic-gate sctp_conn_cache_init()
19027c478bd9Sstevel@tonic-gate {
19037c478bd9Sstevel@tonic-gate 	sctp_conn_cache = kmem_cache_create("sctp_conn_cache",
19047c478bd9Sstevel@tonic-gate 	    sizeof (sctp_t) + sizeof (conn_t), 0, sctp_conn_cache_constructor,
19057c478bd9Sstevel@tonic-gate 	    sctp_conn_cache_destructor, NULL, NULL, NULL, 0);
19067c478bd9Sstevel@tonic-gate }
19077c478bd9Sstevel@tonic-gate 
19087c478bd9Sstevel@tonic-gate static void
19097c478bd9Sstevel@tonic-gate sctp_conn_cache_fini()
19107c478bd9Sstevel@tonic-gate {
19117c478bd9Sstevel@tonic-gate 	kmem_cache_destroy(sctp_conn_cache);
19127c478bd9Sstevel@tonic-gate }
1913