xref: /onnv-gate/usr/src/uts/common/inet/sctp/sctp.c (revision 10828)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51213Skcpoon  * Common Development and Distribution License (the "License").
61213Skcpoon  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
211213Skcpoon 
220Sstevel@tonic-gate /*
238778SErik.Nordmark@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #include <sys/types.h>
280Sstevel@tonic-gate #include <sys/stream.h>
290Sstevel@tonic-gate #include <sys/strsubr.h>
300Sstevel@tonic-gate #include <sys/stropts.h>
310Sstevel@tonic-gate #include <sys/strsun.h>
320Sstevel@tonic-gate #define	_SUN_TPI_VERSION 2
330Sstevel@tonic-gate #include <sys/tihdr.h>
340Sstevel@tonic-gate #include <sys/ddi.h>
350Sstevel@tonic-gate #include <sys/sunddi.h>
360Sstevel@tonic-gate #include <sys/xti_inet.h>
370Sstevel@tonic-gate #include <sys/cmn_err.h>
380Sstevel@tonic-gate #include <sys/debug.h>
390Sstevel@tonic-gate #include <sys/vtrace.h>
400Sstevel@tonic-gate #include <sys/kmem.h>
410Sstevel@tonic-gate #include <sys/cpuvar.h>
420Sstevel@tonic-gate #include <sys/random.h>
431676Sjpk #include <sys/priv.h>
443448Sdh155122 #include <sys/sunldi.h>
450Sstevel@tonic-gate 
460Sstevel@tonic-gate #include <sys/errno.h>
470Sstevel@tonic-gate #include <sys/signal.h>
480Sstevel@tonic-gate #include <sys/socket.h>
490Sstevel@tonic-gate #include <sys/isa_defs.h>
500Sstevel@tonic-gate #include <netinet/in.h>
510Sstevel@tonic-gate #include <netinet/tcp.h>
520Sstevel@tonic-gate #include <netinet/ip6.h>
530Sstevel@tonic-gate #include <netinet/icmp6.h>
540Sstevel@tonic-gate #include <netinet/sctp.h>
550Sstevel@tonic-gate #include <net/if.h>
560Sstevel@tonic-gate 
570Sstevel@tonic-gate #include <inet/common.h>
580Sstevel@tonic-gate #include <inet/ip.h>
590Sstevel@tonic-gate #include <inet/ip6.h>
600Sstevel@tonic-gate #include <inet/mi.h>
610Sstevel@tonic-gate #include <inet/mib2.h>
623448Sdh155122 #include <inet/kstatcom.h>
630Sstevel@tonic-gate #include <inet/nd.h>
640Sstevel@tonic-gate #include <inet/optcom.h>
650Sstevel@tonic-gate #include <inet/ipclassifier.h>
660Sstevel@tonic-gate #include <inet/ipsec_impl.h>
670Sstevel@tonic-gate #include <inet/sctp_ip.h>
680Sstevel@tonic-gate #include <inet/sctp_crc32.h>
690Sstevel@tonic-gate 
700Sstevel@tonic-gate #include "sctp_impl.h"
710Sstevel@tonic-gate #include "sctp_addr.h"
72852Svi117747 #include "sctp_asconf.h"
730Sstevel@tonic-gate 
740Sstevel@tonic-gate int sctpdebug;
750Sstevel@tonic-gate sin6_t	sctp_sin6_null;	/* Zero address for quick clears */
760Sstevel@tonic-gate 
773448Sdh155122 /*
783448Sdh155122  * Have to ensure that sctp_g_q_close is not done by an
793448Sdh155122  * interrupt thread.
803448Sdh155122  */
813448Sdh155122 static taskq_t *sctp_taskq;
820Sstevel@tonic-gate 
830Sstevel@tonic-gate static void	sctp_closei_local(sctp_t *sctp);
840Sstevel@tonic-gate static int	sctp_init_values(sctp_t *, sctp_t *, int);
850Sstevel@tonic-gate static void	sctp_icmp_error_ipv6(sctp_t *sctp, mblk_t *mp);
860Sstevel@tonic-gate static void	sctp_process_recvq(void *);
873448Sdh155122 static void	sctp_rq_tq_init(sctp_stack_t *);
883448Sdh155122 static void	sctp_rq_tq_fini(sctp_stack_t *);
890Sstevel@tonic-gate static void	sctp_conn_cache_init();
900Sstevel@tonic-gate static void	sctp_conn_cache_fini();
910Sstevel@tonic-gate static int	sctp_conn_cache_constructor();
920Sstevel@tonic-gate static void	sctp_conn_cache_destructor();
934691Skcpoon static void	sctp_conn_clear(conn_t *);
943448Sdh155122 void		sctp_g_q_setup(sctp_stack_t *);
953448Sdh155122 void		sctp_g_q_create(sctp_stack_t *);
963448Sdh155122 void		sctp_g_q_destroy(sctp_stack_t *);
973448Sdh155122 
983448Sdh155122 static void	*sctp_stack_init(netstackid_t stackid, netstack_t *ns);
993448Sdh155122 static void	sctp_stack_shutdown(netstackid_t stackid, void *arg);
1003448Sdh155122 static void	sctp_stack_fini(netstackid_t stackid, void *arg);
1010Sstevel@tonic-gate 
1020Sstevel@tonic-gate /*
1030Sstevel@tonic-gate  * SCTP receive queue taskq
1040Sstevel@tonic-gate  *
1050Sstevel@tonic-gate  * At SCTP initialization time, a default taskq is created for
1060Sstevel@tonic-gate  * servicing packets received when the interrupt thread cannot
1070Sstevel@tonic-gate  * get a hold on the sctp_t.  The number of taskq can be increased in
1080Sstevel@tonic-gate  * sctp_find_next_tq() when an existing taskq cannot be dispatched.
1090Sstevel@tonic-gate  * The taskqs are never removed.  But the max number of taskq which
1100Sstevel@tonic-gate  * can be created is controlled by sctp_recvq_tq_list_max_sz.  Note
1110Sstevel@tonic-gate  * that SCTP recvq taskq is not tied to any specific CPU or ill.
1120Sstevel@tonic-gate  *
1130Sstevel@tonic-gate  * Those taskqs are stored in an array recvq_tq_list.  And they are
1140Sstevel@tonic-gate  * used in a round robin fashion.  The current taskq being used is
1150Sstevel@tonic-gate  * determined by recvq_tq_list_cur.
1160Sstevel@tonic-gate  */
1170Sstevel@tonic-gate 
1183448Sdh155122 /* /etc/system variables */
1190Sstevel@tonic-gate /* The minimum number of threads for each taskq. */
1200Sstevel@tonic-gate int		sctp_recvq_tq_thr_min = 4;
1210Sstevel@tonic-gate /* The maximum number of threads for each taskq. */
1220Sstevel@tonic-gate int		sctp_recvq_tq_thr_max = 16;
1230Sstevel@tonic-gate /* The minimum number of tasks for each taskq. */
1240Sstevel@tonic-gate int		sctp_recvq_tq_task_min = 5;
1250Sstevel@tonic-gate /* The maxiimum number of tasks for each taskq. */
1260Sstevel@tonic-gate int		sctp_recvq_tq_task_max = 50;
1270Sstevel@tonic-gate 
1280Sstevel@tonic-gate /*  sctp_t/conn_t kmem cache */
1290Sstevel@tonic-gate struct kmem_cache	*sctp_conn_cache;
1300Sstevel@tonic-gate 
1310Sstevel@tonic-gate #define	SCTP_CONDEMNED(sctp)				\
1320Sstevel@tonic-gate 	mutex_enter(&(sctp)->sctp_reflock);		\
1330Sstevel@tonic-gate 	((sctp)->sctp_condemned = B_TRUE);		\
1341213Skcpoon 	mutex_exit(&(sctp)->sctp_reflock);
1350Sstevel@tonic-gate 
1360Sstevel@tonic-gate /* Link/unlink a sctp_t to/from the global list. */
1373448Sdh155122 #define	SCTP_LINK(sctp, sctps)				\
1383448Sdh155122 	mutex_enter(&(sctps)->sctps_g_lock);		\
1393448Sdh155122 	list_insert_tail(&sctps->sctps_g_list, (sctp));	\
1403448Sdh155122 	mutex_exit(&(sctps)->sctps_g_lock);
1410Sstevel@tonic-gate 
1423448Sdh155122 #define	SCTP_UNLINK(sctp, sctps)				\
1433448Sdh155122 	mutex_enter(&(sctps)->sctps_g_lock);		\
1440Sstevel@tonic-gate 	ASSERT((sctp)->sctp_condemned);			\
1453448Sdh155122 	list_remove(&(sctps)->sctps_g_list, (sctp));	\
1463448Sdh155122 	mutex_exit(&(sctps)->sctps_g_lock);
1470Sstevel@tonic-gate 
1480Sstevel@tonic-gate /*
149852Svi117747  * Hooks for Sun Cluster. On non-clustered nodes these will remain NULL.
150852Svi117747  * PSARC/2005/602.
151852Svi117747  */
152852Svi117747 void (*cl_sctp_listen)(sa_family_t, uchar_t *, uint_t, in_port_t) = NULL;
153852Svi117747 void (*cl_sctp_unlisten)(sa_family_t, uchar_t *, uint_t, in_port_t) = NULL;
154852Svi117747 void (*cl_sctp_connect)(sa_family_t, uchar_t *, uint_t, in_port_t,
155852Svi117747     uchar_t *, uint_t, in_port_t, boolean_t, cl_sctp_handle_t) = NULL;
156852Svi117747 void (*cl_sctp_disconnect)(sa_family_t, cl_sctp_handle_t) = NULL;
157852Svi117747 void (*cl_sctp_assoc_change)(sa_family_t, uchar_t *, size_t, uint_t,
158852Svi117747     uchar_t *, size_t, uint_t, int, cl_sctp_handle_t) = NULL;
159852Svi117747 void (*cl_sctp_check_addrs)(sa_family_t, in_port_t, uchar_t **, size_t,
160852Svi117747     uint_t *, boolean_t) = NULL;
161852Svi117747 /*
1620Sstevel@tonic-gate  * Return the version number of the SCTP kernel interface.
1630Sstevel@tonic-gate  */
1640Sstevel@tonic-gate int
1650Sstevel@tonic-gate sctp_itf_ver(int cl_ver)
1660Sstevel@tonic-gate {
1670Sstevel@tonic-gate 	if (cl_ver != SCTP_ITF_VER)
1680Sstevel@tonic-gate 		return (-1);
1690Sstevel@tonic-gate 	return (SCTP_ITF_VER);
1700Sstevel@tonic-gate }
1710Sstevel@tonic-gate 
1720Sstevel@tonic-gate /*
1730Sstevel@tonic-gate  * Called when we need a new sctp instantiation but don't really have a
1740Sstevel@tonic-gate  * new q to hang it off of. Copy the priv flag from the passed in structure.
1750Sstevel@tonic-gate  */
1760Sstevel@tonic-gate sctp_t *
1770Sstevel@tonic-gate sctp_create_eager(sctp_t *psctp)
1780Sstevel@tonic-gate {
1790Sstevel@tonic-gate 	sctp_t	*sctp;
1800Sstevel@tonic-gate 	mblk_t	*ack_mp, *hb_mp;
1810Sstevel@tonic-gate 	conn_t	*connp, *pconnp;
1821676Sjpk 	cred_t *credp;
1833448Sdh155122 	sctp_stack_t	*sctps = psctp->sctp_sctps;
1840Sstevel@tonic-gate 
1853448Sdh155122 	if ((connp = ipcl_conn_create(IPCL_SCTPCONN, KM_NOSLEEP,
1864505Skcpoon 	    sctps->sctps_netstack)) == NULL) {
1870Sstevel@tonic-gate 		return (NULL);
1883448Sdh155122 	}
1891676Sjpk 
1901676Sjpk 	connp->conn_ulp_labeled = is_system_labeled();
1911676Sjpk 
1920Sstevel@tonic-gate 	sctp = CONN2SCTP(connp);
1933448Sdh155122 	sctp->sctp_sctps = sctps;
1940Sstevel@tonic-gate 
1954691Skcpoon 	if ((ack_mp = sctp_timer_alloc(sctp, sctp_ack_timer,
1964691Skcpoon 	    KM_NOSLEEP)) == NULL ||
1974691Skcpoon 	    (hb_mp = sctp_timer_alloc(sctp, sctp_heartbeat_timer,
1984691Skcpoon 	    KM_NOSLEEP)) == NULL) {
1990Sstevel@tonic-gate 		if (ack_mp != NULL)
2000Sstevel@tonic-gate 			freeb(ack_mp);
2014691Skcpoon 		sctp_conn_clear(connp);
2023448Sdh155122 		sctp->sctp_sctps = NULL;
2033448Sdh155122 		SCTP_G_Q_REFRELE(sctps);
2040Sstevel@tonic-gate 		kmem_cache_free(sctp_conn_cache, connp);
2050Sstevel@tonic-gate 		return (NULL);
2060Sstevel@tonic-gate 	}
2070Sstevel@tonic-gate 
2080Sstevel@tonic-gate 	sctp->sctp_ack_mp = ack_mp;
2090Sstevel@tonic-gate 	sctp->sctp_heartbeat_mp = hb_mp;
2100Sstevel@tonic-gate 
2110Sstevel@tonic-gate 	/* Inherit information from the "parent" */
2120Sstevel@tonic-gate 	sctp->sctp_ipversion = psctp->sctp_ipversion;
2130Sstevel@tonic-gate 	sctp->sctp_family = psctp->sctp_family;
2140Sstevel@tonic-gate 	pconnp = psctp->sctp_connp;
2150Sstevel@tonic-gate 	connp->conn_af_isv6 = pconnp->conn_af_isv6;
2160Sstevel@tonic-gate 	connp->conn_pkt_isv6 = pconnp->conn_pkt_isv6;
2170Sstevel@tonic-gate 	connp->conn_ipv6_v6only = pconnp->conn_ipv6_v6only;
2180Sstevel@tonic-gate 	if (sctp_init_values(sctp, psctp, KM_NOSLEEP) != 0) {
2190Sstevel@tonic-gate 		freeb(ack_mp);
2200Sstevel@tonic-gate 		freeb(hb_mp);
2214691Skcpoon 		sctp_conn_clear(connp);
2223448Sdh155122 		sctp->sctp_sctps = NULL;
2233448Sdh155122 		SCTP_G_Q_REFRELE(sctps);
2240Sstevel@tonic-gate 		kmem_cache_free(sctp_conn_cache, connp);
2250Sstevel@tonic-gate 		return (NULL);
2260Sstevel@tonic-gate 	}
2271676Sjpk 
2281676Sjpk 	/*
2291676Sjpk 	 * If the parent is multilevel, then we'll fix up the remote cred
2301676Sjpk 	 * when we do sctp_accept_comm.
2311676Sjpk 	 */
2321676Sjpk 	if ((credp = pconnp->conn_cred) != NULL) {
2331676Sjpk 		connp->conn_cred = credp;
2341676Sjpk 		crhold(credp);
2351676Sjpk 		/*
2361676Sjpk 		 * If the caller has the process-wide flag set, then default to
2371676Sjpk 		 * MAC exempt mode.  This allows read-down to unlabeled hosts.
2381676Sjpk 		 */
2391676Sjpk 		if (getpflags(NET_MAC_AWARE, credp) != 0)
2401676Sjpk 			connp->conn_mac_exempt = B_TRUE;
2410Sstevel@tonic-gate 	}
2422263Ssommerfe 
2432263Ssommerfe 	connp->conn_allzones = pconnp->conn_allzones;
2442263Ssommerfe 	connp->conn_zoneid = pconnp->conn_zoneid;
2458778SErik.Nordmark@Sun.COM 	sctp->sctp_cpid = psctp->sctp_cpid;
2468778SErik.Nordmark@Sun.COM 	sctp->sctp_open_time = lbolt64;
2472263Ssommerfe 
2480Sstevel@tonic-gate 	sctp->sctp_mss = psctp->sctp_mss;
2490Sstevel@tonic-gate 	sctp->sctp_detached = B_TRUE;
2500Sstevel@tonic-gate 	/*
2510Sstevel@tonic-gate 	 * Link to the global as soon as possible so that this sctp_t
2520Sstevel@tonic-gate 	 * can be found.
2530Sstevel@tonic-gate 	 */
2543448Sdh155122 	SCTP_LINK(sctp, sctps);
2550Sstevel@tonic-gate 
2560Sstevel@tonic-gate 	return (sctp);
2570Sstevel@tonic-gate }
2580Sstevel@tonic-gate 
2590Sstevel@tonic-gate /*
2600Sstevel@tonic-gate  * We are dying for some reason.  Try to do it gracefully.
2610Sstevel@tonic-gate  */
2620Sstevel@tonic-gate void
2630Sstevel@tonic-gate sctp_clean_death(sctp_t *sctp, int err)
2640Sstevel@tonic-gate {
2650Sstevel@tonic-gate 	ASSERT(sctp != NULL);
2660Sstevel@tonic-gate 	ASSERT((sctp->sctp_family == AF_INET &&
2670Sstevel@tonic-gate 	    sctp->sctp_ipversion == IPV4_VERSION) ||
2680Sstevel@tonic-gate 	    (sctp->sctp_family == AF_INET6 &&
2690Sstevel@tonic-gate 	    (sctp->sctp_ipversion == IPV4_VERSION ||
2700Sstevel@tonic-gate 	    sctp->sctp_ipversion == IPV6_VERSION)));
2710Sstevel@tonic-gate 
2721676Sjpk 	dprint(3, ("sctp_clean_death %p, state %d\n", (void *)sctp,
2731676Sjpk 	    sctp->sctp_state));
2740Sstevel@tonic-gate 
2750Sstevel@tonic-gate 	sctp->sctp_client_errno = err;
2760Sstevel@tonic-gate 	/*
2770Sstevel@tonic-gate 	 * Check to see if we need to notify upper layer.
2780Sstevel@tonic-gate 	 */
2790Sstevel@tonic-gate 	if ((sctp->sctp_state >= SCTPS_COOKIE_WAIT) &&
2800Sstevel@tonic-gate 	    !SCTP_IS_DETACHED(sctp)) {
2810Sstevel@tonic-gate 		if (sctp->sctp_xmit_head || sctp->sctp_xmit_unsent) {
2820Sstevel@tonic-gate 			sctp_regift_xmitlist(sctp);
2830Sstevel@tonic-gate 		}
2848348SEric.Yu@Sun.COM 		if (sctp->sctp_ulp_disconnected(sctp->sctp_ulpd, 0, err)) {
2850Sstevel@tonic-gate 			/*
2860Sstevel@tonic-gate 			 * Socket is gone, detach.
2870Sstevel@tonic-gate 			 */
2880Sstevel@tonic-gate 			sctp->sctp_detached = B_TRUE;
2890Sstevel@tonic-gate 			sctp->sctp_ulpd = NULL;
2908348SEric.Yu@Sun.COM 			sctp->sctp_upcalls = NULL;
2910Sstevel@tonic-gate 		}
2920Sstevel@tonic-gate 	}
2930Sstevel@tonic-gate 
2940Sstevel@tonic-gate 	/* Remove this sctp from all hashes. */
2950Sstevel@tonic-gate 	sctp_closei_local(sctp);
2960Sstevel@tonic-gate 
2970Sstevel@tonic-gate 	/*
2980Sstevel@tonic-gate 	 * If the sctp_t is detached, we need to finish freeing up
2990Sstevel@tonic-gate 	 * the resources.  At this point, ip_fanout_sctp() should have
3000Sstevel@tonic-gate 	 * a hold on this sctp_t.  Some thread doing snmp stuff can
3010Sstevel@tonic-gate 	 * have a hold.  And a taskq can also have a hold waiting to
3020Sstevel@tonic-gate 	 * work.  sctp_unlink() the sctp_t from the global list so
3030Sstevel@tonic-gate 	 * that no new thread can find it.  Then do a SCTP_REFRELE().
3040Sstevel@tonic-gate 	 * The sctp_t will be freed after all those threads are done.
3050Sstevel@tonic-gate 	 */
3060Sstevel@tonic-gate 	if (SCTP_IS_DETACHED(sctp)) {
3070Sstevel@tonic-gate 		SCTP_CONDEMNED(sctp);
3080Sstevel@tonic-gate 		SCTP_REFRELE(sctp);
3090Sstevel@tonic-gate 	}
3100Sstevel@tonic-gate }
3110Sstevel@tonic-gate 
3120Sstevel@tonic-gate /*
3130Sstevel@tonic-gate  * Called by upper layer when it wants to close this association.
3140Sstevel@tonic-gate  * Depending on the state of this assoication, we need to do
3150Sstevel@tonic-gate  * different things.
3160Sstevel@tonic-gate  *
3170Sstevel@tonic-gate  * If the state is below COOKIE_ECHOED or it is COOKIE_ECHOED but with
3180Sstevel@tonic-gate  * no sent data, just remove this sctp from all the hashes.  This
3190Sstevel@tonic-gate  * makes sure that all packets from the other end will go to the default
3200Sstevel@tonic-gate  * sctp handling.  The upper layer will then do a sctp_close() to clean
3210Sstevel@tonic-gate  * up.
3220Sstevel@tonic-gate  *
3230Sstevel@tonic-gate  * Otherwise, check and see if SO_LINGER is set.  If it is set, check
3240Sstevel@tonic-gate  * the value.  If the value is 0, consider this an abortive close.  Send
3250Sstevel@tonic-gate  * an ABORT message and kill the associatiion.
3260Sstevel@tonic-gate  *
3270Sstevel@tonic-gate  */
3280Sstevel@tonic-gate int
3290Sstevel@tonic-gate sctp_disconnect(sctp_t *sctp)
3300Sstevel@tonic-gate {
3310Sstevel@tonic-gate 	int	error = 0;
3320Sstevel@tonic-gate 
3331676Sjpk 	dprint(3, ("sctp_disconnect %p, state %d\n", (void *)sctp,
3341676Sjpk 	    sctp->sctp_state));
3350Sstevel@tonic-gate 
3360Sstevel@tonic-gate 	RUN_SCTP(sctp);
3370Sstevel@tonic-gate 
3380Sstevel@tonic-gate 	switch (sctp->sctp_state) {
3390Sstevel@tonic-gate 	case SCTPS_IDLE:
3400Sstevel@tonic-gate 	case SCTPS_BOUND:
3410Sstevel@tonic-gate 	case SCTPS_LISTEN:
3420Sstevel@tonic-gate 		break;
3430Sstevel@tonic-gate 	case SCTPS_COOKIE_WAIT:
3440Sstevel@tonic-gate 	case SCTPS_COOKIE_ECHOED:
3450Sstevel@tonic-gate 		/*
3460Sstevel@tonic-gate 		 * Close during the connect 3-way handshake
3470Sstevel@tonic-gate 		 * but here there may or may not be pending data
3480Sstevel@tonic-gate 		 * already on queue. Process almost same as in
3490Sstevel@tonic-gate 		 * the ESTABLISHED state.
3500Sstevel@tonic-gate 		 */
3510Sstevel@tonic-gate 		if (sctp->sctp_xmit_head == NULL &&
3520Sstevel@tonic-gate 		    sctp->sctp_xmit_unsent == NULL) {
3530Sstevel@tonic-gate 			break;
3540Sstevel@tonic-gate 		}
3550Sstevel@tonic-gate 		/* FALLTHRU */
3560Sstevel@tonic-gate 	default:
3570Sstevel@tonic-gate 		/*
3587480SKacheong.Poon@Sun.COM 		 * If SO_LINGER has set a zero linger time, terminate the
3597480SKacheong.Poon@Sun.COM 		 * association and send an ABORT.
3600Sstevel@tonic-gate 		 */
3610Sstevel@tonic-gate 		if (sctp->sctp_linger && sctp->sctp_lingertime == 0) {
3627480SKacheong.Poon@Sun.COM 			sctp_user_abort(sctp, NULL);
3637480SKacheong.Poon@Sun.COM 			WAKE_SCTP(sctp);
3647480SKacheong.Poon@Sun.COM 			return (error);
3650Sstevel@tonic-gate 		}
3660Sstevel@tonic-gate 
3670Sstevel@tonic-gate 		/*
3687480SKacheong.Poon@Sun.COM 		 * In there is unread data, send an ABORT and terminate the
3697480SKacheong.Poon@Sun.COM 		 * association.
370852Svi117747 		 */
371852Svi117747 		if (sctp->sctp_rxqueued > 0 || sctp->sctp_irwnd >
372852Svi117747 		    sctp->sctp_rwnd) {
3737480SKacheong.Poon@Sun.COM 			sctp_user_abort(sctp, NULL);
3747480SKacheong.Poon@Sun.COM 			WAKE_SCTP(sctp);
3757480SKacheong.Poon@Sun.COM 			return (error);
376852Svi117747 		}
377852Svi117747 		/*
3780Sstevel@tonic-gate 		 * Transmit the shutdown before detaching the sctp_t.
3790Sstevel@tonic-gate 		 * After sctp_detach returns this queue/perimeter
3800Sstevel@tonic-gate 		 * no longer owns the sctp_t thus others can modify it.
3810Sstevel@tonic-gate 		 */
3820Sstevel@tonic-gate 		sctp_send_shutdown(sctp, 0);
3830Sstevel@tonic-gate 
3840Sstevel@tonic-gate 		/* Pass gathered wisdom to IP for keeping */
3851735Skcpoon 		sctp_update_ire(sctp);
3860Sstevel@tonic-gate 
3870Sstevel@tonic-gate 		/*
3880Sstevel@tonic-gate 		 * If lingering on close then wait until the shutdown
3890Sstevel@tonic-gate 		 * is complete, or the SO_LINGER time passes, or an
3900Sstevel@tonic-gate 		 * ABORT is sent/received.  Note that sctp_disconnect()
3910Sstevel@tonic-gate 		 * can be called more than once.  Make sure that only
3920Sstevel@tonic-gate 		 * one thread waits.
3930Sstevel@tonic-gate 		 */
3940Sstevel@tonic-gate 		if (sctp->sctp_linger && sctp->sctp_lingertime > 0 &&
3950Sstevel@tonic-gate 		    sctp->sctp_state >= SCTPS_ESTABLISHED &&
3960Sstevel@tonic-gate 		    !sctp->sctp_lingering) {
3970Sstevel@tonic-gate 			clock_t stoptime;	/* in ticks */
3980Sstevel@tonic-gate 			clock_t ret;
3990Sstevel@tonic-gate 
4000Sstevel@tonic-gate 			/*
4010Sstevel@tonic-gate 			 * Process the sendq to send the SHUTDOWN out
4020Sstevel@tonic-gate 			 * before waiting.
4030Sstevel@tonic-gate 			 */
4040Sstevel@tonic-gate 			sctp_process_sendq(sctp);
4050Sstevel@tonic-gate 
4060Sstevel@tonic-gate 			sctp->sctp_lingering = 1;
4070Sstevel@tonic-gate 			sctp->sctp_client_errno = 0;
4080Sstevel@tonic-gate 			stoptime = lbolt + sctp->sctp_lingertime;
4090Sstevel@tonic-gate 
4100Sstevel@tonic-gate 			mutex_enter(&sctp->sctp_lock);
4110Sstevel@tonic-gate 			sctp->sctp_running = B_FALSE;
4120Sstevel@tonic-gate 			while (sctp->sctp_state >= SCTPS_ESTABLISHED &&
4130Sstevel@tonic-gate 			    sctp->sctp_client_errno == 0) {
4140Sstevel@tonic-gate 				cv_broadcast(&sctp->sctp_cv);
4150Sstevel@tonic-gate 				ret = cv_timedwait_sig(&sctp->sctp_cv,
4160Sstevel@tonic-gate 				    &sctp->sctp_lock, stoptime);
4170Sstevel@tonic-gate 				if (ret < 0) {
4180Sstevel@tonic-gate 					/* Stoptime has reached. */
4190Sstevel@tonic-gate 					sctp->sctp_client_errno = EWOULDBLOCK;
4200Sstevel@tonic-gate 					break;
4210Sstevel@tonic-gate 				} else if (ret == 0) {
4220Sstevel@tonic-gate 					/* Got a signal. */
4230Sstevel@tonic-gate 					break;
4240Sstevel@tonic-gate 				}
4250Sstevel@tonic-gate 			}
4260Sstevel@tonic-gate 			error = sctp->sctp_client_errno;
4270Sstevel@tonic-gate 			sctp->sctp_client_errno = 0;
4280Sstevel@tonic-gate 			mutex_exit(&sctp->sctp_lock);
4290Sstevel@tonic-gate 		}
4300Sstevel@tonic-gate 
4310Sstevel@tonic-gate 		WAKE_SCTP(sctp);
4320Sstevel@tonic-gate 		sctp_process_sendq(sctp);
4330Sstevel@tonic-gate 		return (error);
4340Sstevel@tonic-gate 	}
4350Sstevel@tonic-gate 
4360Sstevel@tonic-gate 
4370Sstevel@tonic-gate 	/* Remove this sctp from all hashes so nobody can find it. */
4380Sstevel@tonic-gate 	sctp_closei_local(sctp);
4390Sstevel@tonic-gate 	WAKE_SCTP(sctp);
4400Sstevel@tonic-gate 	return (error);
4410Sstevel@tonic-gate }
4420Sstevel@tonic-gate 
4430Sstevel@tonic-gate void
4440Sstevel@tonic-gate sctp_close(sctp_t *sctp)
4450Sstevel@tonic-gate {
4461676Sjpk 	dprint(3, ("sctp_close %p, state %d\n", (void *)sctp,
4471676Sjpk 	    sctp->sctp_state));
4480Sstevel@tonic-gate 
4490Sstevel@tonic-gate 	RUN_SCTP(sctp);
4500Sstevel@tonic-gate 	sctp->sctp_detached = 1;
4510Sstevel@tonic-gate 	sctp->sctp_ulpd = NULL;
4528348SEric.Yu@Sun.COM 	sctp->sctp_upcalls = NULL;
4530Sstevel@tonic-gate 	bzero(&sctp->sctp_events, sizeof (sctp->sctp_events));
4540Sstevel@tonic-gate 
4550Sstevel@tonic-gate 	/* If the graceful shutdown has not been completed, just return. */
4560Sstevel@tonic-gate 	if (sctp->sctp_state != SCTPS_IDLE) {
4570Sstevel@tonic-gate 		WAKE_SCTP(sctp);
4580Sstevel@tonic-gate 		return;
4590Sstevel@tonic-gate 	}
4600Sstevel@tonic-gate 
4610Sstevel@tonic-gate 	/*
4620Sstevel@tonic-gate 	 * Since sctp_t is in SCTPS_IDLE state, so the only thread which
4630Sstevel@tonic-gate 	 * can have a hold on the sctp_t is doing snmp stuff.  Just do
4640Sstevel@tonic-gate 	 * a SCTP_REFRELE() here after the SCTP_UNLINK().  It will
4650Sstevel@tonic-gate 	 * be freed when the other thread is done.
4660Sstevel@tonic-gate 	 */
4670Sstevel@tonic-gate 	SCTP_CONDEMNED(sctp);
4680Sstevel@tonic-gate 	WAKE_SCTP(sctp);
4690Sstevel@tonic-gate 	SCTP_REFRELE(sctp);
4700Sstevel@tonic-gate }
4710Sstevel@tonic-gate 
4720Sstevel@tonic-gate /*
4730Sstevel@tonic-gate  * Unlink from global list and do the eager close.
4740Sstevel@tonic-gate  * Remove the refhold implicit in being on the global list.
4750Sstevel@tonic-gate  */
4760Sstevel@tonic-gate void
4770Sstevel@tonic-gate sctp_close_eager(sctp_t *sctp)
4780Sstevel@tonic-gate {
4790Sstevel@tonic-gate 	SCTP_CONDEMNED(sctp);
4800Sstevel@tonic-gate 	sctp_closei_local(sctp);
4810Sstevel@tonic-gate 	SCTP_REFRELE(sctp);
4820Sstevel@tonic-gate }
4830Sstevel@tonic-gate 
4840Sstevel@tonic-gate /*
4850Sstevel@tonic-gate  * The sctp_t is going away. Remove it from all lists and set it
4860Sstevel@tonic-gate  * to SCTPS_IDLE. The caller has to remove it from the
4870Sstevel@tonic-gate  * global list. The freeing up of memory is deferred until
4880Sstevel@tonic-gate  * sctp_free(). This is needed since a thread in sctp_input() might have
4890Sstevel@tonic-gate  * done a SCTP_REFHOLD on this structure before it was removed from the
4900Sstevel@tonic-gate  * hashes.
4910Sstevel@tonic-gate  */
4920Sstevel@tonic-gate static void
4930Sstevel@tonic-gate sctp_closei_local(sctp_t *sctp)
4940Sstevel@tonic-gate {
4950Sstevel@tonic-gate 	mblk_t	*mp;
4960Sstevel@tonic-gate 	ire_t	*ire = NULL;
4970Sstevel@tonic-gate 	conn_t	*connp = sctp->sctp_connp;
4980Sstevel@tonic-gate 
4994505Skcpoon 	/* Sanity check, don't do the same thing twice.  */
5004505Skcpoon 	if (connp->conn_state_flags & CONN_CLOSING) {
5014505Skcpoon 		ASSERT(sctp->sctp_state == SCTPS_IDLE);
5024505Skcpoon 		return;
5034505Skcpoon 	}
5044505Skcpoon 
5050Sstevel@tonic-gate 	/* Stop and free the timers */
5060Sstevel@tonic-gate 	sctp_free_faddr_timers(sctp);
5070Sstevel@tonic-gate 	if ((mp = sctp->sctp_heartbeat_mp) != NULL) {
5080Sstevel@tonic-gate 		sctp_timer_free(mp);
5090Sstevel@tonic-gate 		sctp->sctp_heartbeat_mp = NULL;
5100Sstevel@tonic-gate 	}
5110Sstevel@tonic-gate 	if ((mp = sctp->sctp_ack_mp) != NULL) {
5120Sstevel@tonic-gate 		sctp_timer_free(mp);
5130Sstevel@tonic-gate 		sctp->sctp_ack_mp = NULL;
5140Sstevel@tonic-gate 	}
5150Sstevel@tonic-gate 
5160Sstevel@tonic-gate 	/* Set the CONN_CLOSING flag so that IP will not cache IRE again. */
5170Sstevel@tonic-gate 	mutex_enter(&connp->conn_lock);
5180Sstevel@tonic-gate 	connp->conn_state_flags |= CONN_CLOSING;
5190Sstevel@tonic-gate 	ire = connp->conn_ire_cache;
5200Sstevel@tonic-gate 	connp->conn_ire_cache = NULL;
5210Sstevel@tonic-gate 	mutex_exit(&connp->conn_lock);
5220Sstevel@tonic-gate 	if (ire != NULL)
5230Sstevel@tonic-gate 		IRE_REFRELE_NOTR(ire);
5240Sstevel@tonic-gate 
5250Sstevel@tonic-gate 	/* Remove from all hashes. */
5260Sstevel@tonic-gate 	sctp_bind_hash_remove(sctp);
5270Sstevel@tonic-gate 	sctp_conn_hash_remove(sctp);
5280Sstevel@tonic-gate 	sctp_listen_hash_remove(sctp);
5290Sstevel@tonic-gate 	sctp->sctp_state = SCTPS_IDLE;
5300Sstevel@tonic-gate 
5310Sstevel@tonic-gate 	/*
5320Sstevel@tonic-gate 	 * Clean up the recvq as much as possible.  All those packets
5330Sstevel@tonic-gate 	 * will be silently dropped as this sctp_t is now in idle state.
5340Sstevel@tonic-gate 	 */
5350Sstevel@tonic-gate 	mutex_enter(&sctp->sctp_recvq_lock);
5360Sstevel@tonic-gate 	while ((mp = sctp->sctp_recvq) != NULL) {
5370Sstevel@tonic-gate 		mblk_t *ipsec_mp;
5380Sstevel@tonic-gate 
5390Sstevel@tonic-gate 		sctp->sctp_recvq = mp->b_next;
5400Sstevel@tonic-gate 		mp->b_next = NULL;
5410Sstevel@tonic-gate 		if ((ipsec_mp = mp->b_prev) != NULL) {
5420Sstevel@tonic-gate 			freeb(ipsec_mp);
5430Sstevel@tonic-gate 			mp->b_prev = NULL;
5440Sstevel@tonic-gate 		}
5450Sstevel@tonic-gate 		freemsg(mp);
5460Sstevel@tonic-gate 	}
5470Sstevel@tonic-gate 	mutex_exit(&sctp->sctp_recvq_lock);
5480Sstevel@tonic-gate }
5490Sstevel@tonic-gate 
5500Sstevel@tonic-gate /*
5510Sstevel@tonic-gate  * Free memory associated with the sctp/ip header template.
5520Sstevel@tonic-gate  */
5530Sstevel@tonic-gate static void
5540Sstevel@tonic-gate sctp_headers_free(sctp_t *sctp)
5550Sstevel@tonic-gate {
5560Sstevel@tonic-gate 	if (sctp->sctp_iphc != NULL) {
5570Sstevel@tonic-gate 		kmem_free(sctp->sctp_iphc, sctp->sctp_iphc_len);
5580Sstevel@tonic-gate 		sctp->sctp_iphc = NULL;
5590Sstevel@tonic-gate 		sctp->sctp_ipha = NULL;
5600Sstevel@tonic-gate 		sctp->sctp_hdr_len = 0;
5610Sstevel@tonic-gate 		sctp->sctp_ip_hdr_len = 0;
5620Sstevel@tonic-gate 		sctp->sctp_iphc_len = 0;
5630Sstevel@tonic-gate 		sctp->sctp_sctph = NULL;
5640Sstevel@tonic-gate 		sctp->sctp_hdr_len = 0;
5650Sstevel@tonic-gate 	}
5660Sstevel@tonic-gate 	if (sctp->sctp_iphc6 != NULL) {
5670Sstevel@tonic-gate 		kmem_free(sctp->sctp_iphc6, sctp->sctp_iphc6_len);
5680Sstevel@tonic-gate 		sctp->sctp_iphc6 = NULL;
5690Sstevel@tonic-gate 		sctp->sctp_ip6h = NULL;
5700Sstevel@tonic-gate 		sctp->sctp_hdr6_len = 0;
5710Sstevel@tonic-gate 		sctp->sctp_ip_hdr6_len = 0;
5720Sstevel@tonic-gate 		sctp->sctp_iphc6_len = 0;
5730Sstevel@tonic-gate 		sctp->sctp_sctph6 = NULL;
5740Sstevel@tonic-gate 		sctp->sctp_hdr6_len = 0;
5750Sstevel@tonic-gate 	}
5760Sstevel@tonic-gate }
5770Sstevel@tonic-gate 
5780Sstevel@tonic-gate static void
5790Sstevel@tonic-gate sctp_free_xmit_data(sctp_t *sctp)
5800Sstevel@tonic-gate {
5810Sstevel@tonic-gate 	mblk_t	*ump = NULL;
5820Sstevel@tonic-gate 	mblk_t	*nump;
5830Sstevel@tonic-gate 	mblk_t	*mp;
5840Sstevel@tonic-gate 	mblk_t	*nmp;
5850Sstevel@tonic-gate 
5860Sstevel@tonic-gate 	sctp->sctp_xmit_unacked = NULL;
5870Sstevel@tonic-gate 	ump = sctp->sctp_xmit_head;
5880Sstevel@tonic-gate 	sctp->sctp_xmit_tail = sctp->sctp_xmit_head = NULL;
5890Sstevel@tonic-gate free_unsent:
5900Sstevel@tonic-gate 	for (; ump != NULL; ump = nump) {
5910Sstevel@tonic-gate 		for (mp = ump->b_cont; mp != NULL; mp = nmp) {
5920Sstevel@tonic-gate 			nmp = mp->b_next;
5930Sstevel@tonic-gate 			mp->b_next = NULL;
5940Sstevel@tonic-gate 			mp->b_prev = NULL;
5950Sstevel@tonic-gate 			freemsg(mp);
5960Sstevel@tonic-gate 		}
5970Sstevel@tonic-gate 		ASSERT(DB_REF(ump) == 1);
5980Sstevel@tonic-gate 		nump = ump->b_next;
5990Sstevel@tonic-gate 		ump->b_next = NULL;
6000Sstevel@tonic-gate 		ump->b_prev = NULL;
6010Sstevel@tonic-gate 		ump->b_cont = NULL;
6020Sstevel@tonic-gate 		freeb(ump);
6030Sstevel@tonic-gate 	}
6040Sstevel@tonic-gate 	if ((ump = sctp->sctp_xmit_unsent) == NULL) {
6050Sstevel@tonic-gate 		ASSERT(sctp->sctp_xmit_unsent_tail == NULL);
6060Sstevel@tonic-gate 		return;
6070Sstevel@tonic-gate 	}
6080Sstevel@tonic-gate 	sctp->sctp_xmit_unsent = sctp->sctp_xmit_unsent_tail = NULL;
6090Sstevel@tonic-gate 	goto free_unsent;
6100Sstevel@tonic-gate }
6110Sstevel@tonic-gate 
6120Sstevel@tonic-gate /*
6130Sstevel@tonic-gate  * Cleanup all the messages in the stream queue and the reassembly lists.
6140Sstevel@tonic-gate  * If 'free' is true, then delete the streams as well.
6150Sstevel@tonic-gate  */
6160Sstevel@tonic-gate void
6170Sstevel@tonic-gate sctp_instream_cleanup(sctp_t *sctp, boolean_t free)
6180Sstevel@tonic-gate {
6190Sstevel@tonic-gate 	int	i;
6200Sstevel@tonic-gate 	mblk_t	*mp;
6210Sstevel@tonic-gate 	mblk_t	*mp1;
6220Sstevel@tonic-gate 
6230Sstevel@tonic-gate 	if (sctp->sctp_instr != NULL) {
6240Sstevel@tonic-gate 		/* walk thru and flush out anything remaining in the Q */
6250Sstevel@tonic-gate 		for (i = 0; i < sctp->sctp_num_istr; i++) {
6260Sstevel@tonic-gate 			mp = sctp->sctp_instr[i].istr_msgs;
6270Sstevel@tonic-gate 			while (mp != NULL) {
6280Sstevel@tonic-gate 				mp1 = mp->b_next;
6290Sstevel@tonic-gate 				mp->b_next = mp->b_prev = NULL;
6300Sstevel@tonic-gate 				freemsg(mp);
6310Sstevel@tonic-gate 				mp = mp1;
6320Sstevel@tonic-gate 			}
6330Sstevel@tonic-gate 			sctp->sctp_instr[i].istr_msgs = NULL;
6344116Snh145002 			sctp->sctp_instr[i].istr_nmsgs = 0;
6350Sstevel@tonic-gate 			sctp_free_reass((sctp->sctp_instr) + i);
6360Sstevel@tonic-gate 			sctp->sctp_instr[i].nextseq = 0;
6370Sstevel@tonic-gate 		}
6380Sstevel@tonic-gate 		if (free) {
6390Sstevel@tonic-gate 			kmem_free(sctp->sctp_instr,
6400Sstevel@tonic-gate 			    sizeof (*sctp->sctp_instr) * sctp->sctp_num_istr);
6410Sstevel@tonic-gate 			sctp->sctp_instr = NULL;
6420Sstevel@tonic-gate 			sctp->sctp_num_istr = 0;
6430Sstevel@tonic-gate 		}
6440Sstevel@tonic-gate 	}
6450Sstevel@tonic-gate 	/* un-ordered fragments */
6460Sstevel@tonic-gate 	if (sctp->sctp_uo_frags != NULL) {
6470Sstevel@tonic-gate 		for (mp = sctp->sctp_uo_frags; mp != NULL; mp = mp1) {
6480Sstevel@tonic-gate 			mp1 = mp->b_next;
6490Sstevel@tonic-gate 			mp->b_next = mp->b_prev = NULL;
6500Sstevel@tonic-gate 			freemsg(mp);
6510Sstevel@tonic-gate 		}
6520Sstevel@tonic-gate 	}
6530Sstevel@tonic-gate }
6540Sstevel@tonic-gate 
6550Sstevel@tonic-gate /*
6560Sstevel@tonic-gate  * Last reference to the sctp_t is gone. Free all memory associated with it.
6570Sstevel@tonic-gate  * Called from SCTP_REFRELE. Called inline in sctp_close()
6580Sstevel@tonic-gate  */
6590Sstevel@tonic-gate void
6600Sstevel@tonic-gate sctp_free(conn_t *connp)
6610Sstevel@tonic-gate {
6620Sstevel@tonic-gate 	sctp_t *sctp = CONN2SCTP(connp);
6630Sstevel@tonic-gate 	int		cnt;
6643448Sdh155122 	sctp_stack_t	*sctps = sctp->sctp_sctps;
6650Sstevel@tonic-gate 
6663448Sdh155122 	ASSERT(sctps != NULL);
6670Sstevel@tonic-gate 	/* Unlink it from the global list */
6683448Sdh155122 	SCTP_UNLINK(sctp, sctps);
6690Sstevel@tonic-gate 
6700Sstevel@tonic-gate 	ASSERT(connp->conn_ref == 0);
6710Sstevel@tonic-gate 	ASSERT(connp->conn_ulp == IPPROTO_SCTP);
6720Sstevel@tonic-gate 	ASSERT(!MUTEX_HELD(&sctp->sctp_reflock));
6730Sstevel@tonic-gate 	ASSERT(sctp->sctp_refcnt == 0);
6740Sstevel@tonic-gate 
6750Sstevel@tonic-gate 	ASSERT(sctp->sctp_ptpbhn == NULL && sctp->sctp_bind_hash == NULL);
6760Sstevel@tonic-gate 	ASSERT(sctp->sctp_conn_hash_next == NULL &&
6770Sstevel@tonic-gate 	    sctp->sctp_conn_hash_prev == NULL);
6780Sstevel@tonic-gate 
6790Sstevel@tonic-gate 
6800Sstevel@tonic-gate 	/* Free up all the resources. */
6810Sstevel@tonic-gate 
6820Sstevel@tonic-gate 	/* blow away sctp stream management */
6830Sstevel@tonic-gate 	if (sctp->sctp_ostrcntrs != NULL) {
6840Sstevel@tonic-gate 		kmem_free(sctp->sctp_ostrcntrs,
6850Sstevel@tonic-gate 		    sizeof (uint16_t) * sctp->sctp_num_ostr);
6860Sstevel@tonic-gate 		sctp->sctp_ostrcntrs = NULL;
6870Sstevel@tonic-gate 	}
6880Sstevel@tonic-gate 	sctp_instream_cleanup(sctp, B_TRUE);
6890Sstevel@tonic-gate 
6900Sstevel@tonic-gate 	/* Remove all data transfer resources. */
6910Sstevel@tonic-gate 	sctp->sctp_istr_nmsgs = 0;
6920Sstevel@tonic-gate 	sctp->sctp_rxqueued = 0;
6930Sstevel@tonic-gate 	sctp_free_xmit_data(sctp);
6940Sstevel@tonic-gate 	sctp->sctp_unacked = 0;
6950Sstevel@tonic-gate 	sctp->sctp_unsent = 0;
696852Svi117747 	if (sctp->sctp_cxmit_list != NULL)
697852Svi117747 		sctp_asconf_free_cxmit(sctp, NULL);
698852Svi117747 
6990Sstevel@tonic-gate 	sctp->sctp_lastdata = NULL;
7000Sstevel@tonic-gate 
7010Sstevel@tonic-gate 	/* Clear out default xmit settings */
7020Sstevel@tonic-gate 	sctp->sctp_def_stream = 0;
7030Sstevel@tonic-gate 	sctp->sctp_def_flags = 0;
7040Sstevel@tonic-gate 	sctp->sctp_def_ppid = 0;
7050Sstevel@tonic-gate 	sctp->sctp_def_context = 0;
7060Sstevel@tonic-gate 	sctp->sctp_def_timetolive = 0;
7070Sstevel@tonic-gate 
7080Sstevel@tonic-gate 	if (sctp->sctp_sack_info != NULL) {
7090Sstevel@tonic-gate 		sctp_free_set(sctp->sctp_sack_info);
7100Sstevel@tonic-gate 		sctp->sctp_sack_info = NULL;
7110Sstevel@tonic-gate 	}
7120Sstevel@tonic-gate 	sctp->sctp_sack_gaps = 0;
7130Sstevel@tonic-gate 
7140Sstevel@tonic-gate 	if (sctp->sctp_cookie_mp != NULL) {
7150Sstevel@tonic-gate 		freemsg(sctp->sctp_cookie_mp);
7160Sstevel@tonic-gate 		sctp->sctp_cookie_mp = NULL;
7170Sstevel@tonic-gate 	}
7180Sstevel@tonic-gate 
7190Sstevel@tonic-gate 	/* Remove all the address resources. */
7200Sstevel@tonic-gate 	sctp_zap_addrs(sctp);
7210Sstevel@tonic-gate 	for (cnt = 0; cnt < SCTP_IPIF_HASH; cnt++) {
7220Sstevel@tonic-gate 		ASSERT(sctp->sctp_saddrs[cnt].ipif_count == 0);
7230Sstevel@tonic-gate 		list_destroy(&sctp->sctp_saddrs[cnt].sctp_ipif_list);
7240Sstevel@tonic-gate 	}
7250Sstevel@tonic-gate 
7261676Sjpk 	ip6_pkt_free(&sctp->sctp_sticky_ipp);
7270Sstevel@tonic-gate 
7280Sstevel@tonic-gate 	if (sctp->sctp_hopopts != NULL) {
7290Sstevel@tonic-gate 		mi_free(sctp->sctp_hopopts);
7300Sstevel@tonic-gate 		sctp->sctp_hopopts = NULL;
7310Sstevel@tonic-gate 		sctp->sctp_hopoptslen = 0;
7320Sstevel@tonic-gate 	}
7330Sstevel@tonic-gate 	ASSERT(sctp->sctp_hopoptslen == 0);
7340Sstevel@tonic-gate 	if (sctp->sctp_dstopts != NULL) {
7350Sstevel@tonic-gate 		mi_free(sctp->sctp_dstopts);
7360Sstevel@tonic-gate 		sctp->sctp_dstopts = NULL;
7370Sstevel@tonic-gate 		sctp->sctp_dstoptslen = 0;
7380Sstevel@tonic-gate 	}
7390Sstevel@tonic-gate 	ASSERT(sctp->sctp_dstoptslen == 0);
7400Sstevel@tonic-gate 	if (sctp->sctp_rtdstopts != NULL) {
7410Sstevel@tonic-gate 		mi_free(sctp->sctp_rtdstopts);
7420Sstevel@tonic-gate 		sctp->sctp_rtdstopts = NULL;
7430Sstevel@tonic-gate 		sctp->sctp_rtdstoptslen = 0;
7440Sstevel@tonic-gate 	}
7450Sstevel@tonic-gate 	ASSERT(sctp->sctp_rtdstoptslen == 0);
7460Sstevel@tonic-gate 	if (sctp->sctp_rthdr != NULL) {
7470Sstevel@tonic-gate 		mi_free(sctp->sctp_rthdr);
7480Sstevel@tonic-gate 		sctp->sctp_rthdr = NULL;
7490Sstevel@tonic-gate 		sctp->sctp_rthdrlen = 0;
7500Sstevel@tonic-gate 	}
7510Sstevel@tonic-gate 	ASSERT(sctp->sctp_rthdrlen == 0);
7520Sstevel@tonic-gate 	sctp_headers_free(sctp);
7530Sstevel@tonic-gate 
7540Sstevel@tonic-gate 	sctp->sctp_shutdown_faddr = NULL;
7550Sstevel@tonic-gate 
7564964Skcpoon 	if (sctp->sctp_err_chunks != NULL) {
7574964Skcpoon 		freemsg(sctp->sctp_err_chunks);
7584964Skcpoon 		sctp->sctp_err_chunks = NULL;
7594964Skcpoon 		sctp->sctp_err_len = 0;
7604964Skcpoon 	}
7614964Skcpoon 
7620Sstevel@tonic-gate 	/* Clear all the bitfields. */
7630Sstevel@tonic-gate 	bzero(&sctp->sctp_bits, sizeof (sctp->sctp_bits));
7640Sstevel@tonic-gate 
7650Sstevel@tonic-gate 	/* It is time to update the global statistics. */
7663448Sdh155122 	UPDATE_MIB(&sctps->sctps_mib, sctpOutSCTPPkts, sctp->sctp_opkts);
7673448Sdh155122 	UPDATE_MIB(&sctps->sctps_mib, sctpOutCtrlChunks, sctp->sctp_obchunks);
7683448Sdh155122 	UPDATE_MIB(&sctps->sctps_mib, sctpOutOrderChunks, sctp->sctp_odchunks);
7693448Sdh155122 	UPDATE_MIB(&sctps->sctps_mib,
7703448Sdh155122 	    sctpOutUnorderChunks, sctp->sctp_oudchunks);
7713448Sdh155122 	UPDATE_MIB(&sctps->sctps_mib, sctpRetransChunks, sctp->sctp_rxtchunks);
7723448Sdh155122 	UPDATE_MIB(&sctps->sctps_mib, sctpInSCTPPkts, sctp->sctp_ipkts);
7733448Sdh155122 	UPDATE_MIB(&sctps->sctps_mib, sctpInCtrlChunks, sctp->sctp_ibchunks);
7743448Sdh155122 	UPDATE_MIB(&sctps->sctps_mib, sctpInOrderChunks, sctp->sctp_idchunks);
7753448Sdh155122 	UPDATE_MIB(&sctps->sctps_mib,
7763448Sdh155122 	    sctpInUnorderChunks, sctp->sctp_iudchunks);
7773448Sdh155122 	UPDATE_MIB(&sctps->sctps_mib, sctpFragUsrMsgs, sctp->sctp_fragdmsgs);
7783448Sdh155122 	UPDATE_MIB(&sctps->sctps_mib, sctpReasmUsrMsgs, sctp->sctp_reassmsgs);
7790Sstevel@tonic-gate 	sctp->sctp_opkts = 0;
7800Sstevel@tonic-gate 	sctp->sctp_obchunks = 0;
7810Sstevel@tonic-gate 	sctp->sctp_odchunks = 0;
7820Sstevel@tonic-gate 	sctp->sctp_oudchunks = 0;
7830Sstevel@tonic-gate 	sctp->sctp_rxtchunks = 0;
7840Sstevel@tonic-gate 	sctp->sctp_ipkts = 0;
7850Sstevel@tonic-gate 	sctp->sctp_ibchunks = 0;
7860Sstevel@tonic-gate 	sctp->sctp_idchunks = 0;
7870Sstevel@tonic-gate 	sctp->sctp_iudchunks = 0;
7880Sstevel@tonic-gate 	sctp->sctp_fragdmsgs = 0;
7890Sstevel@tonic-gate 	sctp->sctp_reassmsgs = 0;
79010751SGeorge.Shepherd@Sun.COM 	sctp->sctp_outseqtsns = 0;
79110751SGeorge.Shepherd@Sun.COM 	sctp->sctp_osacks = 0;
79210751SGeorge.Shepherd@Sun.COM 	sctp->sctp_isacks = 0;
79310751SGeorge.Shepherd@Sun.COM 	sctp->sctp_idupchunks = 0;
79410751SGeorge.Shepherd@Sun.COM 	sctp->sctp_gapcnt = 0;
79510751SGeorge.Shepherd@Sun.COM 	sctp->sctp_cum_obchunks = 0;
79610751SGeorge.Shepherd@Sun.COM 	sctp->sctp_cum_odchunks = 0;
79710751SGeorge.Shepherd@Sun.COM 	sctp->sctp_cum_oudchunks = 0;
79810751SGeorge.Shepherd@Sun.COM 	sctp->sctp_cum_rxtchunks = 0;
79910751SGeorge.Shepherd@Sun.COM 	sctp->sctp_cum_ibchunks = 0;
80010751SGeorge.Shepherd@Sun.COM 	sctp->sctp_cum_idchunks = 0;
80110751SGeorge.Shepherd@Sun.COM 	sctp->sctp_cum_iudchunks = 0;
8020Sstevel@tonic-gate 
8030Sstevel@tonic-gate 	sctp->sctp_autoclose = 0;
8045586Skcpoon 	sctp->sctp_tx_adaptation_code = 0;
8050Sstevel@tonic-gate 
8062283Skp158701 	sctp->sctp_v6label_len = 0;
8072283Skp158701 	sctp->sctp_v4label_len = 0;
8082283Skp158701 
8093448Sdh155122 	/* Every sctp_t holds one reference on the default queue */
8103448Sdh155122 	sctp->sctp_sctps = NULL;
8113448Sdh155122 	SCTP_G_Q_REFRELE(sctps);
8123448Sdh155122 
8134691Skcpoon 	sctp_conn_clear(connp);
8140Sstevel@tonic-gate 	kmem_cache_free(sctp_conn_cache, connp);
8150Sstevel@tonic-gate }
8160Sstevel@tonic-gate 
8170Sstevel@tonic-gate /* Diagnostic routine used to return a string associated with the sctp state. */
8180Sstevel@tonic-gate char *
8190Sstevel@tonic-gate sctp_display(sctp_t *sctp, char *sup_buf)
8200Sstevel@tonic-gate {
8210Sstevel@tonic-gate 	char	*buf;
8220Sstevel@tonic-gate 	char	buf1[30];
8230Sstevel@tonic-gate 	static char	priv_buf[INET6_ADDRSTRLEN * 2 + 80];
8240Sstevel@tonic-gate 	char	*cp;
8250Sstevel@tonic-gate 
8260Sstevel@tonic-gate 	if (sctp == NULL)
8270Sstevel@tonic-gate 		return ("NULL_SCTP");
8280Sstevel@tonic-gate 
8290Sstevel@tonic-gate 	buf = (sup_buf != NULL) ? sup_buf : priv_buf;
8300Sstevel@tonic-gate 
8310Sstevel@tonic-gate 	switch (sctp->sctp_state) {
8320Sstevel@tonic-gate 	case SCTPS_IDLE:
8330Sstevel@tonic-gate 		cp = "SCTP_IDLE";
8340Sstevel@tonic-gate 		break;
8350Sstevel@tonic-gate 	case SCTPS_BOUND:
8360Sstevel@tonic-gate 		cp = "SCTP_BOUND";
8370Sstevel@tonic-gate 		break;
8380Sstevel@tonic-gate 	case SCTPS_LISTEN:
8390Sstevel@tonic-gate 		cp = "SCTP_LISTEN";
8400Sstevel@tonic-gate 		break;
8410Sstevel@tonic-gate 	case SCTPS_COOKIE_WAIT:
8420Sstevel@tonic-gate 		cp = "SCTP_COOKIE_WAIT";
8430Sstevel@tonic-gate 		break;
8440Sstevel@tonic-gate 	case SCTPS_COOKIE_ECHOED:
8450Sstevel@tonic-gate 		cp = "SCTP_COOKIE_ECHOED";
8460Sstevel@tonic-gate 		break;
8470Sstevel@tonic-gate 	case SCTPS_ESTABLISHED:
8480Sstevel@tonic-gate 		cp = "SCTP_ESTABLISHED";
8490Sstevel@tonic-gate 		break;
8500Sstevel@tonic-gate 	case SCTPS_SHUTDOWN_PENDING:
8510Sstevel@tonic-gate 		cp = "SCTP_SHUTDOWN_PENDING";
8520Sstevel@tonic-gate 		break;
8530Sstevel@tonic-gate 	case SCTPS_SHUTDOWN_SENT:
8540Sstevel@tonic-gate 		cp = "SCTPS_SHUTDOWN_SENT";
8550Sstevel@tonic-gate 		break;
8560Sstevel@tonic-gate 	case SCTPS_SHUTDOWN_RECEIVED:
8570Sstevel@tonic-gate 		cp = "SCTPS_SHUTDOWN_RECEIVED";
8580Sstevel@tonic-gate 		break;
8590Sstevel@tonic-gate 	case SCTPS_SHUTDOWN_ACK_SENT:
8600Sstevel@tonic-gate 		cp = "SCTPS_SHUTDOWN_ACK_SENT";
8610Sstevel@tonic-gate 		break;
8620Sstevel@tonic-gate 	default:
8630Sstevel@tonic-gate 		(void) mi_sprintf(buf1, "SCTPUnkState(%d)", sctp->sctp_state);
8640Sstevel@tonic-gate 		cp = buf1;
8650Sstevel@tonic-gate 		break;
8660Sstevel@tonic-gate 	}
8670Sstevel@tonic-gate 	(void) mi_sprintf(buf, "[%u, %u] %s",
8680Sstevel@tonic-gate 	    ntohs(sctp->sctp_lport), ntohs(sctp->sctp_fport), cp);
8690Sstevel@tonic-gate 
8700Sstevel@tonic-gate 	return (buf);
8710Sstevel@tonic-gate }
8720Sstevel@tonic-gate 
8730Sstevel@tonic-gate /*
8740Sstevel@tonic-gate  * Initialize protocol control block. If a parent exists, inherit
8750Sstevel@tonic-gate  * all values set through setsockopt().
8760Sstevel@tonic-gate  */
8770Sstevel@tonic-gate static int
8780Sstevel@tonic-gate sctp_init_values(sctp_t *sctp, sctp_t *psctp, int sleep)
8790Sstevel@tonic-gate {
8800Sstevel@tonic-gate 	int	err;
8810Sstevel@tonic-gate 	int	cnt;
8823448Sdh155122 	sctp_stack_t	*sctps = sctp->sctp_sctps;
8832263Ssommerfe 	conn_t 	*connp, *pconnp;
8840Sstevel@tonic-gate 
8850Sstevel@tonic-gate 	ASSERT((sctp->sctp_family == AF_INET &&
8860Sstevel@tonic-gate 	    sctp->sctp_ipversion == IPV4_VERSION) ||
8870Sstevel@tonic-gate 	    (sctp->sctp_family == AF_INET6 &&
8880Sstevel@tonic-gate 	    (sctp->sctp_ipversion == IPV4_VERSION ||
8890Sstevel@tonic-gate 	    sctp->sctp_ipversion == IPV6_VERSION)));
8900Sstevel@tonic-gate 
8910Sstevel@tonic-gate 	sctp->sctp_nsaddrs = 0;
8920Sstevel@tonic-gate 	for (cnt = 0; cnt < SCTP_IPIF_HASH; cnt++) {
8930Sstevel@tonic-gate 		sctp->sctp_saddrs[cnt].ipif_count = 0;
8940Sstevel@tonic-gate 		list_create(&sctp->sctp_saddrs[cnt].sctp_ipif_list,
8950Sstevel@tonic-gate 		    sizeof (sctp_saddr_ipif_t), offsetof(sctp_saddr_ipif_t,
8960Sstevel@tonic-gate 		    saddr_ipif));
8970Sstevel@tonic-gate 	}
8980Sstevel@tonic-gate 	sctp->sctp_ports = 0;
8990Sstevel@tonic-gate 	sctp->sctp_running = B_FALSE;
9000Sstevel@tonic-gate 	sctp->sctp_state = SCTPS_IDLE;
9010Sstevel@tonic-gate 
9020Sstevel@tonic-gate 	sctp->sctp_refcnt = 1;
9030Sstevel@tonic-gate 
9040Sstevel@tonic-gate 	sctp->sctp_strikes = 0;
9050Sstevel@tonic-gate 
9060Sstevel@tonic-gate 	sctp->sctp_last_mtu_probe = lbolt64;
9073448Sdh155122 	sctp->sctp_mtu_probe_intvl = sctps->sctps_mtu_probe_interval;
9080Sstevel@tonic-gate 
9090Sstevel@tonic-gate 	sctp->sctp_sack_gaps = 0;
9100Sstevel@tonic-gate 	sctp->sctp_sack_toggle = 2;
9110Sstevel@tonic-gate 
9124691Skcpoon 	/* Only need to do the allocation if there is no "cached" one. */
9134691Skcpoon 	if (sctp->sctp_pad_mp == NULL) {
9144691Skcpoon 		if (sleep == KM_SLEEP) {
9154691Skcpoon 			sctp->sctp_pad_mp = allocb_wait(SCTP_ALIGN, BPRI_MED,
9164691Skcpoon 			    STR_NOSIG, NULL);
9174691Skcpoon 		} else {
9184691Skcpoon 			sctp->sctp_pad_mp = allocb(SCTP_ALIGN, BPRI_MED);
9194691Skcpoon 			if (sctp->sctp_pad_mp == NULL)
9204691Skcpoon 				return (ENOMEM);
9214691Skcpoon 		}
9224691Skcpoon 		bzero(sctp->sctp_pad_mp->b_rptr, SCTP_ALIGN);
9234691Skcpoon 	}
9244691Skcpoon 
9250Sstevel@tonic-gate 	if (psctp != NULL) {
9260Sstevel@tonic-gate 		/*
9270Sstevel@tonic-gate 		 * Inherit from parent
9280Sstevel@tonic-gate 		 */
9294691Skcpoon 		sctp->sctp_iphc = kmem_zalloc(psctp->sctp_iphc_len, sleep);
9304691Skcpoon 		if (sctp->sctp_iphc == NULL) {
9314691Skcpoon 			sctp->sctp_iphc_len = 0;
9324691Skcpoon 			err = ENOMEM;
9334691Skcpoon 			goto failure;
9344691Skcpoon 		}
9350Sstevel@tonic-gate 		sctp->sctp_iphc_len = psctp->sctp_iphc_len;
9360Sstevel@tonic-gate 		sctp->sctp_hdr_len = psctp->sctp_hdr_len;
9370Sstevel@tonic-gate 
9384691Skcpoon 		sctp->sctp_iphc6 = kmem_zalloc(psctp->sctp_iphc6_len, sleep);
9390Sstevel@tonic-gate 		if (sctp->sctp_iphc6 == NULL) {
9400Sstevel@tonic-gate 			sctp->sctp_iphc6_len = 0;
9414691Skcpoon 			err = ENOMEM;
9424691Skcpoon 			goto failure;
9430Sstevel@tonic-gate 		}
9440Sstevel@tonic-gate 		sctp->sctp_iphc6_len = psctp->sctp_iphc6_len;
9450Sstevel@tonic-gate 		sctp->sctp_hdr6_len = psctp->sctp_hdr6_len;
9460Sstevel@tonic-gate 
9470Sstevel@tonic-gate 		sctp->sctp_ip_hdr_len = psctp->sctp_ip_hdr_len;
9480Sstevel@tonic-gate 		sctp->sctp_ip_hdr6_len = psctp->sctp_ip_hdr6_len;
9490Sstevel@tonic-gate 
9500Sstevel@tonic-gate 		/*
9510Sstevel@tonic-gate 		 * Copy the IP+SCTP header templates from listener
9520Sstevel@tonic-gate 		 */
9530Sstevel@tonic-gate 		bcopy(psctp->sctp_iphc, sctp->sctp_iphc,
9540Sstevel@tonic-gate 		    psctp->sctp_hdr_len);
9550Sstevel@tonic-gate 		sctp->sctp_ipha = (ipha_t *)sctp->sctp_iphc;
9560Sstevel@tonic-gate 		sctp->sctp_sctph = (sctp_hdr_t *)(sctp->sctp_iphc +
9570Sstevel@tonic-gate 		    sctp->sctp_ip_hdr_len);
9580Sstevel@tonic-gate 
9590Sstevel@tonic-gate 		bcopy(psctp->sctp_iphc6, sctp->sctp_iphc6,
9600Sstevel@tonic-gate 		    psctp->sctp_hdr6_len);
9610Sstevel@tonic-gate 		if (((ip6i_t *)(sctp->sctp_iphc6))->ip6i_nxt == IPPROTO_RAW) {
9620Sstevel@tonic-gate 			sctp->sctp_ip6h = (ip6_t *)(sctp->sctp_iphc6 +
9630Sstevel@tonic-gate 			    sizeof (ip6i_t));
9640Sstevel@tonic-gate 		} else {
9650Sstevel@tonic-gate 			sctp->sctp_ip6h = (ip6_t *)sctp->sctp_iphc6;
9660Sstevel@tonic-gate 		}
9670Sstevel@tonic-gate 		sctp->sctp_sctph6 = (sctp_hdr_t *)(sctp->sctp_iphc6 +
9680Sstevel@tonic-gate 		    sctp->sctp_ip_hdr6_len);
9690Sstevel@tonic-gate 
9700Sstevel@tonic-gate 		sctp->sctp_cookie_lifetime = psctp->sctp_cookie_lifetime;
9710Sstevel@tonic-gate 		sctp->sctp_xmit_lowater = psctp->sctp_xmit_lowater;
9720Sstevel@tonic-gate 		sctp->sctp_xmit_hiwater = psctp->sctp_xmit_hiwater;
9730Sstevel@tonic-gate 		sctp->sctp_cwnd_max = psctp->sctp_cwnd_max;
9740Sstevel@tonic-gate 		sctp->sctp_rwnd = psctp->sctp_rwnd;
975852Svi117747 		sctp->sctp_irwnd = psctp->sctp_rwnd;
9763845Svi117747 		sctp->sctp_pd_point = psctp->sctp_pd_point;
9770Sstevel@tonic-gate 		sctp->sctp_rto_max = psctp->sctp_rto_max;
9780Sstevel@tonic-gate 		sctp->sctp_init_rto_max = psctp->sctp_init_rto_max;
9790Sstevel@tonic-gate 		sctp->sctp_rto_min = psctp->sctp_rto_min;
9800Sstevel@tonic-gate 		sctp->sctp_rto_initial = psctp->sctp_rto_initial;
9810Sstevel@tonic-gate 		sctp->sctp_pa_max_rxt = psctp->sctp_pa_max_rxt;
9820Sstevel@tonic-gate 		sctp->sctp_pp_max_rxt = psctp->sctp_pp_max_rxt;
9830Sstevel@tonic-gate 		sctp->sctp_max_init_rxt = psctp->sctp_max_init_rxt;
9840Sstevel@tonic-gate 
9850Sstevel@tonic-gate 		sctp->sctp_def_stream = psctp->sctp_def_stream;
9860Sstevel@tonic-gate 		sctp->sctp_def_flags = psctp->sctp_def_flags;
9870Sstevel@tonic-gate 		sctp->sctp_def_ppid = psctp->sctp_def_ppid;
9880Sstevel@tonic-gate 		sctp->sctp_def_context = psctp->sctp_def_context;
9890Sstevel@tonic-gate 		sctp->sctp_def_timetolive = psctp->sctp_def_timetolive;
9900Sstevel@tonic-gate 
9910Sstevel@tonic-gate 		sctp->sctp_num_istr = psctp->sctp_num_istr;
9920Sstevel@tonic-gate 		sctp->sctp_num_ostr = psctp->sctp_num_ostr;
9930Sstevel@tonic-gate 
9940Sstevel@tonic-gate 		sctp->sctp_hb_interval = psctp->sctp_hb_interval;
9950Sstevel@tonic-gate 		sctp->sctp_autoclose = psctp->sctp_autoclose;
9965586Skcpoon 		sctp->sctp_tx_adaptation_code = psctp->sctp_tx_adaptation_code;
9970Sstevel@tonic-gate 
9980Sstevel@tonic-gate 		/* xxx should be a better way to copy these flags xxx */
9990Sstevel@tonic-gate 		sctp->sctp_debug = psctp->sctp_debug;
10000Sstevel@tonic-gate 		sctp->sctp_bound_to_all = psctp->sctp_bound_to_all;
10010Sstevel@tonic-gate 		sctp->sctp_cansleep = psctp->sctp_cansleep;
10025586Skcpoon 		sctp->sctp_send_adaptation = psctp->sctp_send_adaptation;
10030Sstevel@tonic-gate 		sctp->sctp_ndelay = psctp->sctp_ndelay;
10040Sstevel@tonic-gate 		sctp->sctp_events = psctp->sctp_events;
10050Sstevel@tonic-gate 		sctp->sctp_ipv6_recvancillary = psctp->sctp_ipv6_recvancillary;
10062263Ssommerfe 
10072263Ssommerfe 		/* Copy IP-layer options */
10082263Ssommerfe 		connp = sctp->sctp_connp;
10092263Ssommerfe 		pconnp = psctp->sctp_connp;
10102263Ssommerfe 
10112263Ssommerfe 		connp->conn_broadcast = pconnp->conn_broadcast;
10122263Ssommerfe 		connp->conn_loopback = pconnp->conn_loopback;
10132263Ssommerfe 		connp->conn_dontroute = pconnp->conn_dontroute;
10142263Ssommerfe 		connp->conn_reuseaddr = pconnp->conn_reuseaddr;
10152263Ssommerfe 
10160Sstevel@tonic-gate 	} else {
10170Sstevel@tonic-gate 		/*
10180Sstevel@tonic-gate 		 * Initialize the header template
10190Sstevel@tonic-gate 		 */
10200Sstevel@tonic-gate 		if ((err = sctp_header_init_ipv4(sctp, sleep)) != 0) {
10214691Skcpoon 			goto failure;
10220Sstevel@tonic-gate 		}
10230Sstevel@tonic-gate 		if ((err = sctp_header_init_ipv6(sctp, sleep)) != 0) {
10244691Skcpoon 			goto failure;
10250Sstevel@tonic-gate 		}
10260Sstevel@tonic-gate 
10270Sstevel@tonic-gate 		/*
10280Sstevel@tonic-gate 		 * Set to system defaults
10290Sstevel@tonic-gate 		 */
10303448Sdh155122 		sctp->sctp_cookie_lifetime =
10313448Sdh155122 		    MSEC_TO_TICK(sctps->sctps_cookie_life);
10323448Sdh155122 		sctp->sctp_xmit_lowater = sctps->sctps_xmit_lowat;
10333448Sdh155122 		sctp->sctp_xmit_hiwater = sctps->sctps_xmit_hiwat;
10343448Sdh155122 		sctp->sctp_cwnd_max = sctps->sctps_cwnd_max_;
10353448Sdh155122 		sctp->sctp_rwnd = sctps->sctps_recv_hiwat;
1036852Svi117747 		sctp->sctp_irwnd = sctp->sctp_rwnd;
10373845Svi117747 		sctp->sctp_pd_point = sctp->sctp_rwnd;
10383448Sdh155122 		sctp->sctp_rto_max = MSEC_TO_TICK(sctps->sctps_rto_maxg);
10390Sstevel@tonic-gate 		sctp->sctp_init_rto_max = sctp->sctp_rto_max;
10403448Sdh155122 		sctp->sctp_rto_min = MSEC_TO_TICK(sctps->sctps_rto_ming);
10413448Sdh155122 		sctp->sctp_rto_initial = MSEC_TO_TICK(
10423448Sdh155122 		    sctps->sctps_rto_initialg);
10433448Sdh155122 		sctp->sctp_pa_max_rxt = sctps->sctps_pa_max_retr;
10443448Sdh155122 		sctp->sctp_pp_max_rxt = sctps->sctps_pp_max_retr;
10453448Sdh155122 		sctp->sctp_max_init_rxt = sctps->sctps_max_init_retr;
10460Sstevel@tonic-gate 
10473448Sdh155122 		sctp->sctp_num_istr = sctps->sctps_max_in_streams;
10483448Sdh155122 		sctp->sctp_num_ostr = sctps->sctps_initial_out_streams;
10490Sstevel@tonic-gate 
10503448Sdh155122 		sctp->sctp_hb_interval =
10513448Sdh155122 		    MSEC_TO_TICK(sctps->sctps_heartbeat_interval);
10520Sstevel@tonic-gate 	}
10530Sstevel@tonic-gate 	sctp->sctp_understands_asconf = B_TRUE;
10540Sstevel@tonic-gate 	sctp->sctp_understands_addip = B_TRUE;
10550Sstevel@tonic-gate 	sctp->sctp_prsctp_aware = B_FALSE;
10560Sstevel@tonic-gate 
10570Sstevel@tonic-gate 	sctp->sctp_connp->conn_ref = 1;
10580Sstevel@tonic-gate 	sctp->sctp_connp->conn_fully_bound = B_FALSE;
10590Sstevel@tonic-gate 
10600Sstevel@tonic-gate 	sctp->sctp_prsctpdrop = 0;
10610Sstevel@tonic-gate 	sctp->sctp_msgcount = 0;
10620Sstevel@tonic-gate 
10630Sstevel@tonic-gate 	return (0);
10644691Skcpoon 
10654691Skcpoon failure:
10664691Skcpoon 	if (sctp->sctp_iphc != NULL) {
10674691Skcpoon 		kmem_free(sctp->sctp_iphc, sctp->sctp_iphc_len);
10684691Skcpoon 		sctp->sctp_iphc = NULL;
10694691Skcpoon 	}
10704691Skcpoon 	if (sctp->sctp_iphc6 != NULL) {
10714691Skcpoon 		kmem_free(sctp->sctp_iphc6, sctp->sctp_iphc6_len);
10724691Skcpoon 		sctp->sctp_iphc6 = NULL;
10734691Skcpoon 	}
10744691Skcpoon 	return (err);
10750Sstevel@tonic-gate }
10760Sstevel@tonic-gate 
10770Sstevel@tonic-gate /*
10780Sstevel@tonic-gate  * Extracts the init tag from an INIT chunk and checks if it matches
10790Sstevel@tonic-gate  * the sctp's verification tag. Returns 0 if it doesn't match, 1 if
10800Sstevel@tonic-gate  * it does.
10810Sstevel@tonic-gate  */
10820Sstevel@tonic-gate static boolean_t
10830Sstevel@tonic-gate sctp_icmp_verf(sctp_t *sctp, sctp_hdr_t *sh, mblk_t *mp)
10840Sstevel@tonic-gate {
10850Sstevel@tonic-gate 	sctp_chunk_hdr_t *sch;
10860Sstevel@tonic-gate 	uint32_t verf, *vp;
10870Sstevel@tonic-gate 
10880Sstevel@tonic-gate 	sch = (sctp_chunk_hdr_t *)(sh + 1);
10890Sstevel@tonic-gate 	vp = (uint32_t *)(sch + 1);
10900Sstevel@tonic-gate 
10910Sstevel@tonic-gate 	/* Need at least the data chunk hdr and the first 4 bytes of INIT */
10920Sstevel@tonic-gate 	if ((unsigned char *)(vp + 1) > mp->b_wptr) {
10930Sstevel@tonic-gate 		return (B_FALSE);
10940Sstevel@tonic-gate 	}
10950Sstevel@tonic-gate 
10960Sstevel@tonic-gate 	bcopy(vp, &verf, sizeof (verf));
10970Sstevel@tonic-gate 
10980Sstevel@tonic-gate 	if (verf == sctp->sctp_lvtag) {
10990Sstevel@tonic-gate 		return (B_TRUE);
11000Sstevel@tonic-gate 	}
11010Sstevel@tonic-gate 	return (B_FALSE);
11020Sstevel@tonic-gate }
11030Sstevel@tonic-gate 
11040Sstevel@tonic-gate /*
11050Sstevel@tonic-gate  * sctp_icmp_error is called by sctp_input() to process ICMP error messages
11060Sstevel@tonic-gate  * passed up by IP.  The queue is the default queue.  We need to find a sctp_t
11070Sstevel@tonic-gate  * that corresponds to the returned datagram.  Passes the message back in on
11080Sstevel@tonic-gate  * the correct queue once it has located the connection.
11090Sstevel@tonic-gate  * Assumes that IP has pulled up everything up to and including
11100Sstevel@tonic-gate  * the ICMP header.
11110Sstevel@tonic-gate  */
11120Sstevel@tonic-gate void
11130Sstevel@tonic-gate sctp_icmp_error(sctp_t *sctp, mblk_t *mp)
11140Sstevel@tonic-gate {
11150Sstevel@tonic-gate 	icmph_t *icmph;
11160Sstevel@tonic-gate 	ipha_t	*ipha;
11170Sstevel@tonic-gate 	int	iph_hdr_length;
11180Sstevel@tonic-gate 	sctp_hdr_t *sctph;
11190Sstevel@tonic-gate 	mblk_t *first_mp;
11200Sstevel@tonic-gate 	uint32_t new_mtu;
11210Sstevel@tonic-gate 	in6_addr_t dst;
11220Sstevel@tonic-gate 	sctp_faddr_t *fp;
11233448Sdh155122 	sctp_stack_t	*sctps = sctp->sctp_sctps;
11240Sstevel@tonic-gate 
11251676Sjpk 	dprint(1, ("sctp_icmp_error: sctp=%p, mp=%p\n", (void *)sctp,
11261676Sjpk 	    (void *)mp));
11270Sstevel@tonic-gate 
11280Sstevel@tonic-gate 	first_mp = mp;
11290Sstevel@tonic-gate 
11300Sstevel@tonic-gate 	ipha = (ipha_t *)mp->b_rptr;
11310Sstevel@tonic-gate 	if (IPH_HDR_VERSION(ipha) != IPV4_VERSION) {
11320Sstevel@tonic-gate 		ASSERT(IPH_HDR_VERSION(ipha) == IPV6_VERSION);
11330Sstevel@tonic-gate 		sctp_icmp_error_ipv6(sctp, first_mp);
11340Sstevel@tonic-gate 		return;
11350Sstevel@tonic-gate 	}
11360Sstevel@tonic-gate 
1137*10828SGeorge.Shepherd@Sun.COM 	/* account for the ip hdr from the icmp message */
11380Sstevel@tonic-gate 	iph_hdr_length = IPH_HDR_LENGTH(ipha);
11390Sstevel@tonic-gate 	icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length];
1140*10828SGeorge.Shepherd@Sun.COM 	/* now the ip hdr of message resulting in this icmp */
11410Sstevel@tonic-gate 	ipha = (ipha_t *)&icmph[1];
11420Sstevel@tonic-gate 	iph_hdr_length = IPH_HDR_LENGTH(ipha);
11430Sstevel@tonic-gate 	sctph = (sctp_hdr_t *)((char *)ipha + iph_hdr_length);
1144*10828SGeorge.Shepherd@Sun.COM 	/* first_mp must expose the full sctp header. */
11450Sstevel@tonic-gate 	if ((uchar_t *)(sctph + 1) >= mp->b_wptr) {
11460Sstevel@tonic-gate 		/* not enough data for SCTP header */
11470Sstevel@tonic-gate 		freemsg(first_mp);
11480Sstevel@tonic-gate 		return;
11490Sstevel@tonic-gate 	}
11500Sstevel@tonic-gate 
11510Sstevel@tonic-gate 	switch (icmph->icmph_type) {
11520Sstevel@tonic-gate 	case ICMP_DEST_UNREACHABLE:
11530Sstevel@tonic-gate 		switch (icmph->icmph_code) {
11540Sstevel@tonic-gate 		case ICMP_FRAGMENTATION_NEEDED:
11550Sstevel@tonic-gate 			/*
11560Sstevel@tonic-gate 			 * Reduce the MSS based on the new MTU.  This will
11570Sstevel@tonic-gate 			 * eliminate any fragmentation locally.
11580Sstevel@tonic-gate 			 * N.B.  There may well be some funny side-effects on
11590Sstevel@tonic-gate 			 * the local send policy and the remote receive policy.
11600Sstevel@tonic-gate 			 * Pending further research, we provide
11610Sstevel@tonic-gate 			 * sctp_ignore_path_mtu just in case this proves
11620Sstevel@tonic-gate 			 * disastrous somewhere.
11630Sstevel@tonic-gate 			 *
11640Sstevel@tonic-gate 			 * After updating the MSS, retransmit part of the
11650Sstevel@tonic-gate 			 * dropped segment using the new mss by calling
11660Sstevel@tonic-gate 			 * sctp_wput_slow().  Need to adjust all those
11670Sstevel@tonic-gate 			 * params to make sure sctp_wput_slow() work properly.
11680Sstevel@tonic-gate 			 */
11693448Sdh155122 			if (sctps->sctps_ignore_path_mtu)
11700Sstevel@tonic-gate 				break;
11710Sstevel@tonic-gate 
11720Sstevel@tonic-gate 			/* find the offending faddr */
11730Sstevel@tonic-gate 			IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, &dst);
11740Sstevel@tonic-gate 			fp = sctp_lookup_faddr(sctp, &dst);
11750Sstevel@tonic-gate 			if (fp == NULL) {
11760Sstevel@tonic-gate 				break;
11770Sstevel@tonic-gate 			}
11780Sstevel@tonic-gate 
11790Sstevel@tonic-gate 			new_mtu = ntohs(icmph->icmph_du_mtu);
11800Sstevel@tonic-gate 
11810Sstevel@tonic-gate 			if (new_mtu - sctp->sctp_hdr_len >= fp->sfa_pmss)
11820Sstevel@tonic-gate 				break;
11830Sstevel@tonic-gate 
11840Sstevel@tonic-gate 			/*
11850Sstevel@tonic-gate 			 * Make sure that sfa_pmss is a multiple of
11860Sstevel@tonic-gate 			 * SCTP_ALIGN.
11870Sstevel@tonic-gate 			 */
11880Sstevel@tonic-gate 			fp->sfa_pmss = (new_mtu - sctp->sctp_hdr_len) &
11894505Skcpoon 			    ~(SCTP_ALIGN - 1);
11900Sstevel@tonic-gate 			fp->pmtu_discovered = 1;
1191*10828SGeorge.Shepherd@Sun.COM 			/*
1192*10828SGeorge.Shepherd@Sun.COM 			 * It is possible, even likely that a fast retransmit
1193*10828SGeorge.Shepherd@Sun.COM 			 * attempt has been dropped by ip as a result of this
1194*10828SGeorge.Shepherd@Sun.COM 			 * error, retransmission bundles as much as possible.
1195*10828SGeorge.Shepherd@Sun.COM 			 * A retransmit here prevents significant delays waiting
1196*10828SGeorge.Shepherd@Sun.COM 			 * on the timer. Analogous to behaviour of TCP after
1197*10828SGeorge.Shepherd@Sun.COM 			 * ICMP too big.
1198*10828SGeorge.Shepherd@Sun.COM 			 */
1199*10828SGeorge.Shepherd@Sun.COM 			sctp_rexmit(sctp, fp);
12000Sstevel@tonic-gate 			break;
12010Sstevel@tonic-gate 		case ICMP_PORT_UNREACHABLE:
12020Sstevel@tonic-gate 		case ICMP_PROTOCOL_UNREACHABLE:
12030Sstevel@tonic-gate 			switch (sctp->sctp_state) {
12040Sstevel@tonic-gate 			case SCTPS_COOKIE_WAIT:
12050Sstevel@tonic-gate 			case SCTPS_COOKIE_ECHOED:
12060Sstevel@tonic-gate 				/* make sure the verification tag matches */
12070Sstevel@tonic-gate 				if (!sctp_icmp_verf(sctp, sctph, mp)) {
12080Sstevel@tonic-gate 					break;
12090Sstevel@tonic-gate 				}
12103448Sdh155122 				BUMP_MIB(&sctps->sctps_mib, sctpAborted);
12113314Skcpoon 				sctp_assoc_event(sctp, SCTP_CANT_STR_ASSOC, 0,
12123314Skcpoon 				    NULL);
12130Sstevel@tonic-gate 				sctp_clean_death(sctp, ECONNREFUSED);
12140Sstevel@tonic-gate 				break;
12150Sstevel@tonic-gate 			}
12160Sstevel@tonic-gate 			break;
12170Sstevel@tonic-gate 		case ICMP_HOST_UNREACHABLE:
12180Sstevel@tonic-gate 		case ICMP_NET_UNREACHABLE:
12190Sstevel@tonic-gate 			/* Record the error in case we finally time out. */
12200Sstevel@tonic-gate 			sctp->sctp_client_errno = (icmph->icmph_code ==
12210Sstevel@tonic-gate 			    ICMP_HOST_UNREACHABLE) ? EHOSTUNREACH : ENETUNREACH;
12220Sstevel@tonic-gate 			break;
12230Sstevel@tonic-gate 		default:
12240Sstevel@tonic-gate 			break;
12250Sstevel@tonic-gate 		}
12260Sstevel@tonic-gate 		break;
12270Sstevel@tonic-gate 	case ICMP_SOURCE_QUENCH: {
12280Sstevel@tonic-gate 		/* Reduce the sending rate as if we got a retransmit timeout */
12290Sstevel@tonic-gate 		break;
12300Sstevel@tonic-gate 	}
12310Sstevel@tonic-gate 	}
12320Sstevel@tonic-gate 	freemsg(first_mp);
12330Sstevel@tonic-gate }
12340Sstevel@tonic-gate 
12350Sstevel@tonic-gate /*
12360Sstevel@tonic-gate  * sctp_icmp_error_ipv6() is called by sctp_icmp_error() to process ICMPv6
12370Sstevel@tonic-gate  * error messages passed up by IP.
12380Sstevel@tonic-gate  * Assumes that IP has pulled up all the extension headers as well
12390Sstevel@tonic-gate  * as the ICMPv6 header.
12400Sstevel@tonic-gate  */
12410Sstevel@tonic-gate static void
12420Sstevel@tonic-gate sctp_icmp_error_ipv6(sctp_t *sctp, mblk_t *mp)
12430Sstevel@tonic-gate {
12440Sstevel@tonic-gate 	icmp6_t *icmp6;
12450Sstevel@tonic-gate 	ip6_t	*ip6h;
12460Sstevel@tonic-gate 	uint16_t	iph_hdr_length;
12470Sstevel@tonic-gate 	sctp_hdr_t *sctpha;
12480Sstevel@tonic-gate 	uint8_t	*nexthdrp;
12490Sstevel@tonic-gate 	uint32_t new_mtu;
12500Sstevel@tonic-gate 	sctp_faddr_t *fp;
12513448Sdh155122 	sctp_stack_t	*sctps = sctp->sctp_sctps;
12520Sstevel@tonic-gate 
12530Sstevel@tonic-gate 	ip6h = (ip6_t *)mp->b_rptr;
12540Sstevel@tonic-gate 	iph_hdr_length = (ip6h->ip6_nxt != IPPROTO_SCTP) ?
12550Sstevel@tonic-gate 	    ip_hdr_length_v6(mp, ip6h) : IPV6_HDR_LEN;
12560Sstevel@tonic-gate 
12570Sstevel@tonic-gate 	icmp6 = (icmp6_t *)&mp->b_rptr[iph_hdr_length];
12580Sstevel@tonic-gate 	ip6h = (ip6_t *)&icmp6[1];
12590Sstevel@tonic-gate 	if (!ip_hdr_length_nexthdr_v6(mp, ip6h, &iph_hdr_length, &nexthdrp)) {
12600Sstevel@tonic-gate 		freemsg(mp);
12610Sstevel@tonic-gate 		return;
12620Sstevel@tonic-gate 	}
12630Sstevel@tonic-gate 	ASSERT(*nexthdrp == IPPROTO_SCTP);
12640Sstevel@tonic-gate 
12650Sstevel@tonic-gate 	/* XXX need ifindex to find connection */
12660Sstevel@tonic-gate 	sctpha = (sctp_hdr_t *)((char *)ip6h + iph_hdr_length);
12670Sstevel@tonic-gate 	if ((uchar_t *)sctpha >= mp->b_wptr) {
12680Sstevel@tonic-gate 		/* not enough data for SCTP header */
12690Sstevel@tonic-gate 		freemsg(mp);
12700Sstevel@tonic-gate 		return;
12710Sstevel@tonic-gate 	}
12720Sstevel@tonic-gate 	switch (icmp6->icmp6_type) {
12730Sstevel@tonic-gate 	case ICMP6_PACKET_TOO_BIG:
12740Sstevel@tonic-gate 		/*
12750Sstevel@tonic-gate 		 * Reduce the MSS based on the new MTU.  This will
12760Sstevel@tonic-gate 		 * eliminate any fragmentation locally.
12770Sstevel@tonic-gate 		 * N.B.  There may well be some funny side-effects on
12780Sstevel@tonic-gate 		 * the local send policy and the remote receive policy.
12790Sstevel@tonic-gate 		 * Pending further research, we provide
12800Sstevel@tonic-gate 		 * sctp_ignore_path_mtu just in case this proves
12810Sstevel@tonic-gate 		 * disastrous somewhere.
12820Sstevel@tonic-gate 		 *
12830Sstevel@tonic-gate 		 * After updating the MSS, retransmit part of the
12840Sstevel@tonic-gate 		 * dropped segment using the new mss by calling
12850Sstevel@tonic-gate 		 * sctp_wput_slow().  Need to adjust all those
12860Sstevel@tonic-gate 		 * params to make sure sctp_wput_slow() work properly.
12870Sstevel@tonic-gate 		 */
12883448Sdh155122 		if (sctps->sctps_ignore_path_mtu)
12890Sstevel@tonic-gate 			break;
12900Sstevel@tonic-gate 
12910Sstevel@tonic-gate 		/* find the offending faddr */
12920Sstevel@tonic-gate 		fp = sctp_lookup_faddr(sctp, &ip6h->ip6_dst);
12930Sstevel@tonic-gate 		if (fp == NULL) {
12940Sstevel@tonic-gate 			break;
12950Sstevel@tonic-gate 		}
12960Sstevel@tonic-gate 
12970Sstevel@tonic-gate 		new_mtu = ntohs(icmp6->icmp6_mtu);
12980Sstevel@tonic-gate 
12990Sstevel@tonic-gate 		if (new_mtu - sctp->sctp_hdr6_len >= fp->sfa_pmss)
13000Sstevel@tonic-gate 			break;
13010Sstevel@tonic-gate 
13020Sstevel@tonic-gate 		/* Make sure that sfa_pmss is a multiple of SCTP_ALIGN. */
13030Sstevel@tonic-gate 		fp->sfa_pmss = (new_mtu - sctp->sctp_hdr6_len) &
13044505Skcpoon 		    ~(SCTP_ALIGN - 1);
13050Sstevel@tonic-gate 		fp->pmtu_discovered = 1;
13060Sstevel@tonic-gate 
13070Sstevel@tonic-gate 		break;
13080Sstevel@tonic-gate 
13090Sstevel@tonic-gate 	case ICMP6_DST_UNREACH:
13100Sstevel@tonic-gate 		switch (icmp6->icmp6_code) {
13110Sstevel@tonic-gate 		case ICMP6_DST_UNREACH_NOPORT:
13120Sstevel@tonic-gate 			/* make sure the verification tag matches */
13130Sstevel@tonic-gate 			if (!sctp_icmp_verf(sctp, sctpha, mp)) {
13140Sstevel@tonic-gate 				break;
13150Sstevel@tonic-gate 			}
13160Sstevel@tonic-gate 			if (sctp->sctp_state == SCTPS_COOKIE_WAIT ||
13170Sstevel@tonic-gate 			    sctp->sctp_state == SCTPS_COOKIE_ECHOED) {
13183448Sdh155122 				BUMP_MIB(&sctps->sctps_mib, sctpAborted);
13193314Skcpoon 				sctp_assoc_event(sctp, SCTP_CANT_STR_ASSOC, 0,
13203314Skcpoon 				    NULL);
13210Sstevel@tonic-gate 				sctp_clean_death(sctp, ECONNREFUSED);
13220Sstevel@tonic-gate 			}
13230Sstevel@tonic-gate 			break;
13240Sstevel@tonic-gate 
13250Sstevel@tonic-gate 		case ICMP6_DST_UNREACH_ADMIN:
13260Sstevel@tonic-gate 		case ICMP6_DST_UNREACH_NOROUTE:
13270Sstevel@tonic-gate 		case ICMP6_DST_UNREACH_NOTNEIGHBOR:
13280Sstevel@tonic-gate 		case ICMP6_DST_UNREACH_ADDR:
13290Sstevel@tonic-gate 			/* Record the error in case we finally time out. */
13300Sstevel@tonic-gate 			sctp->sctp_client_errno = EHOSTUNREACH;
13310Sstevel@tonic-gate 			break;
13320Sstevel@tonic-gate 		default:
13330Sstevel@tonic-gate 			break;
13340Sstevel@tonic-gate 		}
13350Sstevel@tonic-gate 		break;
13360Sstevel@tonic-gate 
13370Sstevel@tonic-gate 	case ICMP6_PARAM_PROB:
13380Sstevel@tonic-gate 		/* If this corresponds to an ICMP_PROTOCOL_UNREACHABLE */
13390Sstevel@tonic-gate 		if (icmp6->icmp6_code == ICMP6_PARAMPROB_NEXTHEADER &&
13400Sstevel@tonic-gate 		    (uchar_t *)ip6h + icmp6->icmp6_pptr ==
13410Sstevel@tonic-gate 		    (uchar_t *)nexthdrp) {
13420Sstevel@tonic-gate 			/* make sure the verification tag matches */
13430Sstevel@tonic-gate 			if (!sctp_icmp_verf(sctp, sctpha, mp)) {
13440Sstevel@tonic-gate 				break;
13450Sstevel@tonic-gate 			}
13460Sstevel@tonic-gate 			if (sctp->sctp_state == SCTPS_COOKIE_WAIT) {
13473448Sdh155122 				BUMP_MIB(&sctps->sctps_mib, sctpAborted);
13483314Skcpoon 				sctp_assoc_event(sctp, SCTP_CANT_STR_ASSOC, 0,
13493314Skcpoon 				    NULL);
13500Sstevel@tonic-gate 				sctp_clean_death(sctp, ECONNREFUSED);
13510Sstevel@tonic-gate 			}
13520Sstevel@tonic-gate 			break;
13530Sstevel@tonic-gate 		}
13540Sstevel@tonic-gate 		break;
13550Sstevel@tonic-gate 
13560Sstevel@tonic-gate 	case ICMP6_TIME_EXCEEDED:
13570Sstevel@tonic-gate 	default:
13580Sstevel@tonic-gate 		break;
13590Sstevel@tonic-gate 	}
13600Sstevel@tonic-gate 	freemsg(mp);
13610Sstevel@tonic-gate }
13620Sstevel@tonic-gate 
13630Sstevel@tonic-gate /*
13640Sstevel@tonic-gate  * Called by sockfs to create a new sctp instance.
13650Sstevel@tonic-gate  *
13660Sstevel@tonic-gate  * If parent pointer is passed in, inherit settings from it.
13670Sstevel@tonic-gate  */
13680Sstevel@tonic-gate sctp_t *
13698348SEric.Yu@Sun.COM sctp_create(void *ulpd, sctp_t *parent, int family, int flags,
13708348SEric.Yu@Sun.COM     sock_upcalls_t *upcalls, sctp_sockbuf_limits_t *sbl,
13710Sstevel@tonic-gate     cred_t *credp)
13720Sstevel@tonic-gate {
13730Sstevel@tonic-gate 	sctp_t		*sctp, *psctp;
13740Sstevel@tonic-gate 	conn_t		*sctp_connp;
13750Sstevel@tonic-gate 	mblk_t		*ack_mp, *hb_mp;
13760Sstevel@tonic-gate 	int		sleep = flags & SCTP_CAN_BLOCK ? KM_SLEEP : KM_NOSLEEP;
13773448Sdh155122 	zoneid_t	zoneid;
13783448Sdh155122 	sctp_stack_t	*sctps;
13790Sstevel@tonic-gate 
13800Sstevel@tonic-gate 	/* User must supply a credential. */
13810Sstevel@tonic-gate 	if (credp == NULL)
13820Sstevel@tonic-gate 		return (NULL);
13830Sstevel@tonic-gate 
13843448Sdh155122 	psctp = (sctp_t *)parent;
13853448Sdh155122 	if (psctp != NULL) {
13863448Sdh155122 		sctps = psctp->sctp_sctps;
13873448Sdh155122 		/* Increase here to have common decrease at end */
13883448Sdh155122 		netstack_hold(sctps->sctps_netstack);
13893448Sdh155122 	} else {
13903448Sdh155122 		netstack_t *ns;
13913448Sdh155122 
13923448Sdh155122 		ns = netstack_find_by_cred(credp);
13933448Sdh155122 		ASSERT(ns != NULL);
13943448Sdh155122 		sctps = ns->netstack_sctp;
13953448Sdh155122 		ASSERT(sctps != NULL);
13963448Sdh155122 
13973448Sdh155122 		/*
13983448Sdh155122 		 * For exclusive stacks we set the zoneid to zero
13993448Sdh155122 		 * to make SCTP operate as if in the global zone.
14003448Sdh155122 		 */
14013448Sdh155122 		if (sctps->sctps_netstack->netstack_stackid !=
14023448Sdh155122 		    GLOBAL_NETSTACKID)
14033448Sdh155122 			zoneid = GLOBAL_ZONEID;
14043448Sdh155122 		else
14053448Sdh155122 			zoneid = crgetzoneid(credp);
14063448Sdh155122 
14073448Sdh155122 		/*
14083448Sdh155122 		 * For stackid zero this is done from strplumb.c, but
14093448Sdh155122 		 * non-zero stackids are handled here.
14103448Sdh155122 		 */
14113448Sdh155122 		if (sctps->sctps_g_q == NULL &&
14123448Sdh155122 		    sctps->sctps_netstack->netstack_stackid !=
14133448Sdh155122 		    GLOBAL_NETSTACKID) {
14143448Sdh155122 			sctp_g_q_setup(sctps);
14153448Sdh155122 		}
14163448Sdh155122 	}
14173448Sdh155122 	if ((sctp_connp = ipcl_conn_create(IPCL_SCTPCONN, sleep,
14184505Skcpoon 	    sctps->sctps_netstack)) == NULL) {
14193448Sdh155122 		netstack_rele(sctps->sctps_netstack);
14203448Sdh155122 		SCTP_KSTAT(sctps, sctp_conn_create);
14210Sstevel@tonic-gate 		return (NULL);
14221735Skcpoon 	}
14233448Sdh155122 	/*
14243448Sdh155122 	 * ipcl_conn_create did a netstack_hold. Undo the hold that was
14253448Sdh155122 	 * done at top of sctp_create.
14263448Sdh155122 	 */
14273448Sdh155122 	netstack_rele(sctps->sctps_netstack);
14283448Sdh155122 	sctp = CONN2SCTP(sctp_connp);
14293448Sdh155122 	sctp->sctp_sctps = sctps;
14301676Sjpk 
14313448Sdh155122 	sctp_connp->conn_ulp_labeled = is_system_labeled();
14324691Skcpoon 	if ((ack_mp = sctp_timer_alloc(sctp, sctp_ack_timer, sleep)) == NULL ||
14334691Skcpoon 	    (hb_mp = sctp_timer_alloc(sctp, sctp_heartbeat_timer,
14344691Skcpoon 	    sleep)) == NULL) {
14350Sstevel@tonic-gate 		if (ack_mp != NULL)
14360Sstevel@tonic-gate 			freeb(ack_mp);
14374691Skcpoon 		sctp_conn_clear(sctp_connp);
14383448Sdh155122 		sctp->sctp_sctps = NULL;
14393448Sdh155122 		SCTP_G_Q_REFRELE(sctps);
14400Sstevel@tonic-gate 		kmem_cache_free(sctp_conn_cache, sctp_connp);
14410Sstevel@tonic-gate 		return (NULL);
14420Sstevel@tonic-gate 	}
14430Sstevel@tonic-gate 
14440Sstevel@tonic-gate 	sctp->sctp_ack_mp = ack_mp;
14450Sstevel@tonic-gate 	sctp->sctp_heartbeat_mp = hb_mp;
14460Sstevel@tonic-gate 
14470Sstevel@tonic-gate 	switch (family) {
14480Sstevel@tonic-gate 	case AF_INET6:
14490Sstevel@tonic-gate 		sctp_connp->conn_af_isv6 = B_TRUE;
14500Sstevel@tonic-gate 		sctp->sctp_ipversion = IPV6_VERSION;
14510Sstevel@tonic-gate 		sctp->sctp_family = AF_INET6;
14520Sstevel@tonic-gate 		break;
14530Sstevel@tonic-gate 
14540Sstevel@tonic-gate 	case AF_INET:
14550Sstevel@tonic-gate 		sctp_connp->conn_af_isv6 = B_FALSE;
14560Sstevel@tonic-gate 		sctp_connp->conn_pkt_isv6 = B_FALSE;
14570Sstevel@tonic-gate 		sctp->sctp_ipversion = IPV4_VERSION;
14580Sstevel@tonic-gate 		sctp->sctp_family = AF_INET;
14590Sstevel@tonic-gate 		break;
14600Sstevel@tonic-gate 	default:
14610Sstevel@tonic-gate 		ASSERT(0);
14620Sstevel@tonic-gate 		break;
14630Sstevel@tonic-gate 	}
14640Sstevel@tonic-gate 	if (sctp_init_values(sctp, psctp, sleep) != 0) {
14650Sstevel@tonic-gate 		freeb(ack_mp);
14660Sstevel@tonic-gate 		freeb(hb_mp);
14674691Skcpoon 		sctp_conn_clear(sctp_connp);
14683448Sdh155122 		sctp->sctp_sctps = NULL;
14693448Sdh155122 		SCTP_G_Q_REFRELE(sctps);
14700Sstevel@tonic-gate 		kmem_cache_free(sctp_conn_cache, sctp_connp);
14710Sstevel@tonic-gate 		return (NULL);
14720Sstevel@tonic-gate 	}
14730Sstevel@tonic-gate 	sctp->sctp_cansleep = ((flags & SCTP_CAN_BLOCK) == SCTP_CAN_BLOCK);
14740Sstevel@tonic-gate 
14753448Sdh155122 	sctp->sctp_mss = sctps->sctps_initial_mtu - ((family == AF_INET6) ?
14764505Skcpoon 	    sctp->sctp_hdr6_len : sctp->sctp_hdr_len);
14770Sstevel@tonic-gate 
14780Sstevel@tonic-gate 	if (psctp != NULL) {
14790Sstevel@tonic-gate 		RUN_SCTP(psctp);
14800Sstevel@tonic-gate 		/*
14810Sstevel@tonic-gate 		 * Inherit local address list, local port. Parent is either
14820Sstevel@tonic-gate 		 * in SCTPS_BOUND, or SCTPS_LISTEN state.
14830Sstevel@tonic-gate 		 */
14840Sstevel@tonic-gate 		ASSERT((psctp->sctp_state == SCTPS_BOUND) ||
14850Sstevel@tonic-gate 		    (psctp->sctp_state == SCTPS_LISTEN));
14860Sstevel@tonic-gate 		if (sctp_dup_saddrs(psctp, sctp, sleep)) {
14870Sstevel@tonic-gate 			WAKE_SCTP(psctp);
14880Sstevel@tonic-gate 			freeb(ack_mp);
14890Sstevel@tonic-gate 			freeb(hb_mp);
14900Sstevel@tonic-gate 			sctp_headers_free(sctp);
14914691Skcpoon 			sctp_conn_clear(sctp_connp);
14923448Sdh155122 			sctp->sctp_sctps = NULL;
14933448Sdh155122 			SCTP_G_Q_REFRELE(sctps);
14940Sstevel@tonic-gate 			kmem_cache_free(sctp_conn_cache, sctp_connp);
14950Sstevel@tonic-gate 			return (NULL);
14960Sstevel@tonic-gate 		}
14970Sstevel@tonic-gate 
14980Sstevel@tonic-gate 		/*
14990Sstevel@tonic-gate 		 * If the parent is specified, it'll be immediatelly
15000Sstevel@tonic-gate 		 * followed by sctp_connect(). So don't add this guy to
15010Sstevel@tonic-gate 		 * bind hash.
15020Sstevel@tonic-gate 		 */
15030Sstevel@tonic-gate 		sctp->sctp_lport = psctp->sctp_lport;
15040Sstevel@tonic-gate 		sctp->sctp_state = SCTPS_BOUND;
15052263Ssommerfe 		sctp->sctp_allzones = psctp->sctp_allzones;
15060Sstevel@tonic-gate 		sctp->sctp_zoneid = psctp->sctp_zoneid;
15070Sstevel@tonic-gate 		WAKE_SCTP(psctp);
15080Sstevel@tonic-gate 	} else {
15093448Sdh155122 		sctp->sctp_zoneid = zoneid;
15100Sstevel@tonic-gate 	}
15110Sstevel@tonic-gate 
15128778SErik.Nordmark@Sun.COM 	sctp->sctp_cpid = curproc->p_pid;
15138778SErik.Nordmark@Sun.COM 	sctp->sctp_open_time = lbolt64;
15148778SErik.Nordmark@Sun.COM 
15159710SKen.Powell@Sun.COM 	ASSERT(sctp_connp->conn_cred == NULL);
15160Sstevel@tonic-gate 	sctp_connp->conn_cred = credp;
15170Sstevel@tonic-gate 	crhold(credp);
15180Sstevel@tonic-gate 
15191676Sjpk 	/*
15201676Sjpk 	 * If the caller has the process-wide flag set, then default to MAC
15211676Sjpk 	 * exempt mode.  This allows read-down to unlabeled hosts.
15221676Sjpk 	 */
15231676Sjpk 	if (getpflags(NET_MAC_AWARE, credp) != 0)
15241676Sjpk 		sctp_connp->conn_mac_exempt = B_TRUE;
15251676Sjpk 
15260Sstevel@tonic-gate 	/* Initialize SCTP instance values,  our verf tag must never be 0 */
15270Sstevel@tonic-gate 	(void) random_get_pseudo_bytes((uint8_t *)&sctp->sctp_lvtag,
15280Sstevel@tonic-gate 	    sizeof (sctp->sctp_lvtag));
15290Sstevel@tonic-gate 	if (sctp->sctp_lvtag == 0)
15300Sstevel@tonic-gate 		sctp->sctp_lvtag = (uint32_t)gethrtime();
15310Sstevel@tonic-gate 	ASSERT(sctp->sctp_lvtag != 0);
15320Sstevel@tonic-gate 
15330Sstevel@tonic-gate 	sctp->sctp_ltsn = sctp->sctp_lvtag + 1;
15340Sstevel@tonic-gate 	sctp->sctp_lcsn = sctp->sctp_ltsn;
15350Sstevel@tonic-gate 	sctp->sctp_recovery_tsn = sctp->sctp_lastack_rxd = sctp->sctp_ltsn - 1;
15360Sstevel@tonic-gate 	sctp->sctp_adv_pap = sctp->sctp_lastack_rxd;
15370Sstevel@tonic-gate 
15380Sstevel@tonic-gate 	/* Information required by upper layer */
15398348SEric.Yu@Sun.COM 	if (ulpd != NULL) {
15408348SEric.Yu@Sun.COM 		sctp->sctp_ulpd = ulpd;
15410Sstevel@tonic-gate 
15428348SEric.Yu@Sun.COM 		ASSERT(upcalls != NULL);
15438348SEric.Yu@Sun.COM 		sctp->sctp_upcalls = upcalls;
15440Sstevel@tonic-gate 		ASSERT(sbl != NULL);
15450Sstevel@tonic-gate 		/* Fill in the socket buffer limits for sctpsockfs */
15460Sstevel@tonic-gate 		sbl->sbl_txlowat = sctp->sctp_xmit_lowater;
15470Sstevel@tonic-gate 		sbl->sbl_txbuf = sctp->sctp_xmit_hiwater;
15480Sstevel@tonic-gate 		sbl->sbl_rxbuf = sctp->sctp_rwnd;
15490Sstevel@tonic-gate 		sbl->sbl_rxlowat = SCTP_RECV_LOWATER;
15500Sstevel@tonic-gate 	}
15518348SEric.Yu@Sun.COM 	/* If no ulpd, must be creating the default sctp */
15528348SEric.Yu@Sun.COM 	ASSERT(ulpd != NULL || sctps->sctps_gsctp == NULL);
15530Sstevel@tonic-gate 
15540Sstevel@tonic-gate 	/* Insert this in the global list. */
15553448Sdh155122 	SCTP_LINK(sctp, sctps);
15560Sstevel@tonic-gate 
15570Sstevel@tonic-gate 	return (sctp);
15580Sstevel@tonic-gate }
15590Sstevel@tonic-gate 
15603448Sdh155122 /*
15613448Sdh155122  * Make sure we wait until the default queue is setup, yet allow
15623448Sdh155122  * sctp_g_q_create() to open a SCTP stream.
15633448Sdh155122  * We need to allow sctp_g_q_create() do do an open
15643448Sdh155122  * of sctp, hence we compare curhread.
15653448Sdh155122  * All others have to wait until the sctps_g_q has been
15663448Sdh155122  * setup.
15673448Sdh155122  */
15680Sstevel@tonic-gate void
15693448Sdh155122 sctp_g_q_setup(sctp_stack_t *sctps)
15703448Sdh155122 {
15713448Sdh155122 	mutex_enter(&sctps->sctps_g_q_lock);
15723448Sdh155122 	if (sctps->sctps_g_q != NULL) {
15733448Sdh155122 		mutex_exit(&sctps->sctps_g_q_lock);
15743448Sdh155122 		return;
15753448Sdh155122 	}
15763448Sdh155122 	if (sctps->sctps_g_q_creator == NULL) {
15773448Sdh155122 		/* This thread will set it up */
15783448Sdh155122 		sctps->sctps_g_q_creator = curthread;
15793448Sdh155122 		mutex_exit(&sctps->sctps_g_q_lock);
15803448Sdh155122 		sctp_g_q_create(sctps);
15813448Sdh155122 		mutex_enter(&sctps->sctps_g_q_lock);
15823448Sdh155122 		ASSERT(sctps->sctps_g_q_creator == curthread);
15833448Sdh155122 		sctps->sctps_g_q_creator = NULL;
15843448Sdh155122 		cv_signal(&sctps->sctps_g_q_cv);
15853448Sdh155122 		ASSERT(sctps->sctps_g_q != NULL);
15863448Sdh155122 		mutex_exit(&sctps->sctps_g_q_lock);
15873448Sdh155122 		return;
15883448Sdh155122 	}
15893448Sdh155122 	/* Everybody but the creator has to wait */
15903448Sdh155122 	if (sctps->sctps_g_q_creator != curthread) {
15913448Sdh155122 		while (sctps->sctps_g_q == NULL)
15923448Sdh155122 			cv_wait(&sctps->sctps_g_q_cv, &sctps->sctps_g_q_lock);
15933448Sdh155122 	}
15943448Sdh155122 	mutex_exit(&sctps->sctps_g_q_lock);
15953448Sdh155122 }
15963448Sdh155122 
15973448Sdh155122 #define	IP	"ip"
15983448Sdh155122 
15993448Sdh155122 #define	SCTP6DEV		"/devices/pseudo/sctp6@0:sctp6"
16003448Sdh155122 
16013448Sdh155122 /*
16023448Sdh155122  * Create a default sctp queue here instead of in strplumb
16033448Sdh155122  */
16043448Sdh155122 void
16053448Sdh155122 sctp_g_q_create(sctp_stack_t *sctps)
16060Sstevel@tonic-gate {
16073448Sdh155122 	int error;
16083448Sdh155122 	ldi_handle_t	lh = NULL;
16093448Sdh155122 	ldi_ident_t	li = NULL;
16103448Sdh155122 	int		rval;
16113448Sdh155122 	cred_t		*cr;
16125240Snordmark 	major_t IP_MAJ;
16133448Sdh155122 
16143448Sdh155122 #ifdef NS_DEBUG
16153448Sdh155122 	(void) printf("sctp_g_q_create()for stack %d\n",
16163448Sdh155122 	    sctps->sctps_netstack->netstack_stackid);
16173448Sdh155122 #endif
16183448Sdh155122 
16195240Snordmark 	IP_MAJ = ddi_name_to_major(IP);
16205240Snordmark 
16213448Sdh155122 	ASSERT(sctps->sctps_g_q_creator == curthread);
16223448Sdh155122 
16233448Sdh155122 	error = ldi_ident_from_major(IP_MAJ, &li);
16243448Sdh155122 	if (error) {
16253448Sdh155122 #ifdef DEBUG
16263448Sdh155122 		printf("sctp_g_q_create: lyr ident get failed error %d\n",
16273448Sdh155122 		    error);
16283448Sdh155122 #endif
16293448Sdh155122 		return;
16303448Sdh155122 	}
16313448Sdh155122 
16323448Sdh155122 	cr = zone_get_kcred(netstackid_to_zoneid(
16334505Skcpoon 	    sctps->sctps_netstack->netstack_stackid));
16343448Sdh155122 	ASSERT(cr != NULL);
16353448Sdh155122 	/*
16363448Sdh155122 	 * We set the sctp default queue to IPv6 because IPv4 falls
16373448Sdh155122 	 * back to IPv6 when it can't find a client, but
16383448Sdh155122 	 * IPv6 does not fall back to IPv4.
16393448Sdh155122 	 */
16403448Sdh155122 	error = ldi_open_by_name(SCTP6DEV, FREAD|FWRITE, cr, &lh, li);
16413448Sdh155122 	if (error) {
16423448Sdh155122 #ifdef DEBUG
16433448Sdh155122 		printf("sctp_g_q_create: open of SCTP6DEV failed error %d\n",
16443448Sdh155122 		    error);
16453448Sdh155122 #endif
16463448Sdh155122 		goto out;
16473448Sdh155122 	}
16483448Sdh155122 
16493448Sdh155122 	/*
16503448Sdh155122 	 * This ioctl causes the sctp framework to cache a pointer to
16513448Sdh155122 	 * this stream, so we don't want to close the stream after
16523448Sdh155122 	 * this operation.
16533448Sdh155122 	 * Use the kernel credentials that are for the zone we're in.
16543448Sdh155122 	 */
16553448Sdh155122 	error = ldi_ioctl(lh, SCTP_IOC_DEFAULT_Q,
16563448Sdh155122 	    (intptr_t)0, FKIOCTL, cr, &rval);
16573448Sdh155122 	if (error) {
16583448Sdh155122 #ifdef DEBUG
16593448Sdh155122 		printf("sctp_g_q_create: ioctl SCTP_IOC_DEFAULT_Q failed "
16603448Sdh155122 		    "error %d\n", error);
16613448Sdh155122 #endif
16623448Sdh155122 		goto out;
16633448Sdh155122 	}
16643448Sdh155122 	sctps->sctps_g_q_lh = lh;	/* For sctp_g_q_inactive */
16653448Sdh155122 	lh = NULL;
16663448Sdh155122 out:
16673448Sdh155122 	/* Close layered handles */
16683448Sdh155122 	if (li)
16693448Sdh155122 		ldi_ident_release(li);
16703448Sdh155122 	/* Keep cred around until _inactive needs it */
16713448Sdh155122 	sctps->sctps_g_q_cr = cr;
16723448Sdh155122 }
16730Sstevel@tonic-gate 
16743448Sdh155122 /*
16753448Sdh155122  * Remove the sctp_default queue so that new connections will not find it.
16763448Sdh155122  * SCTP uses sctp_g_q for all transmission, so all sctp'ts implicitly
16773448Sdh155122  * refer to it. Hence have each one have a reference on sctp_g_q_ref!
16783448Sdh155122  *
16793448Sdh155122  * We decrement the refcnt added in sctp_g_q_create. Once all the
16803448Sdh155122  * sctp_t's which use the default go away, sctp_g_q_close will be called
16813448Sdh155122  * and close the sctp_g_q. Once sctp_g_q is closed, sctp_close() will drop the
16823448Sdh155122  * last reference count on the stack by calling netstack_rele().
16833448Sdh155122  */
16843448Sdh155122 void
16853448Sdh155122 sctp_g_q_destroy(sctp_stack_t *sctps)
16863448Sdh155122 {
16873448Sdh155122 	if (sctps->sctps_g_q == NULL) {
16883448Sdh155122 		return;	/* Nothing to cleanup */
16893448Sdh155122 	}
16903448Sdh155122 	/*
16913448Sdh155122 	 * Keep sctps_g_q and sctps_gsctp until the last reference has
16923448Sdh155122 	 * dropped, since the output is always done using those.
16933448Sdh155122 	 * Need to decrement twice to take sctp_g_q_create and
16943448Sdh155122 	 * the gsctp reference into account so that sctp_g_q_inactive is called
16953448Sdh155122 	 * when all but the default queue remains.
16963448Sdh155122 	 */
16973448Sdh155122 #ifdef NS_DEBUG
16983448Sdh155122 	(void) printf("sctp_g_q_destroy: ref %d\n",
16993448Sdh155122 	    sctps->sctps_g_q_ref);
17003448Sdh155122 #endif
17013448Sdh155122 	SCTP_G_Q_REFRELE(sctps);
17023448Sdh155122 }
17033448Sdh155122 
17043448Sdh155122 /*
17053448Sdh155122  * Called when last user (could be sctp_g_q_destroy) drops reference count
17063448Sdh155122  * using SCTP_G_Q_REFRELE.
17073448Sdh155122  * Run by sctp_q_q_inactive using a taskq.
17083448Sdh155122  */
17093448Sdh155122 static void
17103448Sdh155122 sctp_g_q_close(void *arg)
17113448Sdh155122 {
17123448Sdh155122 	sctp_stack_t *sctps = arg;
17133448Sdh155122 	int error;
17143448Sdh155122 	ldi_handle_t	lh = NULL;
17153448Sdh155122 	ldi_ident_t	li = NULL;
17163448Sdh155122 	cred_t		*cr;
17175240Snordmark 	major_t IP_MAJ;
17185240Snordmark 
17195240Snordmark 	IP_MAJ = ddi_name_to_major(IP);
17203448Sdh155122 
17213448Sdh155122 	lh = sctps->sctps_g_q_lh;
17223448Sdh155122 	if (lh == NULL)
17233448Sdh155122 		return;	/* Nothing to cleanup */
17243448Sdh155122 
17253448Sdh155122 	error = ldi_ident_from_major(IP_MAJ, &li);
17263448Sdh155122 	if (error) {
17273448Sdh155122 #ifdef NS_DEBUG
17283448Sdh155122 		printf("sctp_g_q_inactive: lyr ident get failed error %d\n",
17293448Sdh155122 		    error);
17303448Sdh155122 #endif
17313448Sdh155122 		return;
17323448Sdh155122 	}
17330Sstevel@tonic-gate 
17343448Sdh155122 	cr = sctps->sctps_g_q_cr;
17353448Sdh155122 	sctps->sctps_g_q_cr = NULL;
17363448Sdh155122 	ASSERT(cr != NULL);
17373448Sdh155122 
17383448Sdh155122 	/*
17393448Sdh155122 	 * Make sure we can break the recursion when sctp_close decrements
17403448Sdh155122 	 * the reference count causing g_q_inactive to be called again.
17413448Sdh155122 	 */
17423448Sdh155122 	sctps->sctps_g_q_lh = NULL;
17433448Sdh155122 
17443448Sdh155122 	/* close the default queue */
17453448Sdh155122 	(void) ldi_close(lh, FREAD|FWRITE, cr);
17463448Sdh155122 
17473448Sdh155122 	/* Close layered handles */
17483448Sdh155122 	ldi_ident_release(li);
17493448Sdh155122 	crfree(cr);
17503448Sdh155122 
17513448Sdh155122 	ASSERT(sctps->sctps_g_q != NULL);
17523448Sdh155122 	sctps->sctps_g_q = NULL;
17533448Sdh155122 	/*
17543448Sdh155122 	 * Now free sctps_gsctp.
17553448Sdh155122 	 */
17563448Sdh155122 	ASSERT(sctps->sctps_gsctp != NULL);
17573448Sdh155122 	sctp_closei_local(sctps->sctps_gsctp);
17583448Sdh155122 	SCTP_CONDEMNED(sctps->sctps_gsctp);
17593448Sdh155122 	SCTP_REFRELE(sctps->sctps_gsctp);
17603448Sdh155122 	sctps->sctps_gsctp = NULL;
17613448Sdh155122 }
17620Sstevel@tonic-gate 
17633448Sdh155122 /*
17643448Sdh155122  * Called when last sctp_t drops reference count using SCTP_G_Q_REFRELE.
17653448Sdh155122  *
17663448Sdh155122  * Have to ensure that the ldi routines are not used by an
17673448Sdh155122  * interrupt thread by using a taskq.
17683448Sdh155122  */
17693448Sdh155122 void
17703448Sdh155122 sctp_g_q_inactive(sctp_stack_t *sctps)
17713448Sdh155122 {
17723448Sdh155122 	if (sctps->sctps_g_q_lh == NULL)
17733448Sdh155122 		return;	/* Nothing to cleanup */
17743448Sdh155122 
17753448Sdh155122 	ASSERT(sctps->sctps_g_q_ref == 0);
17763448Sdh155122 	SCTP_G_Q_REFHOLD(sctps); /* Compensate for what g_q_destroy did */
17773448Sdh155122 
17783448Sdh155122 	if (servicing_interrupt()) {
17793448Sdh155122 		(void) taskq_dispatch(sctp_taskq, sctp_g_q_close,
17804505Skcpoon 		    (void *) sctps, TQ_SLEEP);
17813448Sdh155122 	} else {
17823448Sdh155122 		sctp_g_q_close(sctps);
17830Sstevel@tonic-gate 	}
17843448Sdh155122 }
17853448Sdh155122 
17863448Sdh155122 /* Run at module load time */
17873448Sdh155122 void
17883448Sdh155122 sctp_ddi_g_init(void)
17893448Sdh155122 {
17900Sstevel@tonic-gate 	/* Create sctp_t/conn_t cache */
17910Sstevel@tonic-gate 	sctp_conn_cache_init();
17920Sstevel@tonic-gate 
17930Sstevel@tonic-gate 	/* Create the faddr cache */
17940Sstevel@tonic-gate 	sctp_faddr_init();
17950Sstevel@tonic-gate 
17960Sstevel@tonic-gate 	/* Create the sets cache */
17970Sstevel@tonic-gate 	sctp_sets_init();
17980Sstevel@tonic-gate 
17990Sstevel@tonic-gate 	/* Create the PR-SCTP sets cache */
18000Sstevel@tonic-gate 	sctp_ftsn_sets_init();
18010Sstevel@tonic-gate 
18020Sstevel@tonic-gate 	/* Initialize tables used for CRC calculation */
18030Sstevel@tonic-gate 	sctp_crc32_init();
18040Sstevel@tonic-gate 
18053448Sdh155122 	sctp_taskq = taskq_create("sctp_taskq", 1, minclsyspri, 1, 1,
18063448Sdh155122 	    TASKQ_PREPOPULATE);
18073448Sdh155122 
18083448Sdh155122 	/*
18093448Sdh155122 	 * We want to be informed each time a stack is created or
18103448Sdh155122 	 * destroyed in the kernel, so we can maintain the
18113448Sdh155122 	 * set of sctp_stack_t's.
18123448Sdh155122 	 */
18133448Sdh155122 	netstack_register(NS_SCTP, sctp_stack_init, sctp_stack_shutdown,
18143448Sdh155122 	    sctp_stack_fini);
18150Sstevel@tonic-gate }
18160Sstevel@tonic-gate 
18173448Sdh155122 static void *
18183448Sdh155122 sctp_stack_init(netstackid_t stackid, netstack_t *ns)
18190Sstevel@tonic-gate {
18203448Sdh155122 	sctp_stack_t	*sctps;
18213448Sdh155122 
18223448Sdh155122 	sctps = kmem_zalloc(sizeof (*sctps), KM_SLEEP);
18233448Sdh155122 	sctps->sctps_netstack = ns;
18243448Sdh155122 
18253448Sdh155122 	/* Initialize locks */
18263448Sdh155122 	mutex_init(&sctps->sctps_g_q_lock, NULL, MUTEX_DEFAULT, NULL);
18273448Sdh155122 	cv_init(&sctps->sctps_g_q_cv, NULL, CV_DEFAULT, NULL);
18283448Sdh155122 	mutex_init(&sctps->sctps_g_lock, NULL, MUTEX_DEFAULT, NULL);
18293448Sdh155122 	mutex_init(&sctps->sctps_epriv_port_lock, NULL, MUTEX_DEFAULT, NULL);
18303448Sdh155122 	sctps->sctps_g_num_epriv_ports = SCTP_NUM_EPRIV_PORTS;
18313448Sdh155122 	sctps->sctps_g_epriv_ports[0] = 2049;
18323448Sdh155122 	sctps->sctps_g_epriv_ports[1] = 4045;
18333448Sdh155122 
18343448Sdh155122 	/* Initialize SCTP hash arrays. */
18353448Sdh155122 	sctp_hash_init(sctps);
18363448Sdh155122 
18373448Sdh155122 	if (!sctp_nd_init(sctps)) {
18383448Sdh155122 		sctp_nd_free(sctps);
18393448Sdh155122 	}
18400Sstevel@tonic-gate 
18413448Sdh155122 	/* Initialize the recvq taskq. */
18423448Sdh155122 	sctp_rq_tq_init(sctps);
18433448Sdh155122 
18443448Sdh155122 	/* saddr init */
18453448Sdh155122 	sctp_saddr_init(sctps);
18463448Sdh155122 
18473448Sdh155122 	/* Global SCTP PCB list. */
18483448Sdh155122 	list_create(&sctps->sctps_g_list, sizeof (sctp_t),
18493448Sdh155122 	    offsetof(sctp_t, sctp_list));
18503448Sdh155122 
18513448Sdh155122 	/* Initialize sctp kernel stats. */
18523448Sdh155122 	sctps->sctps_mibkp = sctp_kstat_init(stackid);
18533448Sdh155122 	sctps->sctps_kstat =
18543448Sdh155122 	    sctp_kstat2_init(stackid, &sctps->sctps_statistics);
18553448Sdh155122 
18563448Sdh155122 	return (sctps);
18573448Sdh155122 }
18583448Sdh155122 
18593448Sdh155122 /*
18603448Sdh155122  * Called when the module is about to be unloaded.
18613448Sdh155122  */
18623448Sdh155122 void
18633448Sdh155122 sctp_ddi_g_destroy(void)
18643448Sdh155122 {
18650Sstevel@tonic-gate 	/* Destroy sctp_t/conn_t caches */
18660Sstevel@tonic-gate 	sctp_conn_cache_fini();
18670Sstevel@tonic-gate 
18680Sstevel@tonic-gate 	/* Destroy the faddr cache */
18690Sstevel@tonic-gate 	sctp_faddr_fini();
18700Sstevel@tonic-gate 
18710Sstevel@tonic-gate 	/* Destroy the sets cache */
18720Sstevel@tonic-gate 	sctp_sets_fini();
18730Sstevel@tonic-gate 
18740Sstevel@tonic-gate 	/* Destroy the PR-SCTP sets cache */
18750Sstevel@tonic-gate 	sctp_ftsn_sets_fini();
18760Sstevel@tonic-gate 
18773448Sdh155122 	netstack_unregister(NS_SCTP);
18783448Sdh155122 	taskq_destroy(sctp_taskq);
18793448Sdh155122 }
18803448Sdh155122 
18813448Sdh155122 /*
18823448Sdh155122  * Shut down the SCTP stack instance.
18833448Sdh155122  */
18843448Sdh155122 /* ARGSUSED */
18853448Sdh155122 static void
18863448Sdh155122 sctp_stack_shutdown(netstackid_t stackid, void *arg)
18873448Sdh155122 {
18883448Sdh155122 	sctp_stack_t *sctps = (sctp_stack_t *)arg;
18893448Sdh155122 
18903448Sdh155122 	sctp_g_q_destroy(sctps);
18913448Sdh155122 }
18923448Sdh155122 
18933448Sdh155122 /*
18943448Sdh155122  * Free the SCTP stack instance.
18953448Sdh155122  */
18963448Sdh155122 static void
18973448Sdh155122 sctp_stack_fini(netstackid_t stackid, void *arg)
18983448Sdh155122 {
18993448Sdh155122 	sctp_stack_t *sctps = (sctp_stack_t *)arg;
19003448Sdh155122 
19013448Sdh155122 	sctp_nd_free(sctps);
19023448Sdh155122 
19030Sstevel@tonic-gate 	/* Destroy the recvq taskqs. */
19043448Sdh155122 	sctp_rq_tq_fini(sctps);
19050Sstevel@tonic-gate 
19060Sstevel@tonic-gate 	/* Destroy saddr  */
19073448Sdh155122 	sctp_saddr_fini(sctps);
19080Sstevel@tonic-gate 
19090Sstevel@tonic-gate 	/* Global SCTP PCB list. */
19103448Sdh155122 	list_destroy(&sctps->sctps_g_list);
19110Sstevel@tonic-gate 
19120Sstevel@tonic-gate 	/* Destroy SCTP hash arrays. */
19133448Sdh155122 	sctp_hash_destroy(sctps);
19143448Sdh155122 
19153448Sdh155122 	/* Destroy SCTP kernel stats. */
19163448Sdh155122 	sctp_kstat2_fini(stackid, sctps->sctps_kstat);
19173448Sdh155122 	sctps->sctps_kstat = NULL;
19183448Sdh155122 	bzero(&sctps->sctps_statistics, sizeof (sctps->sctps_statistics));
19193448Sdh155122 
19203448Sdh155122 	sctp_kstat_fini(stackid, sctps->sctps_mibkp);
19213448Sdh155122 	sctps->sctps_mibkp = NULL;
19220Sstevel@tonic-gate 
19233448Sdh155122 	mutex_destroy(&sctps->sctps_g_lock);
19243448Sdh155122 	mutex_destroy(&sctps->sctps_epriv_port_lock);
19253448Sdh155122 	mutex_destroy(&sctps->sctps_g_q_lock);
19263448Sdh155122 	cv_destroy(&sctps->sctps_g_q_cv);
19273448Sdh155122 
19283448Sdh155122 	kmem_free(sctps, sizeof (*sctps));
19290Sstevel@tonic-gate }
19300Sstevel@tonic-gate 
19310Sstevel@tonic-gate void
19323448Sdh155122 sctp_display_all(sctp_stack_t *sctps)
19330Sstevel@tonic-gate {
19340Sstevel@tonic-gate 	sctp_t *sctp_walker;
19350Sstevel@tonic-gate 
19363448Sdh155122 	mutex_enter(&sctps->sctps_g_lock);
19373448Sdh155122 	for (sctp_walker = sctps->sctps_gsctp; sctp_walker != NULL;
19383448Sdh155122 	    sctp_walker = (sctp_t *)list_next(&sctps->sctps_g_list,
19393448Sdh155122 	    sctp_walker)) {
19400Sstevel@tonic-gate 		(void) sctp_display(sctp_walker, NULL);
19410Sstevel@tonic-gate 	}
19423448Sdh155122 	mutex_exit(&sctps->sctps_g_lock);
19430Sstevel@tonic-gate }
19440Sstevel@tonic-gate 
19450Sstevel@tonic-gate static void
19463448Sdh155122 sctp_rq_tq_init(sctp_stack_t *sctps)
19470Sstevel@tonic-gate {
19483448Sdh155122 	sctps->sctps_recvq_tq_list_max_sz = 16;
19493448Sdh155122 	sctps->sctps_recvq_tq_list_cur_sz = 1;
19500Sstevel@tonic-gate 	/*
19510Sstevel@tonic-gate 	 * Initialize the recvq_tq_list and create the first recvq taskq.
19520Sstevel@tonic-gate 	 * What to do if it fails?
19530Sstevel@tonic-gate 	 */
19543448Sdh155122 	sctps->sctps_recvq_tq_list =
19553448Sdh155122 	    kmem_zalloc(sctps->sctps_recvq_tq_list_max_sz * sizeof (taskq_t *),
19564505Skcpoon 	    KM_SLEEP);
19573448Sdh155122 	sctps->sctps_recvq_tq_list[0] = taskq_create("sctp_def_recvq_taskq",
19580Sstevel@tonic-gate 	    MIN(sctp_recvq_tq_thr_max, MAX(sctp_recvq_tq_thr_min, ncpus)),
19590Sstevel@tonic-gate 	    minclsyspri, sctp_recvq_tq_task_min, sctp_recvq_tq_task_max,
19600Sstevel@tonic-gate 	    TASKQ_PREPOPULATE);
19613448Sdh155122 	mutex_init(&sctps->sctps_rq_tq_lock, NULL, MUTEX_DEFAULT, NULL);
19620Sstevel@tonic-gate }
19630Sstevel@tonic-gate 
19640Sstevel@tonic-gate static void
19653448Sdh155122 sctp_rq_tq_fini(sctp_stack_t *sctps)
19660Sstevel@tonic-gate {
19670Sstevel@tonic-gate 	int i;
19680Sstevel@tonic-gate 
19693448Sdh155122 	for (i = 0; i < sctps->sctps_recvq_tq_list_cur_sz; i++) {
19703448Sdh155122 		ASSERT(sctps->sctps_recvq_tq_list[i] != NULL);
19713448Sdh155122 		taskq_destroy(sctps->sctps_recvq_tq_list[i]);
19720Sstevel@tonic-gate 	}
19733448Sdh155122 	kmem_free(sctps->sctps_recvq_tq_list,
19743448Sdh155122 	    sctps->sctps_recvq_tq_list_max_sz * sizeof (taskq_t *));
19753448Sdh155122 	sctps->sctps_recvq_tq_list = NULL;
19760Sstevel@tonic-gate }
19770Sstevel@tonic-gate 
19780Sstevel@tonic-gate /* Add another taskq for a new ill. */
19790Sstevel@tonic-gate void
19803448Sdh155122 sctp_inc_taskq(sctp_stack_t *sctps)
19810Sstevel@tonic-gate {
19820Sstevel@tonic-gate 	taskq_t *tq;
19830Sstevel@tonic-gate 	char tq_name[TASKQ_NAMELEN];
19840Sstevel@tonic-gate 
19853448Sdh155122 	mutex_enter(&sctps->sctps_rq_tq_lock);
19863448Sdh155122 	if (sctps->sctps_recvq_tq_list_cur_sz + 1 >
19873448Sdh155122 	    sctps->sctps_recvq_tq_list_max_sz) {
19883448Sdh155122 		mutex_exit(&sctps->sctps_rq_tq_lock);
19890Sstevel@tonic-gate 		cmn_err(CE_NOTE, "Cannot create more SCTP recvq taskq");
19900Sstevel@tonic-gate 		return;
19910Sstevel@tonic-gate 	}
19920Sstevel@tonic-gate 
19930Sstevel@tonic-gate 	(void) snprintf(tq_name, sizeof (tq_name), "sctp_recvq_taskq_%u",
19943448Sdh155122 	    sctps->sctps_recvq_tq_list_cur_sz);
19950Sstevel@tonic-gate 	tq = taskq_create(tq_name,
19960Sstevel@tonic-gate 	    MIN(sctp_recvq_tq_thr_max, MAX(sctp_recvq_tq_thr_min, ncpus)),
19970Sstevel@tonic-gate 	    minclsyspri, sctp_recvq_tq_task_min, sctp_recvq_tq_task_max,
19980Sstevel@tonic-gate 	    TASKQ_PREPOPULATE);
19990Sstevel@tonic-gate 	if (tq == NULL) {
20003448Sdh155122 		mutex_exit(&sctps->sctps_rq_tq_lock);
20010Sstevel@tonic-gate 		cmn_err(CE_NOTE, "SCTP recvq taskq creation failed");
20020Sstevel@tonic-gate 		return;
20030Sstevel@tonic-gate 	}
20043448Sdh155122 	ASSERT(sctps->sctps_recvq_tq_list[
20054505Skcpoon 	    sctps->sctps_recvq_tq_list_cur_sz] == NULL);
20063448Sdh155122 	sctps->sctps_recvq_tq_list[sctps->sctps_recvq_tq_list_cur_sz] = tq;
20073448Sdh155122 	atomic_add_32(&sctps->sctps_recvq_tq_list_cur_sz, 1);
20083448Sdh155122 	mutex_exit(&sctps->sctps_rq_tq_lock);
20090Sstevel@tonic-gate }
20100Sstevel@tonic-gate 
20110Sstevel@tonic-gate #ifdef DEBUG
20120Sstevel@tonic-gate uint32_t sendq_loop_cnt = 0;
20130Sstevel@tonic-gate uint32_t sendq_collision = 0;
20140Sstevel@tonic-gate uint32_t sendq_empty = 0;
20150Sstevel@tonic-gate #endif
20160Sstevel@tonic-gate 
20170Sstevel@tonic-gate void
20180Sstevel@tonic-gate sctp_add_sendq(sctp_t *sctp, mblk_t *mp)
20190Sstevel@tonic-gate {
20200Sstevel@tonic-gate 	mutex_enter(&sctp->sctp_sendq_lock);
20210Sstevel@tonic-gate 	if (sctp->sctp_sendq == NULL) {
20220Sstevel@tonic-gate 		sctp->sctp_sendq = mp;
20230Sstevel@tonic-gate 		sctp->sctp_sendq_tail = mp;
20240Sstevel@tonic-gate 	} else {
20250Sstevel@tonic-gate 		sctp->sctp_sendq_tail->b_next = mp;
20260Sstevel@tonic-gate 		sctp->sctp_sendq_tail = mp;
20270Sstevel@tonic-gate 	}
20280Sstevel@tonic-gate 	mutex_exit(&sctp->sctp_sendq_lock);
20290Sstevel@tonic-gate }
20300Sstevel@tonic-gate 
20310Sstevel@tonic-gate void
20320Sstevel@tonic-gate sctp_process_sendq(sctp_t *sctp)
20330Sstevel@tonic-gate {
20340Sstevel@tonic-gate 	mblk_t *mp;
20350Sstevel@tonic-gate #ifdef DEBUG
20360Sstevel@tonic-gate 	uint32_t loop_cnt = 0;
20370Sstevel@tonic-gate #endif
20380Sstevel@tonic-gate 
20390Sstevel@tonic-gate 	mutex_enter(&sctp->sctp_sendq_lock);
20400Sstevel@tonic-gate 	if (sctp->sctp_sendq == NULL || sctp->sctp_sendq_sending) {
20410Sstevel@tonic-gate #ifdef DEBUG
20420Sstevel@tonic-gate 		if (sctp->sctp_sendq == NULL)
20430Sstevel@tonic-gate 			sendq_empty++;
20440Sstevel@tonic-gate 		else
20450Sstevel@tonic-gate 			sendq_collision++;
20460Sstevel@tonic-gate #endif
20470Sstevel@tonic-gate 		mutex_exit(&sctp->sctp_sendq_lock);
20480Sstevel@tonic-gate 		return;
20490Sstevel@tonic-gate 	}
20500Sstevel@tonic-gate 	sctp->sctp_sendq_sending = B_TRUE;
20510Sstevel@tonic-gate 
20520Sstevel@tonic-gate 	/*
20530Sstevel@tonic-gate 	 * Note that while we are in this loop, other thread can put
20540Sstevel@tonic-gate 	 * new packets in the receive queue.  We may be looping for
20550Sstevel@tonic-gate 	 * quite a while.  This is OK even for an interrupt thread.
20560Sstevel@tonic-gate 	 * The reason is that SCTP should only able to send a limited
20570Sstevel@tonic-gate 	 * number of packets out in a burst.  So the number of times
20580Sstevel@tonic-gate 	 * we go through this loop should not be many.
20590Sstevel@tonic-gate 	 */
20600Sstevel@tonic-gate 	while ((mp = sctp->sctp_sendq) != NULL) {
20610Sstevel@tonic-gate 		sctp->sctp_sendq = mp->b_next;
20620Sstevel@tonic-gate 		ASSERT(sctp->sctp_connp->conn_ref > 0);
20630Sstevel@tonic-gate 		mutex_exit(&sctp->sctp_sendq_lock);
20640Sstevel@tonic-gate 		mp->b_next = NULL;
20650Sstevel@tonic-gate 		CONN_INC_REF(sctp->sctp_connp);
20660Sstevel@tonic-gate 		mp->b_flag |= MSGHASREF;
20670Sstevel@tonic-gate 		/* If we don't have sctp_current, default to IPv4 */
20680Sstevel@tonic-gate 		IP_PUT(mp, sctp->sctp_connp, sctp->sctp_current == NULL ?
20690Sstevel@tonic-gate 		    B_TRUE : sctp->sctp_current->isv4);
20700Sstevel@tonic-gate 		BUMP_LOCAL(sctp->sctp_opkts);
20710Sstevel@tonic-gate #ifdef DEBUG
20720Sstevel@tonic-gate 		loop_cnt++;
20730Sstevel@tonic-gate #endif
20740Sstevel@tonic-gate 		mutex_enter(&sctp->sctp_sendq_lock);
20750Sstevel@tonic-gate 	}
20760Sstevel@tonic-gate 
20770Sstevel@tonic-gate 	sctp->sctp_sendq_tail = NULL;
20780Sstevel@tonic-gate 	sctp->sctp_sendq_sending = B_FALSE;
20790Sstevel@tonic-gate #ifdef DEBUG
20800Sstevel@tonic-gate 	if (loop_cnt > sendq_loop_cnt)
20810Sstevel@tonic-gate 		sendq_loop_cnt = loop_cnt;
20820Sstevel@tonic-gate #endif
20830Sstevel@tonic-gate 	mutex_exit(&sctp->sctp_sendq_lock);
20840Sstevel@tonic-gate }
20850Sstevel@tonic-gate 
20860Sstevel@tonic-gate #ifdef DEBUG
20870Sstevel@tonic-gate uint32_t recvq_loop_cnt = 0;
20880Sstevel@tonic-gate uint32_t recvq_call = 0;
20890Sstevel@tonic-gate #endif
20900Sstevel@tonic-gate 
20910Sstevel@tonic-gate /*
20920Sstevel@tonic-gate  * Find the next recvq_tq to use.  This routine will go thru all the
20930Sstevel@tonic-gate  * taskqs until it can dispatch a job for the sctp.  If this fails,
20940Sstevel@tonic-gate  * it will create a new taskq and try it.
20950Sstevel@tonic-gate  */
20960Sstevel@tonic-gate static boolean_t
20970Sstevel@tonic-gate sctp_find_next_tq(sctp_t *sctp)
20980Sstevel@tonic-gate {
20990Sstevel@tonic-gate 	int next_tq, try;
21000Sstevel@tonic-gate 	taskq_t *tq;
21013448Sdh155122 	sctp_stack_t	*sctps = sctp->sctp_sctps;
21020Sstevel@tonic-gate 
21030Sstevel@tonic-gate 	/*
21040Sstevel@tonic-gate 	 * Note that since we don't hold a lock on sctp_rq_tq_lock for
21050Sstevel@tonic-gate 	 * performance reason, recvq_ta_list_cur_sz can be changed during
21060Sstevel@tonic-gate 	 * this loop.  The problem this will create is that the loop may
21070Sstevel@tonic-gate 	 * not have tried all the recvq_tq.  This should be OK.
21080Sstevel@tonic-gate 	 */
21093448Sdh155122 	next_tq = atomic_add_32_nv(&sctps->sctps_recvq_tq_list_cur, 1) %
21103448Sdh155122 	    sctps->sctps_recvq_tq_list_cur_sz;
21113448Sdh155122 	for (try = 0; try < sctps->sctps_recvq_tq_list_cur_sz; try++) {
21123448Sdh155122 		tq = sctps->sctps_recvq_tq_list[next_tq];
21130Sstevel@tonic-gate 		if (taskq_dispatch(tq, sctp_process_recvq, sctp,
21140Sstevel@tonic-gate 		    TQ_NOSLEEP) != NULL) {
21150Sstevel@tonic-gate 			sctp->sctp_recvq_tq = tq;
21160Sstevel@tonic-gate 			return (B_TRUE);
21170Sstevel@tonic-gate 		}
21183448Sdh155122 		next_tq = (next_tq + 1) % sctps->sctps_recvq_tq_list_cur_sz;
21190Sstevel@tonic-gate 	}
21200Sstevel@tonic-gate 
21210Sstevel@tonic-gate 	/*
21220Sstevel@tonic-gate 	 * Create one more taskq and try it.  Note that sctp_inc_taskq()
21230Sstevel@tonic-gate 	 * may not have created another taskq if the number of recvq
21240Sstevel@tonic-gate 	 * taskqs is at the maximum.  We are probably in a pretty bad
21250Sstevel@tonic-gate 	 * shape if this actually happens...
21260Sstevel@tonic-gate 	 */
21273448Sdh155122 	sctp_inc_taskq(sctps);
21283448Sdh155122 	tq = sctps->sctps_recvq_tq_list[sctps->sctps_recvq_tq_list_cur_sz - 1];
21290Sstevel@tonic-gate 	if (taskq_dispatch(tq, sctp_process_recvq, sctp, TQ_NOSLEEP) != NULL) {
21300Sstevel@tonic-gate 		sctp->sctp_recvq_tq = tq;
21310Sstevel@tonic-gate 		return (B_TRUE);
21320Sstevel@tonic-gate 	}
21333448Sdh155122 	SCTP_KSTAT(sctps, sctp_find_next_tq);
21340Sstevel@tonic-gate 	return (B_FALSE);
21350Sstevel@tonic-gate }
21360Sstevel@tonic-gate 
21370Sstevel@tonic-gate /*
21380Sstevel@tonic-gate  * To add a message to the recvq.  Note that the sctp_timer_fire()
21390Sstevel@tonic-gate  * routine also uses this function to add the timer message to the
21400Sstevel@tonic-gate  * receive queue for later processing.  And it should be the only
21410Sstevel@tonic-gate  * caller of sctp_add_recvq() which sets the try_harder argument
21420Sstevel@tonic-gate  * to B_TRUE.
21430Sstevel@tonic-gate  *
21440Sstevel@tonic-gate  * If the try_harder argument is B_TRUE, this routine sctp_find_next_tq()
21450Sstevel@tonic-gate  * will try very hard to dispatch the task.  Refer to the comment
21460Sstevel@tonic-gate  * for that routine on how it does that.
21470Sstevel@tonic-gate  */
21480Sstevel@tonic-gate boolean_t
21490Sstevel@tonic-gate sctp_add_recvq(sctp_t *sctp, mblk_t *mp, boolean_t caller_hold_lock)
21500Sstevel@tonic-gate {
21510Sstevel@tonic-gate 	if (!caller_hold_lock)
21520Sstevel@tonic-gate 		mutex_enter(&sctp->sctp_recvq_lock);
21530Sstevel@tonic-gate 
21540Sstevel@tonic-gate 	/* If the taskq dispatch has not been scheduled, do it now. */
21550Sstevel@tonic-gate 	if (sctp->sctp_recvq_tq == NULL) {
21560Sstevel@tonic-gate 		ASSERT(sctp->sctp_recvq == NULL);
21570Sstevel@tonic-gate 		if (!sctp_find_next_tq(sctp)) {
21580Sstevel@tonic-gate 			if (!caller_hold_lock)
21590Sstevel@tonic-gate 				mutex_exit(&sctp->sctp_recvq_lock);
21600Sstevel@tonic-gate 			return (B_FALSE);
21610Sstevel@tonic-gate 		}
21620Sstevel@tonic-gate 		/* Make sure the sctp_t will not go away. */
21630Sstevel@tonic-gate 		SCTP_REFHOLD(sctp);
21640Sstevel@tonic-gate 	}
21650Sstevel@tonic-gate 
21660Sstevel@tonic-gate 	if (sctp->sctp_recvq == NULL) {
21670Sstevel@tonic-gate 		sctp->sctp_recvq = mp;
21680Sstevel@tonic-gate 		sctp->sctp_recvq_tail = mp;
21690Sstevel@tonic-gate 	} else {
21700Sstevel@tonic-gate 		sctp->sctp_recvq_tail->b_next = mp;
21710Sstevel@tonic-gate 		sctp->sctp_recvq_tail = mp;
21720Sstevel@tonic-gate 	}
21730Sstevel@tonic-gate 
21740Sstevel@tonic-gate 	if (!caller_hold_lock)
21750Sstevel@tonic-gate 		mutex_exit(&sctp->sctp_recvq_lock);
21760Sstevel@tonic-gate 	return (B_TRUE);
21770Sstevel@tonic-gate }
21780Sstevel@tonic-gate 
21790Sstevel@tonic-gate static void
21800Sstevel@tonic-gate sctp_process_recvq(void *arg)
21810Sstevel@tonic-gate {
21820Sstevel@tonic-gate 	sctp_t		*sctp = (sctp_t *)arg;
21830Sstevel@tonic-gate 	mblk_t		*mp;
21840Sstevel@tonic-gate 	mblk_t		*ipsec_mp;
21850Sstevel@tonic-gate #ifdef DEBUG
21860Sstevel@tonic-gate 	uint32_t	loop_cnt = 0;
21870Sstevel@tonic-gate #endif
21880Sstevel@tonic-gate 
21890Sstevel@tonic-gate #ifdef	_BIG_ENDIAN
21900Sstevel@tonic-gate #define	IPVER(ip6h)	((((uint32_t *)ip6h)[0] >> 28) & 0x7)
21910Sstevel@tonic-gate #else
21920Sstevel@tonic-gate #define	IPVER(ip6h)	((((uint32_t *)ip6h)[0] >> 4) & 0x7)
21930Sstevel@tonic-gate #endif
21940Sstevel@tonic-gate 
21950Sstevel@tonic-gate 	RUN_SCTP(sctp);
21960Sstevel@tonic-gate 	mutex_enter(&sctp->sctp_recvq_lock);
21970Sstevel@tonic-gate 
21980Sstevel@tonic-gate #ifdef DEBUG
21990Sstevel@tonic-gate 	recvq_call++;
22000Sstevel@tonic-gate #endif
22010Sstevel@tonic-gate 	/*
22020Sstevel@tonic-gate 	 * Note that while we are in this loop, other thread can put
22030Sstevel@tonic-gate 	 * new packets in the receive queue.  We may be looping for
22040Sstevel@tonic-gate 	 * quite a while.
22050Sstevel@tonic-gate 	 */
22060Sstevel@tonic-gate 	while ((mp = sctp->sctp_recvq) != NULL) {
22070Sstevel@tonic-gate 		sctp->sctp_recvq = mp->b_next;
22080Sstevel@tonic-gate 		mutex_exit(&sctp->sctp_recvq_lock);
22090Sstevel@tonic-gate 		mp->b_next = NULL;
22100Sstevel@tonic-gate #ifdef DEBUG
22110Sstevel@tonic-gate 		loop_cnt++;
22120Sstevel@tonic-gate #endif
22130Sstevel@tonic-gate 		ipsec_mp = mp->b_prev;
22140Sstevel@tonic-gate 		mp->b_prev = NULL;
22150Sstevel@tonic-gate 		sctp_input_data(sctp, mp, ipsec_mp);
22160Sstevel@tonic-gate 
22170Sstevel@tonic-gate 		mutex_enter(&sctp->sctp_recvq_lock);
22180Sstevel@tonic-gate 	}
22190Sstevel@tonic-gate 
22200Sstevel@tonic-gate 	sctp->sctp_recvq_tail = NULL;
22210Sstevel@tonic-gate 	sctp->sctp_recvq_tq = NULL;
22220Sstevel@tonic-gate 
22230Sstevel@tonic-gate 	mutex_exit(&sctp->sctp_recvq_lock);
22240Sstevel@tonic-gate 
22250Sstevel@tonic-gate 	WAKE_SCTP(sctp);
22260Sstevel@tonic-gate 
22270Sstevel@tonic-gate 	/* We may have sent something when processing the receive queue. */
22280Sstevel@tonic-gate 	sctp_process_sendq(sctp);
22290Sstevel@tonic-gate #ifdef DEBUG
22300Sstevel@tonic-gate 	if (loop_cnt > recvq_loop_cnt)
22310Sstevel@tonic-gate 		recvq_loop_cnt = loop_cnt;
22320Sstevel@tonic-gate #endif
22330Sstevel@tonic-gate 	/* Now it can go away. */
22340Sstevel@tonic-gate 	SCTP_REFRELE(sctp);
22350Sstevel@tonic-gate }
22360Sstevel@tonic-gate 
22370Sstevel@tonic-gate /* ARGSUSED */
22380Sstevel@tonic-gate static int
22390Sstevel@tonic-gate sctp_conn_cache_constructor(void *buf, void *cdrarg, int kmflags)
22400Sstevel@tonic-gate {
22410Sstevel@tonic-gate 	conn_t	*sctp_connp = (conn_t *)buf;
22420Sstevel@tonic-gate 	sctp_t	*sctp = (sctp_t *)&sctp_connp[1];
22430Sstevel@tonic-gate 
22440Sstevel@tonic-gate 	bzero(buf, (char *)&sctp[1] - (char *)buf);
22450Sstevel@tonic-gate 
22460Sstevel@tonic-gate 	sctp->sctp_connp = sctp_connp;
22470Sstevel@tonic-gate 	mutex_init(&sctp->sctp_reflock, NULL, MUTEX_DEFAULT, NULL);
22480Sstevel@tonic-gate 	mutex_init(&sctp->sctp_lock, NULL, MUTEX_DEFAULT, NULL);
22490Sstevel@tonic-gate 	mutex_init(&sctp->sctp_recvq_lock, NULL, MUTEX_DEFAULT, NULL);
22500Sstevel@tonic-gate 	cv_init(&sctp->sctp_cv, NULL, CV_DEFAULT, NULL);
22510Sstevel@tonic-gate 	mutex_init(&sctp->sctp_sendq_lock, NULL, MUTEX_DEFAULT, NULL);
22520Sstevel@tonic-gate 
22530Sstevel@tonic-gate 	return (0);
22540Sstevel@tonic-gate }
22550Sstevel@tonic-gate 
22560Sstevel@tonic-gate /* ARGSUSED */
22570Sstevel@tonic-gate static void
22580Sstevel@tonic-gate sctp_conn_cache_destructor(void *buf, void *cdrarg)
22590Sstevel@tonic-gate {
22600Sstevel@tonic-gate 	conn_t	*sctp_connp = (conn_t *)buf;
22610Sstevel@tonic-gate 	sctp_t	*sctp = (sctp_t *)&sctp_connp[1];
22620Sstevel@tonic-gate 
22630Sstevel@tonic-gate 	ASSERT(!MUTEX_HELD(&sctp->sctp_lock));
22640Sstevel@tonic-gate 	ASSERT(!MUTEX_HELD(&sctp->sctp_reflock));
22650Sstevel@tonic-gate 	ASSERT(!MUTEX_HELD(&sctp->sctp_recvq_lock));
22660Sstevel@tonic-gate 	ASSERT(!MUTEX_HELD(&sctp->sctp_sendq_lock));
22670Sstevel@tonic-gate 	ASSERT(!MUTEX_HELD(&sctp->sctp_connp->conn_lock));
22680Sstevel@tonic-gate 
22690Sstevel@tonic-gate 	ASSERT(sctp->sctp_conn_hash_next == NULL);
22700Sstevel@tonic-gate 	ASSERT(sctp->sctp_conn_hash_prev == NULL);
22710Sstevel@tonic-gate 	ASSERT(sctp->sctp_listen_hash_next == NULL);
22720Sstevel@tonic-gate 	ASSERT(sctp->sctp_listen_hash_prev == NULL);
22730Sstevel@tonic-gate 	ASSERT(sctp->sctp_listen_tfp == NULL);
22740Sstevel@tonic-gate 	ASSERT(sctp->sctp_conn_tfp == NULL);
22750Sstevel@tonic-gate 
22760Sstevel@tonic-gate 	ASSERT(sctp->sctp_faddrs == NULL);
22770Sstevel@tonic-gate 	ASSERT(sctp->sctp_nsaddrs == 0);
22780Sstevel@tonic-gate 
22790Sstevel@tonic-gate 	ASSERT(sctp->sctp_ulpd == NULL);
22800Sstevel@tonic-gate 
22810Sstevel@tonic-gate 	ASSERT(sctp->sctp_lastfaddr == NULL);
22820Sstevel@tonic-gate 	ASSERT(sctp->sctp_primary == NULL);
22830Sstevel@tonic-gate 	ASSERT(sctp->sctp_current == NULL);
22840Sstevel@tonic-gate 	ASSERT(sctp->sctp_lastdata == NULL);
22850Sstevel@tonic-gate 
22860Sstevel@tonic-gate 	ASSERT(sctp->sctp_xmit_head == NULL);
22870Sstevel@tonic-gate 	ASSERT(sctp->sctp_xmit_tail == NULL);
22880Sstevel@tonic-gate 	ASSERT(sctp->sctp_xmit_unsent == NULL);
22890Sstevel@tonic-gate 	ASSERT(sctp->sctp_xmit_unsent_tail == NULL);
22900Sstevel@tonic-gate 
22910Sstevel@tonic-gate 	ASSERT(sctp->sctp_ostrcntrs == NULL);
22920Sstevel@tonic-gate 
22930Sstevel@tonic-gate 	ASSERT(sctp->sctp_sack_info == NULL);
22940Sstevel@tonic-gate 	ASSERT(sctp->sctp_ack_mp == NULL);
22950Sstevel@tonic-gate 	ASSERT(sctp->sctp_instr == NULL);
22960Sstevel@tonic-gate 
22970Sstevel@tonic-gate 	ASSERT(sctp->sctp_iphc == NULL);
22980Sstevel@tonic-gate 	ASSERT(sctp->sctp_iphc6 == NULL);
22990Sstevel@tonic-gate 	ASSERT(sctp->sctp_ipha == NULL);
23000Sstevel@tonic-gate 	ASSERT(sctp->sctp_ip6h == NULL);
23010Sstevel@tonic-gate 	ASSERT(sctp->sctp_sctph == NULL);
23020Sstevel@tonic-gate 	ASSERT(sctp->sctp_sctph6 == NULL);
23030Sstevel@tonic-gate 
23040Sstevel@tonic-gate 	ASSERT(sctp->sctp_cookie_mp == NULL);
23050Sstevel@tonic-gate 
23060Sstevel@tonic-gate 	ASSERT(sctp->sctp_refcnt == 0);
23070Sstevel@tonic-gate 	ASSERT(sctp->sctp_timer_mp == NULL);
23080Sstevel@tonic-gate 	ASSERT(sctp->sctp_connp->conn_ref == 0);
23090Sstevel@tonic-gate 	ASSERT(sctp->sctp_heartbeat_mp == NULL);
23100Sstevel@tonic-gate 	ASSERT(sctp->sctp_ptpbhn == NULL && sctp->sctp_bind_hash == NULL);
23110Sstevel@tonic-gate 
23120Sstevel@tonic-gate 	ASSERT(sctp->sctp_shutdown_faddr == NULL);
23130Sstevel@tonic-gate 
23140Sstevel@tonic-gate 	ASSERT(sctp->sctp_cxmit_list == NULL);
23150Sstevel@tonic-gate 
23160Sstevel@tonic-gate 	ASSERT(sctp->sctp_recvq == NULL);
23170Sstevel@tonic-gate 	ASSERT(sctp->sctp_recvq_tail == NULL);
23180Sstevel@tonic-gate 	ASSERT(sctp->sctp_recvq_tq == NULL);
23190Sstevel@tonic-gate 
23200Sstevel@tonic-gate 	ASSERT(sctp->sctp_sendq == NULL);
23210Sstevel@tonic-gate 	ASSERT(sctp->sctp_sendq_tail == NULL);
23220Sstevel@tonic-gate 	ASSERT(sctp->sctp_sendq_sending == B_FALSE);
23230Sstevel@tonic-gate 
23242283Skp158701 	ASSERT(sctp->sctp_ipp_hopopts == NULL);
23252283Skp158701 	ASSERT(sctp->sctp_ipp_rtdstopts == NULL);
23262283Skp158701 	ASSERT(sctp->sctp_ipp_rthdr == NULL);
23272283Skp158701 	ASSERT(sctp->sctp_ipp_dstopts == NULL);
23282283Skp158701 	ASSERT(sctp->sctp_ipp_pathmtu == NULL);
23292283Skp158701 
23304691Skcpoon 	/*
23314691Skcpoon 	 * sctp_pad_mp can be NULL if the memory allocation fails
23324691Skcpoon 	 * in sctp_init_values() and the conn_t is freed.
23334691Skcpoon 	 */
23344691Skcpoon 	if (sctp->sctp_pad_mp != NULL) {
23354691Skcpoon 		freeb(sctp->sctp_pad_mp);
23364691Skcpoon 		sctp->sctp_pad_mp = NULL;
23374691Skcpoon 	}
23384691Skcpoon 
23390Sstevel@tonic-gate 	mutex_destroy(&sctp->sctp_reflock);
23400Sstevel@tonic-gate 	mutex_destroy(&sctp->sctp_lock);
23410Sstevel@tonic-gate 	mutex_destroy(&sctp->sctp_recvq_lock);
23420Sstevel@tonic-gate 	cv_destroy(&sctp->sctp_cv);
23430Sstevel@tonic-gate 	mutex_destroy(&sctp->sctp_sendq_lock);
23440Sstevel@tonic-gate 
23450Sstevel@tonic-gate }
23460Sstevel@tonic-gate 
23470Sstevel@tonic-gate static void
23480Sstevel@tonic-gate sctp_conn_cache_init()
23490Sstevel@tonic-gate {
23500Sstevel@tonic-gate 	sctp_conn_cache = kmem_cache_create("sctp_conn_cache",
23510Sstevel@tonic-gate 	    sizeof (sctp_t) + sizeof (conn_t), 0, sctp_conn_cache_constructor,
23520Sstevel@tonic-gate 	    sctp_conn_cache_destructor, NULL, NULL, NULL, 0);
23530Sstevel@tonic-gate }
23540Sstevel@tonic-gate 
23550Sstevel@tonic-gate static void
23560Sstevel@tonic-gate sctp_conn_cache_fini()
23570Sstevel@tonic-gate {
23580Sstevel@tonic-gate 	kmem_cache_destroy(sctp_conn_cache);
23590Sstevel@tonic-gate }
23604691Skcpoon 
23614691Skcpoon void
23624691Skcpoon sctp_conn_init(conn_t *connp)
23634691Skcpoon {
23644691Skcpoon 	connp->conn_flags = IPCL_SCTPCONN;
23654691Skcpoon 	connp->conn_rq = connp->conn_wq = NULL;
23664691Skcpoon 	connp->conn_multicast_loop = IP_DEFAULT_MULTICAST_LOOP;
23674691Skcpoon 	connp->conn_ulp = IPPROTO_SCTP;
23684691Skcpoon 	connp->conn_state_flags |= CONN_INCIPIENT;
23694691Skcpoon 	mutex_init(&connp->conn_lock, NULL, MUTEX_DEFAULT, NULL);
23704691Skcpoon 	cv_init(&connp->conn_cv, NULL, CV_DEFAULT, NULL);
23714691Skcpoon }
23724691Skcpoon 
23734691Skcpoon static void
23744691Skcpoon sctp_conn_clear(conn_t *connp)
23754691Skcpoon {
23764691Skcpoon 	/* Clean up conn_t stuff */
23774691Skcpoon 	if (connp->conn_latch != NULL)
23784691Skcpoon 		IPLATCH_REFRELE(connp->conn_latch, connp->conn_netstack);
23794691Skcpoon 	if (connp->conn_policy != NULL)
23804691Skcpoon 		IPPH_REFRELE(connp->conn_policy, connp->conn_netstack);
23814691Skcpoon 	if (connp->conn_ipsec_opt_mp != NULL)
23824691Skcpoon 		freemsg(connp->conn_ipsec_opt_mp);
23839710SKen.Powell@Sun.COM 	if (connp->conn_cred != NULL)
23849710SKen.Powell@Sun.COM 		crfree(connp->conn_cred);
23859710SKen.Powell@Sun.COM 	if (connp->conn_effective_cred != NULL)
23869710SKen.Powell@Sun.COM 		crfree(connp->conn_effective_cred);
23874691Skcpoon 	mutex_destroy(&connp->conn_lock);
23884691Skcpoon 	cv_destroy(&connp->conn_cv);
23894691Skcpoon 	netstack_rele(connp->conn_netstack);
23904691Skcpoon 	bzero(connp, sizeof (struct conn_s));
23914691Skcpoon }
2392