xref: /onnv-gate/usr/src/uts/common/inet/sctp/sctp.c (revision 1213:f4455803313c)
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
5*1213Skcpoon  * Common Development and Distribution License (the "License").
6*1213Skcpoon  * 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  */
21*1213Skcpoon 
220Sstevel@tonic-gate /*
23*1213Skcpoon  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate #include <sys/types.h>
300Sstevel@tonic-gate #include <sys/stream.h>
310Sstevel@tonic-gate #include <sys/strsubr.h>
320Sstevel@tonic-gate #include <sys/stropts.h>
330Sstevel@tonic-gate #include <sys/strsun.h>
340Sstevel@tonic-gate #define	_SUN_TPI_VERSION 2
350Sstevel@tonic-gate #include <sys/tihdr.h>
360Sstevel@tonic-gate #include <sys/ddi.h>
370Sstevel@tonic-gate #include <sys/sunddi.h>
380Sstevel@tonic-gate #include <sys/xti_inet.h>
390Sstevel@tonic-gate #include <sys/cmn_err.h>
400Sstevel@tonic-gate #include <sys/debug.h>
410Sstevel@tonic-gate #include <sys/vtrace.h>
420Sstevel@tonic-gate #include <sys/kmem.h>
430Sstevel@tonic-gate #include <sys/cpuvar.h>
440Sstevel@tonic-gate #include <sys/random.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>
620Sstevel@tonic-gate #include <inet/nd.h>
630Sstevel@tonic-gate #include <inet/optcom.h>
640Sstevel@tonic-gate #include <inet/ipclassifier.h>
650Sstevel@tonic-gate #include <inet/ipsec_impl.h>
660Sstevel@tonic-gate #include <inet/sctp_ip.h>
670Sstevel@tonic-gate #include <inet/sctp_crc32.h>
680Sstevel@tonic-gate 
690Sstevel@tonic-gate #include "sctp_impl.h"
700Sstevel@tonic-gate #include "sctp_addr.h"
71852Svi117747 #include "sctp_asconf.h"
720Sstevel@tonic-gate 
730Sstevel@tonic-gate extern major_t SCTP6_MAJ;
740Sstevel@tonic-gate extern major_t SCTP_MAJ;
750Sstevel@tonic-gate 
760Sstevel@tonic-gate int sctpdebug;
770Sstevel@tonic-gate sin6_t	sctp_sin6_null;	/* Zero address for quick clears */
780Sstevel@tonic-gate 
790Sstevel@tonic-gate extern mblk_t *sctp_pad_mp;	/* pad unaligned data chunks */
800Sstevel@tonic-gate 
810Sstevel@tonic-gate static void	sctp_closei_local(sctp_t *sctp);
820Sstevel@tonic-gate static int	sctp_init_values(sctp_t *, sctp_t *, int);
830Sstevel@tonic-gate void		sctp_display_all();
840Sstevel@tonic-gate static void	sctp_icmp_error_ipv6(sctp_t *sctp, mblk_t *mp);
850Sstevel@tonic-gate static void	sctp_process_recvq(void *);
860Sstevel@tonic-gate static void	sctp_rq_tq_init(void);
870Sstevel@tonic-gate static void	sctp_rq_tq_fini(void);
880Sstevel@tonic-gate static void	sctp_conn_cache_init();
890Sstevel@tonic-gate static void	sctp_conn_cache_fini();
900Sstevel@tonic-gate static int	sctp_conn_cache_constructor();
910Sstevel@tonic-gate static void	sctp_conn_cache_destructor();
920Sstevel@tonic-gate void		sctp_inc_taskq(void);
930Sstevel@tonic-gate 
940Sstevel@tonic-gate /*
950Sstevel@tonic-gate  * SCTP receive queue taskq
960Sstevel@tonic-gate  *
970Sstevel@tonic-gate  * At SCTP initialization time, a default taskq is created for
980Sstevel@tonic-gate  * servicing packets received when the interrupt thread cannot
990Sstevel@tonic-gate  * get a hold on the sctp_t.  The number of taskq can be increased in
1000Sstevel@tonic-gate  * sctp_find_next_tq() when an existing taskq cannot be dispatched.
1010Sstevel@tonic-gate  * The taskqs are never removed.  But the max number of taskq which
1020Sstevel@tonic-gate  * can be created is controlled by sctp_recvq_tq_list_max_sz.  Note
1030Sstevel@tonic-gate  * that SCTP recvq taskq is not tied to any specific CPU or ill.
1040Sstevel@tonic-gate  *
1050Sstevel@tonic-gate  * Those taskqs are stored in an array recvq_tq_list.  And they are
1060Sstevel@tonic-gate  * used in a round robin fashion.  The current taskq being used is
1070Sstevel@tonic-gate  * determined by recvq_tq_list_cur.
1080Sstevel@tonic-gate  */
1090Sstevel@tonic-gate 
1100Sstevel@tonic-gate /* This lock protects the SCTP recvq_tq_list array and recvq_tq_list_cur_sz. */
1110Sstevel@tonic-gate static kmutex_t	sctp_rq_tq_lock;
1120Sstevel@tonic-gate int		sctp_recvq_tq_list_max_sz = 16;
1130Sstevel@tonic-gate static taskq_t	**recvq_tq_list;
1140Sstevel@tonic-gate 
1150Sstevel@tonic-gate /* Current number of recvq taskq.  At least 1 for the default taskq. */
1160Sstevel@tonic-gate static uint32_t	recvq_tq_list_cur_sz = 1;
1170Sstevel@tonic-gate static uint32_t	recvq_tq_list_cur = 0;
1180Sstevel@tonic-gate 
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 /*
1290Sstevel@tonic-gate  * Default queue used for sending packets.  No need to have lock for it
1300Sstevel@tonic-gate  * as it should never be changed.
1310Sstevel@tonic-gate  */
1320Sstevel@tonic-gate queue_t	*sctp_g_q;
1330Sstevel@tonic-gate int sctp_g_q_fd;
1340Sstevel@tonic-gate /* The default sctp_t for responding out of the blue packets. */
1350Sstevel@tonic-gate sctp_t *gsctp;
1360Sstevel@tonic-gate 
1370Sstevel@tonic-gate /* Protected by sctp_g_lock */
1380Sstevel@tonic-gate list_t	sctp_g_list;	/* SCTP instance data chain */
1390Sstevel@tonic-gate kmutex_t sctp_g_lock;
1400Sstevel@tonic-gate 
1410Sstevel@tonic-gate /*  sctp_t/conn_t kmem cache */
1420Sstevel@tonic-gate struct kmem_cache	*sctp_conn_cache;
1430Sstevel@tonic-gate 
1440Sstevel@tonic-gate #define	SCTP_CONDEMNED(sctp)				\
1450Sstevel@tonic-gate 	mutex_enter(&(sctp)->sctp_reflock);		\
1460Sstevel@tonic-gate 	((sctp)->sctp_condemned = B_TRUE);		\
147*1213Skcpoon 	mutex_exit(&(sctp)->sctp_reflock);
1480Sstevel@tonic-gate 
1490Sstevel@tonic-gate /* Link/unlink a sctp_t to/from the global list. */
1500Sstevel@tonic-gate #define	SCTP_LINK(sctp)					\
1510Sstevel@tonic-gate 	mutex_enter(&sctp_g_lock);			\
1520Sstevel@tonic-gate 	list_insert_tail(&sctp_g_list, (sctp));		\
1530Sstevel@tonic-gate 	mutex_exit(&sctp_g_lock);
1540Sstevel@tonic-gate 
1550Sstevel@tonic-gate #define	SCTP_UNLINK(sctp)				\
1560Sstevel@tonic-gate 	mutex_enter(&sctp_g_lock);			\
1570Sstevel@tonic-gate 	ASSERT((sctp)->sctp_condemned);			\
1580Sstevel@tonic-gate 	list_remove(&sctp_g_list, (sctp));		\
1590Sstevel@tonic-gate 	mutex_exit(&sctp_g_lock);
1600Sstevel@tonic-gate 
1610Sstevel@tonic-gate /*
162852Svi117747  * Hooks for Sun Cluster. On non-clustered nodes these will remain NULL.
163852Svi117747  * PSARC/2005/602.
164852Svi117747  */
165852Svi117747 void (*cl_sctp_listen)(sa_family_t, uchar_t *, uint_t, in_port_t) = NULL;
166852Svi117747 void (*cl_sctp_unlisten)(sa_family_t, uchar_t *, uint_t, in_port_t) = NULL;
167852Svi117747 void (*cl_sctp_connect)(sa_family_t, uchar_t *, uint_t, in_port_t,
168852Svi117747     uchar_t *, uint_t, in_port_t, boolean_t, cl_sctp_handle_t) = NULL;
169852Svi117747 void (*cl_sctp_disconnect)(sa_family_t, cl_sctp_handle_t) = NULL;
170852Svi117747 void (*cl_sctp_assoc_change)(sa_family_t, uchar_t *, size_t, uint_t,
171852Svi117747     uchar_t *, size_t, uint_t, int, cl_sctp_handle_t) = NULL;
172852Svi117747 void (*cl_sctp_check_addrs)(sa_family_t, in_port_t, uchar_t **, size_t,
173852Svi117747     uint_t *, boolean_t) = NULL;
174852Svi117747 /*
1750Sstevel@tonic-gate  * Return the version number of the SCTP kernel interface.
1760Sstevel@tonic-gate  */
1770Sstevel@tonic-gate int
1780Sstevel@tonic-gate sctp_itf_ver(int cl_ver)
1790Sstevel@tonic-gate {
1800Sstevel@tonic-gate 	if (cl_ver != SCTP_ITF_VER)
1810Sstevel@tonic-gate 		return (-1);
1820Sstevel@tonic-gate 	return (SCTP_ITF_VER);
1830Sstevel@tonic-gate }
1840Sstevel@tonic-gate 
1850Sstevel@tonic-gate /*
1860Sstevel@tonic-gate  * Called when we need a new sctp instantiation but don't really have a
1870Sstevel@tonic-gate  * new q to hang it off of. Copy the priv flag from the passed in structure.
1880Sstevel@tonic-gate  */
1890Sstevel@tonic-gate sctp_t *
1900Sstevel@tonic-gate sctp_create_eager(sctp_t *psctp)
1910Sstevel@tonic-gate {
1920Sstevel@tonic-gate 	sctp_t	*sctp;
1930Sstevel@tonic-gate 	mblk_t	*ack_mp, *hb_mp;
1940Sstevel@tonic-gate 	conn_t	*connp, *pconnp;
1950Sstevel@tonic-gate 
1960Sstevel@tonic-gate 	if ((connp = ipcl_conn_create(IPCL_SCTPCONN, KM_NOSLEEP)) == NULL)
1970Sstevel@tonic-gate 		return (NULL);
1980Sstevel@tonic-gate 	sctp = CONN2SCTP(connp);
1990Sstevel@tonic-gate 
2000Sstevel@tonic-gate 	if ((ack_mp = sctp_timer_alloc(sctp, sctp_ack_timer)) == NULL ||
2010Sstevel@tonic-gate 	    (hb_mp = sctp_timer_alloc(sctp, sctp_heartbeat_timer)) == NULL) {
2020Sstevel@tonic-gate 		if (ack_mp != NULL)
2030Sstevel@tonic-gate 			freeb(ack_mp);
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);
2210Sstevel@tonic-gate 		kmem_cache_free(sctp_conn_cache, connp);
2220Sstevel@tonic-gate 		return (NULL);
2230Sstevel@tonic-gate 	}
2240Sstevel@tonic-gate 	if (pconnp->conn_cred != NULL) {
2250Sstevel@tonic-gate 		connp->conn_cred = pconnp->conn_cred;
2260Sstevel@tonic-gate 		crhold(connp->conn_cred);
2270Sstevel@tonic-gate 	}
2280Sstevel@tonic-gate 	connp->conn_zoneid = psctp->sctp_zoneid;
2290Sstevel@tonic-gate 	sctp->sctp_mss = psctp->sctp_mss;
2300Sstevel@tonic-gate 	sctp->sctp_detached = B_TRUE;
2310Sstevel@tonic-gate 	/*
2320Sstevel@tonic-gate 	 * Link to the global as soon as possible so that this sctp_t
2330Sstevel@tonic-gate 	 * can be found.
2340Sstevel@tonic-gate 	 */
2350Sstevel@tonic-gate 	SCTP_LINK(sctp);
2360Sstevel@tonic-gate 
2370Sstevel@tonic-gate 	return (sctp);
2380Sstevel@tonic-gate }
2390Sstevel@tonic-gate 
2400Sstevel@tonic-gate /*
2410Sstevel@tonic-gate  * We are dying for some reason.  Try to do it gracefully.
2420Sstevel@tonic-gate  */
2430Sstevel@tonic-gate void
2440Sstevel@tonic-gate sctp_clean_death(sctp_t *sctp, int err)
2450Sstevel@tonic-gate {
2460Sstevel@tonic-gate 	ASSERT(sctp != NULL);
2470Sstevel@tonic-gate 	ASSERT((sctp->sctp_family == AF_INET &&
2480Sstevel@tonic-gate 	    sctp->sctp_ipversion == IPV4_VERSION) ||
2490Sstevel@tonic-gate 	    (sctp->sctp_family == AF_INET6 &&
2500Sstevel@tonic-gate 	    (sctp->sctp_ipversion == IPV4_VERSION ||
2510Sstevel@tonic-gate 	    sctp->sctp_ipversion == IPV6_VERSION)));
2520Sstevel@tonic-gate 
2530Sstevel@tonic-gate 	dprint(3, ("sctp_clean_death %p, state %d\n", sctp, sctp->sctp_state));
2540Sstevel@tonic-gate 
2550Sstevel@tonic-gate 	sctp->sctp_client_errno = err;
2560Sstevel@tonic-gate 	/*
2570Sstevel@tonic-gate 	 * Check to see if we need to notify upper layer.
2580Sstevel@tonic-gate 	 */
2590Sstevel@tonic-gate 	if ((sctp->sctp_state >= SCTPS_COOKIE_WAIT) &&
2600Sstevel@tonic-gate 	    !SCTP_IS_DETACHED(sctp)) {
2610Sstevel@tonic-gate 		if (sctp->sctp_xmit_head || sctp->sctp_xmit_unsent) {
2620Sstevel@tonic-gate 			sctp_regift_xmitlist(sctp);
2630Sstevel@tonic-gate 		}
2640Sstevel@tonic-gate 		if (sctp->sctp_ulp_disconnected(sctp->sctp_ulpd, err)) {
2650Sstevel@tonic-gate 			/*
2660Sstevel@tonic-gate 			 * Socket is gone, detach.
2670Sstevel@tonic-gate 			 */
2680Sstevel@tonic-gate 			sctp->sctp_detached = B_TRUE;
2690Sstevel@tonic-gate 			sctp->sctp_ulpd = NULL;
2700Sstevel@tonic-gate 			bzero(&sctp->sctp_upcalls, sizeof (sctp_upcalls_t));
2710Sstevel@tonic-gate 		}
2720Sstevel@tonic-gate 	}
2730Sstevel@tonic-gate 
2740Sstevel@tonic-gate 	/* Remove this sctp from all hashes. */
2750Sstevel@tonic-gate 	sctp_closei_local(sctp);
2760Sstevel@tonic-gate 
2770Sstevel@tonic-gate 	/*
2780Sstevel@tonic-gate 	 * If the sctp_t is detached, we need to finish freeing up
2790Sstevel@tonic-gate 	 * the resources.  At this point, ip_fanout_sctp() should have
2800Sstevel@tonic-gate 	 * a hold on this sctp_t.  Some thread doing snmp stuff can
2810Sstevel@tonic-gate 	 * have a hold.  And a taskq can also have a hold waiting to
2820Sstevel@tonic-gate 	 * work.  sctp_unlink() the sctp_t from the global list so
2830Sstevel@tonic-gate 	 * that no new thread can find it.  Then do a SCTP_REFRELE().
2840Sstevel@tonic-gate 	 * The sctp_t will be freed after all those threads are done.
2850Sstevel@tonic-gate 	 */
2860Sstevel@tonic-gate 	if (SCTP_IS_DETACHED(sctp)) {
2870Sstevel@tonic-gate 		SCTP_CONDEMNED(sctp);
2880Sstevel@tonic-gate 		SCTP_REFRELE(sctp);
2890Sstevel@tonic-gate 	}
2900Sstevel@tonic-gate }
2910Sstevel@tonic-gate 
2920Sstevel@tonic-gate /*
2930Sstevel@tonic-gate  * Called by upper layer when it wants to close this association.
2940Sstevel@tonic-gate  * Depending on the state of this assoication, we need to do
2950Sstevel@tonic-gate  * different things.
2960Sstevel@tonic-gate  *
2970Sstevel@tonic-gate  * If the state is below COOKIE_ECHOED or it is COOKIE_ECHOED but with
2980Sstevel@tonic-gate  * no sent data, just remove this sctp from all the hashes.  This
2990Sstevel@tonic-gate  * makes sure that all packets from the other end will go to the default
3000Sstevel@tonic-gate  * sctp handling.  The upper layer will then do a sctp_close() to clean
3010Sstevel@tonic-gate  * up.
3020Sstevel@tonic-gate  *
3030Sstevel@tonic-gate  * Otherwise, check and see if SO_LINGER is set.  If it is set, check
3040Sstevel@tonic-gate  * the value.  If the value is 0, consider this an abortive close.  Send
3050Sstevel@tonic-gate  * an ABORT message and kill the associatiion.
3060Sstevel@tonic-gate  *
3070Sstevel@tonic-gate  */
3080Sstevel@tonic-gate int
3090Sstevel@tonic-gate sctp_disconnect(sctp_t *sctp)
3100Sstevel@tonic-gate {
3110Sstevel@tonic-gate 	int	error = 0;
3120Sstevel@tonic-gate 	sctp_faddr_t *fp;
3130Sstevel@tonic-gate 
3140Sstevel@tonic-gate 	dprint(3, ("sctp_disconnect %p, state %d\n", sctp, sctp->sctp_state));
3150Sstevel@tonic-gate 
3160Sstevel@tonic-gate 	RUN_SCTP(sctp);
3170Sstevel@tonic-gate 
3180Sstevel@tonic-gate 	switch (sctp->sctp_state) {
3190Sstevel@tonic-gate 	case SCTPS_IDLE:
3200Sstevel@tonic-gate 	case SCTPS_BOUND:
3210Sstevel@tonic-gate 	case SCTPS_LISTEN:
3220Sstevel@tonic-gate 		break;
3230Sstevel@tonic-gate 	case SCTPS_COOKIE_WAIT:
3240Sstevel@tonic-gate 	case SCTPS_COOKIE_ECHOED:
3250Sstevel@tonic-gate 		/*
3260Sstevel@tonic-gate 		 * Close during the connect 3-way handshake
3270Sstevel@tonic-gate 		 * but here there may or may not be pending data
3280Sstevel@tonic-gate 		 * already on queue. Process almost same as in
3290Sstevel@tonic-gate 		 * the ESTABLISHED state.
3300Sstevel@tonic-gate 		 */
3310Sstevel@tonic-gate 		if (sctp->sctp_xmit_head == NULL &&
3320Sstevel@tonic-gate 		    sctp->sctp_xmit_unsent == NULL) {
3330Sstevel@tonic-gate 			break;
3340Sstevel@tonic-gate 		}
3350Sstevel@tonic-gate 		/* FALLTHRU */
3360Sstevel@tonic-gate 	default:
3370Sstevel@tonic-gate 		/*
3380Sstevel@tonic-gate 		 * If SO_LINGER has set a zero linger time, abort the
3390Sstevel@tonic-gate 		 * connection with a reset.
3400Sstevel@tonic-gate 		 */
3410Sstevel@tonic-gate 		if (sctp->sctp_linger && sctp->sctp_lingertime == 0) {
3420Sstevel@tonic-gate 			sctp_user_abort(sctp, NULL, B_FALSE);
3430Sstevel@tonic-gate 			break;
3440Sstevel@tonic-gate 		}
3450Sstevel@tonic-gate 
3460Sstevel@tonic-gate 		/*
347852Svi117747 		 * In there is unread data, send an ABORT
348852Svi117747 		 */
349852Svi117747 		if (sctp->sctp_rxqueued > 0 || sctp->sctp_irwnd >
350852Svi117747 		    sctp->sctp_rwnd) {
351852Svi117747 			sctp_user_abort(sctp, NULL, B_FALSE);
352852Svi117747 			break;
353852Svi117747 		}
354852Svi117747 		/*
3550Sstevel@tonic-gate 		 * Transmit the shutdown before detaching the sctp_t.
3560Sstevel@tonic-gate 		 * After sctp_detach returns this queue/perimeter
3570Sstevel@tonic-gate 		 * no longer owns the sctp_t thus others can modify it.
3580Sstevel@tonic-gate 		 */
3590Sstevel@tonic-gate 		sctp_send_shutdown(sctp, 0);
3600Sstevel@tonic-gate 
3610Sstevel@tonic-gate 		/* Pass gathered wisdom to IP for keeping */
3620Sstevel@tonic-gate 		for (fp = sctp->sctp_faddrs; fp != NULL; fp = fp->next)
3630Sstevel@tonic-gate 			sctp_faddr2ire(sctp, fp);
3640Sstevel@tonic-gate 
3650Sstevel@tonic-gate 		/*
3660Sstevel@tonic-gate 		 * If lingering on close then wait until the shutdown
3670Sstevel@tonic-gate 		 * is complete, or the SO_LINGER time passes, or an
3680Sstevel@tonic-gate 		 * ABORT is sent/received.  Note that sctp_disconnect()
3690Sstevel@tonic-gate 		 * can be called more than once.  Make sure that only
3700Sstevel@tonic-gate 		 * one thread waits.
3710Sstevel@tonic-gate 		 */
3720Sstevel@tonic-gate 		if (sctp->sctp_linger && sctp->sctp_lingertime > 0 &&
3730Sstevel@tonic-gate 		    sctp->sctp_state >= SCTPS_ESTABLISHED &&
3740Sstevel@tonic-gate 		    !sctp->sctp_lingering) {
3750Sstevel@tonic-gate 			clock_t stoptime;	/* in ticks */
3760Sstevel@tonic-gate 			clock_t ret;
3770Sstevel@tonic-gate 
3780Sstevel@tonic-gate 			/*
3790Sstevel@tonic-gate 			 * Process the sendq to send the SHUTDOWN out
3800Sstevel@tonic-gate 			 * before waiting.
3810Sstevel@tonic-gate 			 */
3820Sstevel@tonic-gate 			sctp_process_sendq(sctp);
3830Sstevel@tonic-gate 
3840Sstevel@tonic-gate 			sctp->sctp_lingering = 1;
3850Sstevel@tonic-gate 			sctp->sctp_client_errno = 0;
3860Sstevel@tonic-gate 			stoptime = lbolt + sctp->sctp_lingertime;
3870Sstevel@tonic-gate 
3880Sstevel@tonic-gate 			mutex_enter(&sctp->sctp_lock);
3890Sstevel@tonic-gate 			sctp->sctp_running = B_FALSE;
3900Sstevel@tonic-gate 			while (sctp->sctp_state >= SCTPS_ESTABLISHED &&
3910Sstevel@tonic-gate 			    sctp->sctp_client_errno == 0) {
3920Sstevel@tonic-gate 				cv_broadcast(&sctp->sctp_cv);
3930Sstevel@tonic-gate 				ret = cv_timedwait_sig(&sctp->sctp_cv,
3940Sstevel@tonic-gate 				    &sctp->sctp_lock, stoptime);
3950Sstevel@tonic-gate 				if (ret < 0) {
3960Sstevel@tonic-gate 					/* Stoptime has reached. */
3970Sstevel@tonic-gate 					sctp->sctp_client_errno = EWOULDBLOCK;
3980Sstevel@tonic-gate 					break;
3990Sstevel@tonic-gate 				} else if (ret == 0) {
4000Sstevel@tonic-gate 					/* Got a signal. */
4010Sstevel@tonic-gate 					break;
4020Sstevel@tonic-gate 				}
4030Sstevel@tonic-gate 			}
4040Sstevel@tonic-gate 			error = sctp->sctp_client_errno;
4050Sstevel@tonic-gate 			sctp->sctp_client_errno = 0;
4060Sstevel@tonic-gate 			mutex_exit(&sctp->sctp_lock);
4070Sstevel@tonic-gate 		}
4080Sstevel@tonic-gate 
4090Sstevel@tonic-gate 		WAKE_SCTP(sctp);
4100Sstevel@tonic-gate 		sctp_process_sendq(sctp);
4110Sstevel@tonic-gate 		return (error);
4120Sstevel@tonic-gate 	}
4130Sstevel@tonic-gate 
4140Sstevel@tonic-gate 
4150Sstevel@tonic-gate 	/* Remove this sctp from all hashes so nobody can find it. */
4160Sstevel@tonic-gate 	sctp_closei_local(sctp);
4170Sstevel@tonic-gate 	WAKE_SCTP(sctp);
4180Sstevel@tonic-gate 	return (error);
4190Sstevel@tonic-gate }
4200Sstevel@tonic-gate 
4210Sstevel@tonic-gate void
4220Sstevel@tonic-gate sctp_close(sctp_t *sctp)
4230Sstevel@tonic-gate {
4240Sstevel@tonic-gate 	dprint(3, ("sctp_close %p, state %d\n", sctp, sctp->sctp_state));
4250Sstevel@tonic-gate 
4260Sstevel@tonic-gate 	RUN_SCTP(sctp);
4270Sstevel@tonic-gate 	sctp->sctp_detached = 1;
4280Sstevel@tonic-gate 	sctp->sctp_ulpd = NULL;
4290Sstevel@tonic-gate 	bzero(&sctp->sctp_upcalls, sizeof (sctp_upcalls_t));
4300Sstevel@tonic-gate 	bzero(&sctp->sctp_events, sizeof (sctp->sctp_events));
4310Sstevel@tonic-gate 
4320Sstevel@tonic-gate 	/* If the graceful shutdown has not been completed, just return. */
4330Sstevel@tonic-gate 	if (sctp->sctp_state != SCTPS_IDLE) {
4340Sstevel@tonic-gate 		WAKE_SCTP(sctp);
4350Sstevel@tonic-gate 		return;
4360Sstevel@tonic-gate 	}
4370Sstevel@tonic-gate 
4380Sstevel@tonic-gate 	/*
4390Sstevel@tonic-gate 	 * Since sctp_t is in SCTPS_IDLE state, so the only thread which
4400Sstevel@tonic-gate 	 * can have a hold on the sctp_t is doing snmp stuff.  Just do
4410Sstevel@tonic-gate 	 * a SCTP_REFRELE() here after the SCTP_UNLINK().  It will
4420Sstevel@tonic-gate 	 * be freed when the other thread is done.
4430Sstevel@tonic-gate 	 */
4440Sstevel@tonic-gate 	SCTP_CONDEMNED(sctp);
4450Sstevel@tonic-gate 	WAKE_SCTP(sctp);
4460Sstevel@tonic-gate 	SCTP_REFRELE(sctp);
4470Sstevel@tonic-gate }
4480Sstevel@tonic-gate 
4490Sstevel@tonic-gate /*
4500Sstevel@tonic-gate  * Unlink from global list and do the eager close.
4510Sstevel@tonic-gate  * Remove the refhold implicit in being on the global list.
4520Sstevel@tonic-gate  */
4530Sstevel@tonic-gate void
4540Sstevel@tonic-gate sctp_close_eager(sctp_t *sctp)
4550Sstevel@tonic-gate {
4560Sstevel@tonic-gate 	SCTP_CONDEMNED(sctp);
4570Sstevel@tonic-gate 	sctp_closei_local(sctp);
4580Sstevel@tonic-gate 	SCTP_REFRELE(sctp);
4590Sstevel@tonic-gate }
4600Sstevel@tonic-gate 
4610Sstevel@tonic-gate /*
4620Sstevel@tonic-gate  * The sctp_t is going away. Remove it from all lists and set it
4630Sstevel@tonic-gate  * to SCTPS_IDLE. The caller has to remove it from the
4640Sstevel@tonic-gate  * global list. The freeing up of memory is deferred until
4650Sstevel@tonic-gate  * sctp_free(). This is needed since a thread in sctp_input() might have
4660Sstevel@tonic-gate  * done a SCTP_REFHOLD on this structure before it was removed from the
4670Sstevel@tonic-gate  * hashes.
4680Sstevel@tonic-gate  */
4690Sstevel@tonic-gate static void
4700Sstevel@tonic-gate sctp_closei_local(sctp_t *sctp)
4710Sstevel@tonic-gate {
4720Sstevel@tonic-gate 	mblk_t	*mp;
4730Sstevel@tonic-gate 	ire_t	*ire = NULL;
4740Sstevel@tonic-gate 	conn_t	*connp = sctp->sctp_connp;
4750Sstevel@tonic-gate 
4760Sstevel@tonic-gate 	/* Stop and free the timers */
4770Sstevel@tonic-gate 	sctp_free_faddr_timers(sctp);
4780Sstevel@tonic-gate 	if ((mp = sctp->sctp_heartbeat_mp) != NULL) {
4790Sstevel@tonic-gate 		sctp_timer_free(mp);
4800Sstevel@tonic-gate 		sctp->sctp_heartbeat_mp = NULL;
4810Sstevel@tonic-gate 	}
4820Sstevel@tonic-gate 	if ((mp = sctp->sctp_ack_mp) != NULL) {
4830Sstevel@tonic-gate 		sctp_timer_free(mp);
4840Sstevel@tonic-gate 		sctp->sctp_ack_mp = NULL;
4850Sstevel@tonic-gate 	}
4860Sstevel@tonic-gate 
4870Sstevel@tonic-gate 	/* Set the CONN_CLOSING flag so that IP will not cache IRE again. */
4880Sstevel@tonic-gate 	mutex_enter(&connp->conn_lock);
4890Sstevel@tonic-gate 	connp->conn_state_flags |= CONN_CLOSING;
4900Sstevel@tonic-gate 	ire = connp->conn_ire_cache;
4910Sstevel@tonic-gate 	connp->conn_ire_cache = NULL;
4920Sstevel@tonic-gate 	mutex_exit(&connp->conn_lock);
4930Sstevel@tonic-gate 	if (ire != NULL)
4940Sstevel@tonic-gate 		IRE_REFRELE_NOTR(ire);
4950Sstevel@tonic-gate 
4960Sstevel@tonic-gate 	/* Remove from all hashes. */
4970Sstevel@tonic-gate 	sctp_bind_hash_remove(sctp);
4980Sstevel@tonic-gate 	sctp_conn_hash_remove(sctp);
4990Sstevel@tonic-gate 	sctp_listen_hash_remove(sctp);
5000Sstevel@tonic-gate 	sctp->sctp_state = SCTPS_IDLE;
5010Sstevel@tonic-gate 
5020Sstevel@tonic-gate 	/*
5030Sstevel@tonic-gate 	 * Clean up the recvq as much as possible.  All those packets
5040Sstevel@tonic-gate 	 * will be silently dropped as this sctp_t is now in idle state.
5050Sstevel@tonic-gate 	 */
5060Sstevel@tonic-gate 	mutex_enter(&sctp->sctp_recvq_lock);
5070Sstevel@tonic-gate 	while ((mp = sctp->sctp_recvq) != NULL) {
5080Sstevel@tonic-gate 		mblk_t *ipsec_mp;
5090Sstevel@tonic-gate 
5100Sstevel@tonic-gate 		sctp->sctp_recvq = mp->b_next;
5110Sstevel@tonic-gate 		mp->b_next = NULL;
5120Sstevel@tonic-gate 		if ((ipsec_mp = mp->b_prev) != NULL) {
5130Sstevel@tonic-gate 			freeb(ipsec_mp);
5140Sstevel@tonic-gate 			mp->b_prev = NULL;
5150Sstevel@tonic-gate 		}
5160Sstevel@tonic-gate 		freemsg(mp);
5170Sstevel@tonic-gate 	}
5180Sstevel@tonic-gate 	mutex_exit(&sctp->sctp_recvq_lock);
5190Sstevel@tonic-gate }
5200Sstevel@tonic-gate 
5210Sstevel@tonic-gate /*
5220Sstevel@tonic-gate  * Free memory associated with the sctp/ip header template.
5230Sstevel@tonic-gate  */
5240Sstevel@tonic-gate static void
5250Sstevel@tonic-gate sctp_headers_free(sctp_t *sctp)
5260Sstevel@tonic-gate {
5270Sstevel@tonic-gate 	if (sctp->sctp_iphc != NULL) {
5280Sstevel@tonic-gate 		kmem_free(sctp->sctp_iphc, sctp->sctp_iphc_len);
5290Sstevel@tonic-gate 		sctp->sctp_iphc = NULL;
5300Sstevel@tonic-gate 		sctp->sctp_ipha = NULL;
5310Sstevel@tonic-gate 		sctp->sctp_hdr_len = 0;
5320Sstevel@tonic-gate 		sctp->sctp_ip_hdr_len = 0;
5330Sstevel@tonic-gate 		sctp->sctp_iphc_len = 0;
5340Sstevel@tonic-gate 		sctp->sctp_sctph = NULL;
5350Sstevel@tonic-gate 		sctp->sctp_hdr_len = 0;
5360Sstevel@tonic-gate 	}
5370Sstevel@tonic-gate 	if (sctp->sctp_iphc6 != NULL) {
5380Sstevel@tonic-gate 		kmem_free(sctp->sctp_iphc6, sctp->sctp_iphc6_len);
5390Sstevel@tonic-gate 		sctp->sctp_iphc6 = NULL;
5400Sstevel@tonic-gate 		sctp->sctp_ip6h = NULL;
5410Sstevel@tonic-gate 		sctp->sctp_hdr6_len = 0;
5420Sstevel@tonic-gate 		sctp->sctp_ip_hdr6_len = 0;
5430Sstevel@tonic-gate 		sctp->sctp_iphc6_len = 0;
5440Sstevel@tonic-gate 		sctp->sctp_sctph6 = NULL;
5450Sstevel@tonic-gate 		sctp->sctp_hdr6_len = 0;
5460Sstevel@tonic-gate 	}
5470Sstevel@tonic-gate }
5480Sstevel@tonic-gate 
5490Sstevel@tonic-gate static void
5500Sstevel@tonic-gate sctp_free_xmit_data(sctp_t *sctp)
5510Sstevel@tonic-gate {
5520Sstevel@tonic-gate 	mblk_t	*ump = NULL;
5530Sstevel@tonic-gate 	mblk_t	*nump;
5540Sstevel@tonic-gate 	mblk_t	*mp;
5550Sstevel@tonic-gate 	mblk_t	*nmp;
5560Sstevel@tonic-gate 
5570Sstevel@tonic-gate 	sctp->sctp_xmit_unacked = NULL;
5580Sstevel@tonic-gate 	ump = sctp->sctp_xmit_head;
5590Sstevel@tonic-gate 	sctp->sctp_xmit_tail = sctp->sctp_xmit_head = NULL;
5600Sstevel@tonic-gate free_unsent:
5610Sstevel@tonic-gate 	for (; ump != NULL; ump = nump) {
5620Sstevel@tonic-gate 		for (mp = ump->b_cont; mp != NULL; mp = nmp) {
5630Sstevel@tonic-gate 			nmp = mp->b_next;
5640Sstevel@tonic-gate 			mp->b_next = NULL;
5650Sstevel@tonic-gate 			mp->b_prev = NULL;
5660Sstevel@tonic-gate 			freemsg(mp);
5670Sstevel@tonic-gate 		}
5680Sstevel@tonic-gate 		ASSERT(DB_REF(ump) == 1);
5690Sstevel@tonic-gate 		nump = ump->b_next;
5700Sstevel@tonic-gate 		ump->b_next = NULL;
5710Sstevel@tonic-gate 		ump->b_prev = NULL;
5720Sstevel@tonic-gate 		ump->b_cont = NULL;
5730Sstevel@tonic-gate 		freeb(ump);
5740Sstevel@tonic-gate 	}
5750Sstevel@tonic-gate 	if ((ump = sctp->sctp_xmit_unsent) == NULL) {
5760Sstevel@tonic-gate 		ASSERT(sctp->sctp_xmit_unsent_tail == NULL);
5770Sstevel@tonic-gate 		return;
5780Sstevel@tonic-gate 	}
5790Sstevel@tonic-gate 	sctp->sctp_xmit_unsent = sctp->sctp_xmit_unsent_tail = NULL;
5800Sstevel@tonic-gate 	goto free_unsent;
5810Sstevel@tonic-gate }
5820Sstevel@tonic-gate 
5830Sstevel@tonic-gate /*
5840Sstevel@tonic-gate  * Cleanup all the messages in the stream queue and the reassembly lists.
5850Sstevel@tonic-gate  * If 'free' is true, then delete the streams as well.
5860Sstevel@tonic-gate  */
5870Sstevel@tonic-gate void
5880Sstevel@tonic-gate sctp_instream_cleanup(sctp_t *sctp, boolean_t free)
5890Sstevel@tonic-gate {
5900Sstevel@tonic-gate 	int	i;
5910Sstevel@tonic-gate 	mblk_t	*mp;
5920Sstevel@tonic-gate 	mblk_t	*mp1;
5930Sstevel@tonic-gate 
5940Sstevel@tonic-gate 	if (sctp->sctp_instr != NULL) {
5950Sstevel@tonic-gate 		/* walk thru and flush out anything remaining in the Q */
5960Sstevel@tonic-gate 		for (i = 0; i < sctp->sctp_num_istr; i++) {
5970Sstevel@tonic-gate 			mp = sctp->sctp_instr[i].istr_msgs;
5980Sstevel@tonic-gate 			while (mp != NULL) {
5990Sstevel@tonic-gate 				mp1 = mp->b_next;
6000Sstevel@tonic-gate 				mp->b_next = mp->b_prev = NULL;
6010Sstevel@tonic-gate 				freemsg(mp);
6020Sstevel@tonic-gate 				mp = mp1;
6030Sstevel@tonic-gate 			}
6040Sstevel@tonic-gate 			sctp->sctp_instr[i].istr_msgs = NULL;
6050Sstevel@tonic-gate 			sctp_free_reass((sctp->sctp_instr) + i);
6060Sstevel@tonic-gate 			sctp->sctp_instr[i].nextseq = 0;
6070Sstevel@tonic-gate 		}
6080Sstevel@tonic-gate 		if (free) {
6090Sstevel@tonic-gate 			kmem_free(sctp->sctp_instr,
6100Sstevel@tonic-gate 			    sizeof (*sctp->sctp_instr) * sctp->sctp_num_istr);
6110Sstevel@tonic-gate 			sctp->sctp_instr = NULL;
6120Sstevel@tonic-gate 			sctp->sctp_num_istr = 0;
6130Sstevel@tonic-gate 		}
6140Sstevel@tonic-gate 	}
6150Sstevel@tonic-gate 	/* un-ordered fragments */
6160Sstevel@tonic-gate 	if (sctp->sctp_uo_frags != NULL) {
6170Sstevel@tonic-gate 		for (mp = sctp->sctp_uo_frags; mp != NULL; mp = mp1) {
6180Sstevel@tonic-gate 			mp1 = mp->b_next;
6190Sstevel@tonic-gate 			mp->b_next = mp->b_prev = NULL;
6200Sstevel@tonic-gate 			freemsg(mp);
6210Sstevel@tonic-gate 		}
6220Sstevel@tonic-gate 	}
6230Sstevel@tonic-gate }
6240Sstevel@tonic-gate 
6250Sstevel@tonic-gate /*
6260Sstevel@tonic-gate  * Last reference to the sctp_t is gone. Free all memory associated with it.
6270Sstevel@tonic-gate  * Called from SCTP_REFRELE. Called inline in sctp_close()
6280Sstevel@tonic-gate  */
6290Sstevel@tonic-gate void
6300Sstevel@tonic-gate sctp_free(conn_t *connp)
6310Sstevel@tonic-gate {
6320Sstevel@tonic-gate 	sctp_t *sctp = CONN2SCTP(connp);
6330Sstevel@tonic-gate 	ip6_pkt_t	*ipp;
6340Sstevel@tonic-gate 	int		cnt;
6350Sstevel@tonic-gate 
6360Sstevel@tonic-gate 	/* Unlink it from the global list */
6370Sstevel@tonic-gate 	SCTP_UNLINK(sctp);
6380Sstevel@tonic-gate 
6390Sstevel@tonic-gate 	ASSERT(connp->conn_ref == 0);
6400Sstevel@tonic-gate 	ASSERT(connp->conn_ulp == IPPROTO_SCTP);
6410Sstevel@tonic-gate 	ASSERT(!MUTEX_HELD(&sctp->sctp_reflock));
6420Sstevel@tonic-gate 	ASSERT(sctp->sctp_refcnt == 0);
6430Sstevel@tonic-gate 
6440Sstevel@tonic-gate 	ASSERT(sctp->sctp_ptpbhn == NULL && sctp->sctp_bind_hash == NULL);
6450Sstevel@tonic-gate 	ASSERT(sctp->sctp_conn_hash_next == NULL &&
6460Sstevel@tonic-gate 	    sctp->sctp_conn_hash_prev == NULL);
6470Sstevel@tonic-gate 
6480Sstevel@tonic-gate 
6490Sstevel@tonic-gate 	/* Free up all the resources. */
6500Sstevel@tonic-gate 
6510Sstevel@tonic-gate 	/* blow away sctp stream management */
6520Sstevel@tonic-gate 	if (sctp->sctp_ostrcntrs != NULL) {
6530Sstevel@tonic-gate 		kmem_free(sctp->sctp_ostrcntrs,
6540Sstevel@tonic-gate 		    sizeof (uint16_t) * sctp->sctp_num_ostr);
6550Sstevel@tonic-gate 		sctp->sctp_ostrcntrs = NULL;
6560Sstevel@tonic-gate 	}
6570Sstevel@tonic-gate 	sctp_instream_cleanup(sctp, B_TRUE);
6580Sstevel@tonic-gate 
6590Sstevel@tonic-gate 	/* Remove all data transfer resources. */
6600Sstevel@tonic-gate 	sctp->sctp_istr_nmsgs = 0;
6610Sstevel@tonic-gate 	sctp->sctp_rxqueued = 0;
6620Sstevel@tonic-gate 	sctp_free_xmit_data(sctp);
6630Sstevel@tonic-gate 	sctp->sctp_unacked = 0;
6640Sstevel@tonic-gate 	sctp->sctp_unsent = 0;
665852Svi117747 	if (sctp->sctp_cxmit_list != NULL)
666852Svi117747 		sctp_asconf_free_cxmit(sctp, NULL);
667852Svi117747 
6680Sstevel@tonic-gate 	sctp->sctp_lastdata = NULL;
6690Sstevel@tonic-gate 
6700Sstevel@tonic-gate 	/* Clear out default xmit settings */
6710Sstevel@tonic-gate 	sctp->sctp_def_stream = 0;
6720Sstevel@tonic-gate 	sctp->sctp_def_flags = 0;
6730Sstevel@tonic-gate 	sctp->sctp_def_ppid = 0;
6740Sstevel@tonic-gate 	sctp->sctp_def_context = 0;
6750Sstevel@tonic-gate 	sctp->sctp_def_timetolive = 0;
6760Sstevel@tonic-gate 
6770Sstevel@tonic-gate 	if (sctp->sctp_sack_info != NULL) {
6780Sstevel@tonic-gate 		sctp_free_set(sctp->sctp_sack_info);
6790Sstevel@tonic-gate 		sctp->sctp_sack_info = NULL;
6800Sstevel@tonic-gate 	}
6810Sstevel@tonic-gate 	sctp->sctp_sack_gaps = 0;
6820Sstevel@tonic-gate 
6830Sstevel@tonic-gate 	if (sctp->sctp_cookie_mp != NULL) {
6840Sstevel@tonic-gate 		freemsg(sctp->sctp_cookie_mp);
6850Sstevel@tonic-gate 		sctp->sctp_cookie_mp = NULL;
6860Sstevel@tonic-gate 	}
6870Sstevel@tonic-gate 
6880Sstevel@tonic-gate 	/* Remove all the address resources. */
6890Sstevel@tonic-gate 	sctp_zap_addrs(sctp);
6900Sstevel@tonic-gate 	for (cnt = 0; cnt < SCTP_IPIF_HASH; cnt++) {
6910Sstevel@tonic-gate 		ASSERT(sctp->sctp_saddrs[cnt].ipif_count == 0);
6920Sstevel@tonic-gate 		list_destroy(&sctp->sctp_saddrs[cnt].sctp_ipif_list);
6930Sstevel@tonic-gate 	}
6940Sstevel@tonic-gate 
6950Sstevel@tonic-gate 	ipp = &sctp->sctp_sticky_ipp;
6960Sstevel@tonic-gate 	if (ipp->ipp_rthdrlen != 0) {
6970Sstevel@tonic-gate 		kmem_free(ipp->ipp_rthdr, ipp->ipp_rthdrlen);
6980Sstevel@tonic-gate 		ipp->ipp_rthdrlen = 0;
6990Sstevel@tonic-gate 	}
7000Sstevel@tonic-gate 
7010Sstevel@tonic-gate 	if (ipp->ipp_dstoptslen != 0) {
7020Sstevel@tonic-gate 		kmem_free(ipp->ipp_dstopts, ipp->ipp_dstoptslen);
7030Sstevel@tonic-gate 		ipp->ipp_dstoptslen = 0;
7040Sstevel@tonic-gate 	}
7050Sstevel@tonic-gate 
7060Sstevel@tonic-gate 	if (ipp->ipp_rtdstoptslen != 0) {
7070Sstevel@tonic-gate 		kmem_free(ipp->ipp_rtdstopts, ipp->ipp_rtdstoptslen);
7080Sstevel@tonic-gate 		ipp->ipp_rtdstoptslen = 0;
7090Sstevel@tonic-gate 	}
7100Sstevel@tonic-gate 
7110Sstevel@tonic-gate 	if (ipp->ipp_hopoptslen != 0) {
7120Sstevel@tonic-gate 		kmem_free(ipp->ipp_hopopts, ipp->ipp_hopoptslen);
7130Sstevel@tonic-gate 		ipp->ipp_hopoptslen = 0;
7140Sstevel@tonic-gate 	}
7150Sstevel@tonic-gate 
7160Sstevel@tonic-gate 	if (sctp->sctp_hopopts != NULL) {
7170Sstevel@tonic-gate 		mi_free(sctp->sctp_hopopts);
7180Sstevel@tonic-gate 		sctp->sctp_hopopts = NULL;
7190Sstevel@tonic-gate 		sctp->sctp_hopoptslen = 0;
7200Sstevel@tonic-gate 	}
7210Sstevel@tonic-gate 	ASSERT(sctp->sctp_hopoptslen == 0);
7220Sstevel@tonic-gate 	if (sctp->sctp_dstopts != NULL) {
7230Sstevel@tonic-gate 		mi_free(sctp->sctp_dstopts);
7240Sstevel@tonic-gate 		sctp->sctp_dstopts = NULL;
7250Sstevel@tonic-gate 		sctp->sctp_dstoptslen = 0;
7260Sstevel@tonic-gate 	}
7270Sstevel@tonic-gate 	ASSERT(sctp->sctp_dstoptslen == 0);
7280Sstevel@tonic-gate 	if (sctp->sctp_rtdstopts != NULL) {
7290Sstevel@tonic-gate 		mi_free(sctp->sctp_rtdstopts);
7300Sstevel@tonic-gate 		sctp->sctp_rtdstopts = NULL;
7310Sstevel@tonic-gate 		sctp->sctp_rtdstoptslen = 0;
7320Sstevel@tonic-gate 	}
7330Sstevel@tonic-gate 	ASSERT(sctp->sctp_rtdstoptslen == 0);
7340Sstevel@tonic-gate 	if (sctp->sctp_rthdr != NULL) {
7350Sstevel@tonic-gate 		mi_free(sctp->sctp_rthdr);
7360Sstevel@tonic-gate 		sctp->sctp_rthdr = NULL;
7370Sstevel@tonic-gate 		sctp->sctp_rthdrlen = 0;
7380Sstevel@tonic-gate 	}
7390Sstevel@tonic-gate 	ASSERT(sctp->sctp_rthdrlen == 0);
7400Sstevel@tonic-gate 	sctp_headers_free(sctp);
7410Sstevel@tonic-gate 
7420Sstevel@tonic-gate 	sctp->sctp_shutdown_faddr = NULL;
7430Sstevel@tonic-gate 
7440Sstevel@tonic-gate 	/* Clear all the bitfields. */
7450Sstevel@tonic-gate 	bzero(&sctp->sctp_bits, sizeof (sctp->sctp_bits));
7460Sstevel@tonic-gate 
7470Sstevel@tonic-gate 	/* It is time to update the global statistics. */
7480Sstevel@tonic-gate 	UPDATE_MIB(&sctp_mib, sctpOutSCTPPkts, sctp->sctp_opkts);
7490Sstevel@tonic-gate 	UPDATE_MIB(&sctp_mib, sctpOutCtrlChunks, sctp->sctp_obchunks);
7500Sstevel@tonic-gate 	UPDATE_MIB(&sctp_mib, sctpOutOrderChunks, sctp->sctp_odchunks);
7510Sstevel@tonic-gate 	UPDATE_MIB(&sctp_mib, sctpOutUnorderChunks, sctp->sctp_oudchunks);
7520Sstevel@tonic-gate 	UPDATE_MIB(&sctp_mib, sctpRetransChunks, sctp->sctp_rxtchunks);
7530Sstevel@tonic-gate 	UPDATE_MIB(&sctp_mib, sctpInSCTPPkts, sctp->sctp_ipkts);
7540Sstevel@tonic-gate 	UPDATE_MIB(&sctp_mib, sctpInCtrlChunks, sctp->sctp_ibchunks);
7550Sstevel@tonic-gate 	UPDATE_MIB(&sctp_mib, sctpInOrderChunks, sctp->sctp_idchunks);
7560Sstevel@tonic-gate 	UPDATE_MIB(&sctp_mib, sctpInUnorderChunks, sctp->sctp_iudchunks);
7570Sstevel@tonic-gate 	UPDATE_MIB(&sctp_mib, sctpFragUsrMsgs, sctp->sctp_fragdmsgs);
7580Sstevel@tonic-gate 	UPDATE_MIB(&sctp_mib, sctpReasmUsrMsgs, sctp->sctp_reassmsgs);
7590Sstevel@tonic-gate 	sctp->sctp_opkts = 0;
7600Sstevel@tonic-gate 	sctp->sctp_obchunks = 0;
7610Sstevel@tonic-gate 	sctp->sctp_odchunks = 0;
7620Sstevel@tonic-gate 	sctp->sctp_oudchunks = 0;
7630Sstevel@tonic-gate 	sctp->sctp_rxtchunks = 0;
7640Sstevel@tonic-gate 	sctp->sctp_ipkts = 0;
7650Sstevel@tonic-gate 	sctp->sctp_ibchunks = 0;
7660Sstevel@tonic-gate 	sctp->sctp_idchunks = 0;
7670Sstevel@tonic-gate 	sctp->sctp_iudchunks = 0;
7680Sstevel@tonic-gate 	sctp->sctp_fragdmsgs = 0;
7690Sstevel@tonic-gate 	sctp->sctp_reassmsgs = 0;
7700Sstevel@tonic-gate 
7710Sstevel@tonic-gate 	sctp->sctp_autoclose = 0;
7720Sstevel@tonic-gate 	sctp->sctp_tx_adaption_code = 0;
7730Sstevel@tonic-gate 
7740Sstevel@tonic-gate 	/* Clean up conn_t stuff */
7750Sstevel@tonic-gate 	connp->conn_policy_cached = B_FALSE;
7760Sstevel@tonic-gate 	if (connp->conn_latch != NULL) {
7770Sstevel@tonic-gate 		IPLATCH_REFRELE(connp->conn_latch);
7780Sstevel@tonic-gate 		connp->conn_latch = NULL;
7790Sstevel@tonic-gate 	}
7800Sstevel@tonic-gate 	if (connp->conn_policy != NULL) {
7810Sstevel@tonic-gate 		IPPH_REFRELE(connp->conn_policy);
7820Sstevel@tonic-gate 		connp->conn_policy = NULL;
7830Sstevel@tonic-gate 	}
7840Sstevel@tonic-gate 	if (connp->conn_ipsec_opt_mp != NULL) {
7850Sstevel@tonic-gate 		freemsg(connp->conn_ipsec_opt_mp);
7860Sstevel@tonic-gate 		connp->conn_ipsec_opt_mp = NULL;
7870Sstevel@tonic-gate 	}
7880Sstevel@tonic-gate 	if (connp->conn_cred != NULL) {
7890Sstevel@tonic-gate 		crfree(connp->conn_cred);
7900Sstevel@tonic-gate 		connp->conn_cred = NULL;
7910Sstevel@tonic-gate 	}
7920Sstevel@tonic-gate 
7930Sstevel@tonic-gate 	kmem_cache_free(sctp_conn_cache, connp);
7940Sstevel@tonic-gate }
7950Sstevel@tonic-gate 
7960Sstevel@tonic-gate /* Diagnostic routine used to return a string associated with the sctp state. */
7970Sstevel@tonic-gate char *
7980Sstevel@tonic-gate sctp_display(sctp_t *sctp, char *sup_buf)
7990Sstevel@tonic-gate {
8000Sstevel@tonic-gate 	char	*buf;
8010Sstevel@tonic-gate 	char	buf1[30];
8020Sstevel@tonic-gate 	static char	priv_buf[INET6_ADDRSTRLEN * 2 + 80];
8030Sstevel@tonic-gate 	char	*cp;
8040Sstevel@tonic-gate 
8050Sstevel@tonic-gate 	if (sctp == NULL)
8060Sstevel@tonic-gate 		return ("NULL_SCTP");
8070Sstevel@tonic-gate 
8080Sstevel@tonic-gate 	buf = (sup_buf != NULL) ? sup_buf : priv_buf;
8090Sstevel@tonic-gate 
8100Sstevel@tonic-gate 	switch (sctp->sctp_state) {
8110Sstevel@tonic-gate 	case SCTPS_IDLE:
8120Sstevel@tonic-gate 		cp = "SCTP_IDLE";
8130Sstevel@tonic-gate 		break;
8140Sstevel@tonic-gate 	case SCTPS_BOUND:
8150Sstevel@tonic-gate 		cp = "SCTP_BOUND";
8160Sstevel@tonic-gate 		break;
8170Sstevel@tonic-gate 	case SCTPS_LISTEN:
8180Sstevel@tonic-gate 		cp = "SCTP_LISTEN";
8190Sstevel@tonic-gate 		break;
8200Sstevel@tonic-gate 	case SCTPS_COOKIE_WAIT:
8210Sstevel@tonic-gate 		cp = "SCTP_COOKIE_WAIT";
8220Sstevel@tonic-gate 		break;
8230Sstevel@tonic-gate 	case SCTPS_COOKIE_ECHOED:
8240Sstevel@tonic-gate 		cp = "SCTP_COOKIE_ECHOED";
8250Sstevel@tonic-gate 		break;
8260Sstevel@tonic-gate 	case SCTPS_ESTABLISHED:
8270Sstevel@tonic-gate 		cp = "SCTP_ESTABLISHED";
8280Sstevel@tonic-gate 		break;
8290Sstevel@tonic-gate 	case SCTPS_SHUTDOWN_PENDING:
8300Sstevel@tonic-gate 		cp = "SCTP_SHUTDOWN_PENDING";
8310Sstevel@tonic-gate 		break;
8320Sstevel@tonic-gate 	case SCTPS_SHUTDOWN_SENT:
8330Sstevel@tonic-gate 		cp = "SCTPS_SHUTDOWN_SENT";
8340Sstevel@tonic-gate 		break;
8350Sstevel@tonic-gate 	case SCTPS_SHUTDOWN_RECEIVED:
8360Sstevel@tonic-gate 		cp = "SCTPS_SHUTDOWN_RECEIVED";
8370Sstevel@tonic-gate 		break;
8380Sstevel@tonic-gate 	case SCTPS_SHUTDOWN_ACK_SENT:
8390Sstevel@tonic-gate 		cp = "SCTPS_SHUTDOWN_ACK_SENT";
8400Sstevel@tonic-gate 		break;
8410Sstevel@tonic-gate 	default:
8420Sstevel@tonic-gate 		(void) mi_sprintf(buf1, "SCTPUnkState(%d)", sctp->sctp_state);
8430Sstevel@tonic-gate 		cp = buf1;
8440Sstevel@tonic-gate 		break;
8450Sstevel@tonic-gate 	}
8460Sstevel@tonic-gate 	(void) mi_sprintf(buf, "[%u, %u] %s",
8470Sstevel@tonic-gate 	    ntohs(sctp->sctp_lport), ntohs(sctp->sctp_fport), cp);
8480Sstevel@tonic-gate 
8490Sstevel@tonic-gate 	return (buf);
8500Sstevel@tonic-gate }
8510Sstevel@tonic-gate 
8520Sstevel@tonic-gate /*
8530Sstevel@tonic-gate  * Initialize protocol control block. If a parent exists, inherit
8540Sstevel@tonic-gate  * all values set through setsockopt().
8550Sstevel@tonic-gate  */
8560Sstevel@tonic-gate static int
8570Sstevel@tonic-gate sctp_init_values(sctp_t *sctp, sctp_t *psctp, int sleep)
8580Sstevel@tonic-gate {
8590Sstevel@tonic-gate 	int	err;
8600Sstevel@tonic-gate 	int	cnt;
8610Sstevel@tonic-gate 
8620Sstevel@tonic-gate 	ASSERT((sctp->sctp_family == AF_INET &&
8630Sstevel@tonic-gate 	    sctp->sctp_ipversion == IPV4_VERSION) ||
8640Sstevel@tonic-gate 	    (sctp->sctp_family == AF_INET6 &&
8650Sstevel@tonic-gate 	    (sctp->sctp_ipversion == IPV4_VERSION ||
8660Sstevel@tonic-gate 	    sctp->sctp_ipversion == IPV6_VERSION)));
8670Sstevel@tonic-gate 
8680Sstevel@tonic-gate 	sctp->sctp_nsaddrs = 0;
8690Sstevel@tonic-gate 	for (cnt = 0; cnt < SCTP_IPIF_HASH; cnt++) {
8700Sstevel@tonic-gate 		sctp->sctp_saddrs[cnt].ipif_count = 0;
8710Sstevel@tonic-gate 		list_create(&sctp->sctp_saddrs[cnt].sctp_ipif_list,
8720Sstevel@tonic-gate 		    sizeof (sctp_saddr_ipif_t), offsetof(sctp_saddr_ipif_t,
8730Sstevel@tonic-gate 		    saddr_ipif));
8740Sstevel@tonic-gate 	}
8750Sstevel@tonic-gate 	sctp->sctp_ports = 0;
8760Sstevel@tonic-gate 	sctp->sctp_running = B_FALSE;
8770Sstevel@tonic-gate 	sctp->sctp_state = SCTPS_IDLE;
8780Sstevel@tonic-gate 
8790Sstevel@tonic-gate 	sctp->sctp_refcnt = 1;
8800Sstevel@tonic-gate 
8810Sstevel@tonic-gate 	sctp->sctp_strikes = 0;
8820Sstevel@tonic-gate 
8830Sstevel@tonic-gate 	sctp->sctp_last_mtu_probe = lbolt64;
8840Sstevel@tonic-gate 	sctp->sctp_mtu_probe_intvl = sctp_mtu_probe_interval;
8850Sstevel@tonic-gate 
8860Sstevel@tonic-gate 	sctp->sctp_sack_gaps = 0;
8870Sstevel@tonic-gate 	sctp->sctp_sack_toggle = 2;
8880Sstevel@tonic-gate 
8890Sstevel@tonic-gate 	if (psctp != NULL) {
8900Sstevel@tonic-gate 		/*
8910Sstevel@tonic-gate 		 * Inherit from parent
8920Sstevel@tonic-gate 		 */
8930Sstevel@tonic-gate 		sctp->sctp_iphc = kmem_zalloc(psctp->sctp_iphc_len,
8940Sstevel@tonic-gate 		    KM_NOSLEEP);
8950Sstevel@tonic-gate 		if (sctp->sctp_iphc == NULL)
8960Sstevel@tonic-gate 			return (ENOMEM);
8970Sstevel@tonic-gate 		sctp->sctp_iphc_len = psctp->sctp_iphc_len;
8980Sstevel@tonic-gate 		sctp->sctp_hdr_len = psctp->sctp_hdr_len;
8990Sstevel@tonic-gate 
9000Sstevel@tonic-gate 		sctp->sctp_iphc6 = kmem_zalloc(psctp->sctp_iphc6_len,
9010Sstevel@tonic-gate 		    KM_NOSLEEP);
9020Sstevel@tonic-gate 		if (sctp->sctp_iphc6 == NULL) {
9030Sstevel@tonic-gate 			sctp->sctp_iphc6_len = 0;
9040Sstevel@tonic-gate 			return (ENOMEM);
9050Sstevel@tonic-gate 		}
9060Sstevel@tonic-gate 		sctp->sctp_iphc6_len = psctp->sctp_iphc6_len;
9070Sstevel@tonic-gate 		sctp->sctp_hdr6_len = psctp->sctp_hdr6_len;
9080Sstevel@tonic-gate 
9090Sstevel@tonic-gate 		sctp->sctp_ip_hdr_len = psctp->sctp_ip_hdr_len;
9100Sstevel@tonic-gate 		sctp->sctp_ip_hdr6_len = psctp->sctp_ip_hdr6_len;
9110Sstevel@tonic-gate 
9120Sstevel@tonic-gate 		/*
9130Sstevel@tonic-gate 		 * Copy the IP+SCTP header templates from listener
9140Sstevel@tonic-gate 		 */
9150Sstevel@tonic-gate 		bcopy(psctp->sctp_iphc, sctp->sctp_iphc,
9160Sstevel@tonic-gate 		    psctp->sctp_hdr_len);
9170Sstevel@tonic-gate 		sctp->sctp_ipha = (ipha_t *)sctp->sctp_iphc;
9180Sstevel@tonic-gate 		sctp->sctp_sctph = (sctp_hdr_t *)(sctp->sctp_iphc +
9190Sstevel@tonic-gate 		    sctp->sctp_ip_hdr_len);
9200Sstevel@tonic-gate 
9210Sstevel@tonic-gate 		bcopy(psctp->sctp_iphc6, sctp->sctp_iphc6,
9220Sstevel@tonic-gate 		    psctp->sctp_hdr6_len);
9230Sstevel@tonic-gate 		if (((ip6i_t *)(sctp->sctp_iphc6))->ip6i_nxt == IPPROTO_RAW) {
9240Sstevel@tonic-gate 			sctp->sctp_ip6h = (ip6_t *)(sctp->sctp_iphc6 +
9250Sstevel@tonic-gate 			    sizeof (ip6i_t));
9260Sstevel@tonic-gate 		} else {
9270Sstevel@tonic-gate 			sctp->sctp_ip6h = (ip6_t *)sctp->sctp_iphc6;
9280Sstevel@tonic-gate 		}
9290Sstevel@tonic-gate 		sctp->sctp_sctph6 = (sctp_hdr_t *)(sctp->sctp_iphc6 +
9300Sstevel@tonic-gate 		    sctp->sctp_ip_hdr6_len);
9310Sstevel@tonic-gate 
9320Sstevel@tonic-gate 		sctp->sctp_cookie_lifetime = psctp->sctp_cookie_lifetime;
9330Sstevel@tonic-gate 		sctp->sctp_xmit_lowater = psctp->sctp_xmit_lowater;
9340Sstevel@tonic-gate 		sctp->sctp_xmit_hiwater = psctp->sctp_xmit_hiwater;
9350Sstevel@tonic-gate 		sctp->sctp_cwnd_max = psctp->sctp_cwnd_max;
9360Sstevel@tonic-gate 		sctp->sctp_rwnd = psctp->sctp_rwnd;
937852Svi117747 		sctp->sctp_irwnd = psctp->sctp_rwnd;
9380Sstevel@tonic-gate 
9390Sstevel@tonic-gate 		sctp->sctp_rto_max = psctp->sctp_rto_max;
9400Sstevel@tonic-gate 		sctp->sctp_init_rto_max = psctp->sctp_init_rto_max;
9410Sstevel@tonic-gate 		sctp->sctp_rto_min = psctp->sctp_rto_min;
9420Sstevel@tonic-gate 		sctp->sctp_rto_initial = psctp->sctp_rto_initial;
9430Sstevel@tonic-gate 		sctp->sctp_pa_max_rxt = psctp->sctp_pa_max_rxt;
9440Sstevel@tonic-gate 		sctp->sctp_pp_max_rxt = psctp->sctp_pp_max_rxt;
9450Sstevel@tonic-gate 		sctp->sctp_max_init_rxt = psctp->sctp_max_init_rxt;
9460Sstevel@tonic-gate 
9470Sstevel@tonic-gate 		sctp->sctp_def_stream = psctp->sctp_def_stream;
9480Sstevel@tonic-gate 		sctp->sctp_def_flags = psctp->sctp_def_flags;
9490Sstevel@tonic-gate 		sctp->sctp_def_ppid = psctp->sctp_def_ppid;
9500Sstevel@tonic-gate 		sctp->sctp_def_context = psctp->sctp_def_context;
9510Sstevel@tonic-gate 		sctp->sctp_def_timetolive = psctp->sctp_def_timetolive;
9520Sstevel@tonic-gate 
9530Sstevel@tonic-gate 		sctp->sctp_num_istr = psctp->sctp_num_istr;
9540Sstevel@tonic-gate 		sctp->sctp_num_ostr = psctp->sctp_num_ostr;
9550Sstevel@tonic-gate 
9560Sstevel@tonic-gate 		sctp->sctp_hb_interval = psctp->sctp_hb_interval;
9570Sstevel@tonic-gate 		sctp->sctp_autoclose = psctp->sctp_autoclose;
9580Sstevel@tonic-gate 		sctp->sctp_tx_adaption_code = psctp->sctp_tx_adaption_code;
9590Sstevel@tonic-gate 
9600Sstevel@tonic-gate 		/* xxx should be a better way to copy these flags xxx */
9610Sstevel@tonic-gate 		sctp->sctp_debug = psctp->sctp_debug;
9620Sstevel@tonic-gate 		sctp->sctp_dontroute = psctp->sctp_dontroute;
9630Sstevel@tonic-gate 		sctp->sctp_useloopback = psctp->sctp_useloopback;
9640Sstevel@tonic-gate 		sctp->sctp_broadcast = psctp->sctp_broadcast;
9650Sstevel@tonic-gate 		sctp->sctp_reuseaddr = psctp->sctp_reuseaddr;
9660Sstevel@tonic-gate 		sctp->sctp_bound_to_all = psctp->sctp_bound_to_all;
9670Sstevel@tonic-gate 		sctp->sctp_cansleep = psctp->sctp_cansleep;
9680Sstevel@tonic-gate 		sctp->sctp_send_adaption = psctp->sctp_send_adaption;
9690Sstevel@tonic-gate 		sctp->sctp_ndelay = psctp->sctp_ndelay;
9700Sstevel@tonic-gate 		sctp->sctp_events = psctp->sctp_events;
9710Sstevel@tonic-gate 		sctp->sctp_ipv6_recvancillary = psctp->sctp_ipv6_recvancillary;
9720Sstevel@tonic-gate 	} else {
9730Sstevel@tonic-gate 		/*
9740Sstevel@tonic-gate 		 * Initialize the header template
9750Sstevel@tonic-gate 		 */
9760Sstevel@tonic-gate 		if ((err = sctp_header_init_ipv4(sctp, sleep)) != 0) {
9770Sstevel@tonic-gate 			return (err);
9780Sstevel@tonic-gate 		}
9790Sstevel@tonic-gate 		if ((err = sctp_header_init_ipv6(sctp, sleep)) != 0) {
9800Sstevel@tonic-gate 			return (err);
9810Sstevel@tonic-gate 		}
9820Sstevel@tonic-gate 
9830Sstevel@tonic-gate 		/*
9840Sstevel@tonic-gate 		 * Set to system defaults
9850Sstevel@tonic-gate 		 */
9860Sstevel@tonic-gate 		sctp->sctp_cookie_lifetime = MSEC_TO_TICK(sctp_cookie_life);
9870Sstevel@tonic-gate 		sctp->sctp_xmit_lowater = sctp_xmit_lowat;
9880Sstevel@tonic-gate 		sctp->sctp_xmit_hiwater = sctp_xmit_hiwat;
9890Sstevel@tonic-gate 		sctp->sctp_cwnd_max = sctp_cwnd_max_;
9900Sstevel@tonic-gate 		sctp->sctp_rwnd = sctp_recv_hiwat;
991852Svi117747 		sctp->sctp_irwnd = sctp->sctp_rwnd;
9920Sstevel@tonic-gate 		sctp->sctp_rto_max = MSEC_TO_TICK(sctp_rto_maxg);
9930Sstevel@tonic-gate 		sctp->sctp_init_rto_max = sctp->sctp_rto_max;
9940Sstevel@tonic-gate 		sctp->sctp_rto_min = MSEC_TO_TICK(sctp_rto_ming);
9950Sstevel@tonic-gate 		sctp->sctp_rto_initial = MSEC_TO_TICK(sctp_rto_initialg);
9960Sstevel@tonic-gate 		sctp->sctp_pa_max_rxt = sctp_pa_max_retr;
9970Sstevel@tonic-gate 		sctp->sctp_pp_max_rxt = sctp_pp_max_retr;
9980Sstevel@tonic-gate 		sctp->sctp_max_init_rxt = sctp_max_init_retr;
9990Sstevel@tonic-gate 
10000Sstevel@tonic-gate 		sctp->sctp_num_istr = sctp_max_in_streams;
10010Sstevel@tonic-gate 		sctp->sctp_num_ostr = sctp_initial_out_streams;
10020Sstevel@tonic-gate 
10030Sstevel@tonic-gate 		sctp->sctp_hb_interval = MSEC_TO_TICK(sctp_heartbeat_interval);
10040Sstevel@tonic-gate 	}
10050Sstevel@tonic-gate 	sctp->sctp_understands_asconf = B_TRUE;
10060Sstevel@tonic-gate 	sctp->sctp_understands_addip = B_TRUE;
10070Sstevel@tonic-gate 	sctp->sctp_prsctp_aware = B_FALSE;
10080Sstevel@tonic-gate 
10090Sstevel@tonic-gate 	sctp->sctp_connp->conn_ref = 1;
10100Sstevel@tonic-gate 	sctp->sctp_connp->conn_fully_bound = B_FALSE;
10110Sstevel@tonic-gate 
10120Sstevel@tonic-gate 	sctp->sctp_prsctpdrop = 0;
10130Sstevel@tonic-gate 	sctp->sctp_msgcount = 0;
10140Sstevel@tonic-gate 
10150Sstevel@tonic-gate 	return (0);
10160Sstevel@tonic-gate }
10170Sstevel@tonic-gate 
10180Sstevel@tonic-gate /*
10190Sstevel@tonic-gate  * Extracts the init tag from an INIT chunk and checks if it matches
10200Sstevel@tonic-gate  * the sctp's verification tag. Returns 0 if it doesn't match, 1 if
10210Sstevel@tonic-gate  * it does.
10220Sstevel@tonic-gate  */
10230Sstevel@tonic-gate static boolean_t
10240Sstevel@tonic-gate sctp_icmp_verf(sctp_t *sctp, sctp_hdr_t *sh, mblk_t *mp)
10250Sstevel@tonic-gate {
10260Sstevel@tonic-gate 	sctp_chunk_hdr_t *sch;
10270Sstevel@tonic-gate 	uint32_t verf, *vp;
10280Sstevel@tonic-gate 
10290Sstevel@tonic-gate 	sch = (sctp_chunk_hdr_t *)(sh + 1);
10300Sstevel@tonic-gate 	vp = (uint32_t *)(sch + 1);
10310Sstevel@tonic-gate 
10320Sstevel@tonic-gate 	/* Need at least the data chunk hdr and the first 4 bytes of INIT */
10330Sstevel@tonic-gate 	if ((unsigned char *)(vp + 1) > mp->b_wptr) {
10340Sstevel@tonic-gate 		return (B_FALSE);
10350Sstevel@tonic-gate 	}
10360Sstevel@tonic-gate 
10370Sstevel@tonic-gate 	bcopy(vp, &verf, sizeof (verf));
10380Sstevel@tonic-gate 
10390Sstevel@tonic-gate 	if (verf == sctp->sctp_lvtag) {
10400Sstevel@tonic-gate 		return (B_TRUE);
10410Sstevel@tonic-gate 	}
10420Sstevel@tonic-gate 	return (B_FALSE);
10430Sstevel@tonic-gate }
10440Sstevel@tonic-gate 
10450Sstevel@tonic-gate /*
10460Sstevel@tonic-gate  * sctp_icmp_error is called by sctp_input() to process ICMP error messages
10470Sstevel@tonic-gate  * passed up by IP.  The queue is the default queue.  We need to find a sctp_t
10480Sstevel@tonic-gate  * that corresponds to the returned datagram.  Passes the message back in on
10490Sstevel@tonic-gate  * the correct queue once it has located the connection.
10500Sstevel@tonic-gate  * Assumes that IP has pulled up everything up to and including
10510Sstevel@tonic-gate  * the ICMP header.
10520Sstevel@tonic-gate  */
10530Sstevel@tonic-gate void
10540Sstevel@tonic-gate sctp_icmp_error(sctp_t *sctp, mblk_t *mp)
10550Sstevel@tonic-gate {
10560Sstevel@tonic-gate 	icmph_t *icmph;
10570Sstevel@tonic-gate 	ipha_t	*ipha;
10580Sstevel@tonic-gate 	int	iph_hdr_length;
10590Sstevel@tonic-gate 	sctp_hdr_t *sctph;
10600Sstevel@tonic-gate 	mblk_t *first_mp;
10610Sstevel@tonic-gate 	uint32_t new_mtu;
10620Sstevel@tonic-gate 	in6_addr_t dst;
10630Sstevel@tonic-gate 	sctp_faddr_t *fp;
10640Sstevel@tonic-gate 
10650Sstevel@tonic-gate 	dprint(1, ("sctp_icmp_error: sctp=%p, mp=%p\n", sctp, mp));
10660Sstevel@tonic-gate 
10670Sstevel@tonic-gate 	first_mp = mp;
10680Sstevel@tonic-gate 
10690Sstevel@tonic-gate 	ipha = (ipha_t *)mp->b_rptr;
10700Sstevel@tonic-gate 	if (IPH_HDR_VERSION(ipha) != IPV4_VERSION) {
10710Sstevel@tonic-gate 		ASSERT(IPH_HDR_VERSION(ipha) == IPV6_VERSION);
10720Sstevel@tonic-gate 		sctp_icmp_error_ipv6(sctp, first_mp);
10730Sstevel@tonic-gate 		return;
10740Sstevel@tonic-gate 	}
10750Sstevel@tonic-gate 
10760Sstevel@tonic-gate 	iph_hdr_length = IPH_HDR_LENGTH(ipha);
10770Sstevel@tonic-gate 	icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length];
10780Sstevel@tonic-gate 	ipha = (ipha_t *)&icmph[1];
10790Sstevel@tonic-gate 	iph_hdr_length = IPH_HDR_LENGTH(ipha);
10800Sstevel@tonic-gate 	sctph = (sctp_hdr_t *)((char *)ipha + iph_hdr_length);
10810Sstevel@tonic-gate 	if ((uchar_t *)(sctph + 1) >= mp->b_wptr) {
10820Sstevel@tonic-gate 		/* not enough data for SCTP header */
10830Sstevel@tonic-gate 		freemsg(first_mp);
10840Sstevel@tonic-gate 		return;
10850Sstevel@tonic-gate 	}
10860Sstevel@tonic-gate 
10870Sstevel@tonic-gate 	switch (icmph->icmph_type) {
10880Sstevel@tonic-gate 	case ICMP_DEST_UNREACHABLE:
10890Sstevel@tonic-gate 		switch (icmph->icmph_code) {
10900Sstevel@tonic-gate 		case ICMP_FRAGMENTATION_NEEDED:
10910Sstevel@tonic-gate 			/*
10920Sstevel@tonic-gate 			 * Reduce the MSS based on the new MTU.  This will
10930Sstevel@tonic-gate 			 * eliminate any fragmentation locally.
10940Sstevel@tonic-gate 			 * N.B.  There may well be some funny side-effects on
10950Sstevel@tonic-gate 			 * the local send policy and the remote receive policy.
10960Sstevel@tonic-gate 			 * Pending further research, we provide
10970Sstevel@tonic-gate 			 * sctp_ignore_path_mtu just in case this proves
10980Sstevel@tonic-gate 			 * disastrous somewhere.
10990Sstevel@tonic-gate 			 *
11000Sstevel@tonic-gate 			 * After updating the MSS, retransmit part of the
11010Sstevel@tonic-gate 			 * dropped segment using the new mss by calling
11020Sstevel@tonic-gate 			 * sctp_wput_slow().  Need to adjust all those
11030Sstevel@tonic-gate 			 * params to make sure sctp_wput_slow() work properly.
11040Sstevel@tonic-gate 			 */
11050Sstevel@tonic-gate 			if (sctp_ignore_path_mtu)
11060Sstevel@tonic-gate 				break;
11070Sstevel@tonic-gate 
11080Sstevel@tonic-gate 			/* find the offending faddr */
11090Sstevel@tonic-gate 			IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, &dst);
11100Sstevel@tonic-gate 			fp = sctp_lookup_faddr(sctp, &dst);
11110Sstevel@tonic-gate 			if (fp == NULL) {
11120Sstevel@tonic-gate 				break;
11130Sstevel@tonic-gate 			}
11140Sstevel@tonic-gate 
11150Sstevel@tonic-gate 			new_mtu = ntohs(icmph->icmph_du_mtu);
11160Sstevel@tonic-gate 
11170Sstevel@tonic-gate 			if (new_mtu - sctp->sctp_hdr_len >= fp->sfa_pmss)
11180Sstevel@tonic-gate 				break;
11190Sstevel@tonic-gate 
11200Sstevel@tonic-gate 			/*
11210Sstevel@tonic-gate 			 * Make sure that sfa_pmss is a multiple of
11220Sstevel@tonic-gate 			 * SCTP_ALIGN.
11230Sstevel@tonic-gate 			 */
11240Sstevel@tonic-gate 			fp->sfa_pmss = (new_mtu - sctp->sctp_hdr_len) &
11250Sstevel@tonic-gate 				~(SCTP_ALIGN - 1);
11260Sstevel@tonic-gate 			fp->pmtu_discovered = 1;
11270Sstevel@tonic-gate 
11280Sstevel@tonic-gate 			break;
11290Sstevel@tonic-gate 		case ICMP_PORT_UNREACHABLE:
11300Sstevel@tonic-gate 		case ICMP_PROTOCOL_UNREACHABLE:
11310Sstevel@tonic-gate 			switch (sctp->sctp_state) {
11320Sstevel@tonic-gate 			case SCTPS_COOKIE_WAIT:
11330Sstevel@tonic-gate 			case SCTPS_COOKIE_ECHOED:
11340Sstevel@tonic-gate 				/* make sure the verification tag matches */
11350Sstevel@tonic-gate 				if (!sctp_icmp_verf(sctp, sctph, mp)) {
11360Sstevel@tonic-gate 					break;
11370Sstevel@tonic-gate 				}
11380Sstevel@tonic-gate 				BUMP_MIB(&sctp_mib, sctpAborted);
11390Sstevel@tonic-gate 				sctp_clean_death(sctp, ECONNREFUSED);
11400Sstevel@tonic-gate 				break;
11410Sstevel@tonic-gate 			}
11420Sstevel@tonic-gate 			break;
11430Sstevel@tonic-gate 		case ICMP_HOST_UNREACHABLE:
11440Sstevel@tonic-gate 		case ICMP_NET_UNREACHABLE:
11450Sstevel@tonic-gate 			/* Record the error in case we finally time out. */
11460Sstevel@tonic-gate 			sctp->sctp_client_errno = (icmph->icmph_code ==
11470Sstevel@tonic-gate 			    ICMP_HOST_UNREACHABLE) ? EHOSTUNREACH : ENETUNREACH;
11480Sstevel@tonic-gate 			break;
11490Sstevel@tonic-gate 		default:
11500Sstevel@tonic-gate 			break;
11510Sstevel@tonic-gate 		}
11520Sstevel@tonic-gate 		break;
11530Sstevel@tonic-gate 	case ICMP_SOURCE_QUENCH: {
11540Sstevel@tonic-gate 		/* Reduce the sending rate as if we got a retransmit timeout */
11550Sstevel@tonic-gate 		break;
11560Sstevel@tonic-gate 	}
11570Sstevel@tonic-gate 	}
11580Sstevel@tonic-gate 	freemsg(first_mp);
11590Sstevel@tonic-gate }
11600Sstevel@tonic-gate 
11610Sstevel@tonic-gate /*
11620Sstevel@tonic-gate  * sctp_icmp_error_ipv6() is called by sctp_icmp_error() to process ICMPv6
11630Sstevel@tonic-gate  * error messages passed up by IP.
11640Sstevel@tonic-gate  * Assumes that IP has pulled up all the extension headers as well
11650Sstevel@tonic-gate  * as the ICMPv6 header.
11660Sstevel@tonic-gate  */
11670Sstevel@tonic-gate static void
11680Sstevel@tonic-gate sctp_icmp_error_ipv6(sctp_t *sctp, mblk_t *mp)
11690Sstevel@tonic-gate {
11700Sstevel@tonic-gate 	icmp6_t *icmp6;
11710Sstevel@tonic-gate 	ip6_t	*ip6h;
11720Sstevel@tonic-gate 	uint16_t	iph_hdr_length;
11730Sstevel@tonic-gate 	sctp_hdr_t *sctpha;
11740Sstevel@tonic-gate 	uint8_t	*nexthdrp;
11750Sstevel@tonic-gate 	uint32_t new_mtu;
11760Sstevel@tonic-gate 	sctp_faddr_t *fp;
11770Sstevel@tonic-gate 
11780Sstevel@tonic-gate 	ip6h = (ip6_t *)mp->b_rptr;
11790Sstevel@tonic-gate 	iph_hdr_length = (ip6h->ip6_nxt != IPPROTO_SCTP) ?
11800Sstevel@tonic-gate 	    ip_hdr_length_v6(mp, ip6h) : IPV6_HDR_LEN;
11810Sstevel@tonic-gate 
11820Sstevel@tonic-gate 	icmp6 = (icmp6_t *)&mp->b_rptr[iph_hdr_length];
11830Sstevel@tonic-gate 	ip6h = (ip6_t *)&icmp6[1];
11840Sstevel@tonic-gate 	if (!ip_hdr_length_nexthdr_v6(mp, ip6h, &iph_hdr_length, &nexthdrp)) {
11850Sstevel@tonic-gate 		freemsg(mp);
11860Sstevel@tonic-gate 		return;
11870Sstevel@tonic-gate 	}
11880Sstevel@tonic-gate 	ASSERT(*nexthdrp == IPPROTO_SCTP);
11890Sstevel@tonic-gate 
11900Sstevel@tonic-gate 	/* XXX need ifindex to find connection */
11910Sstevel@tonic-gate 	sctpha = (sctp_hdr_t *)((char *)ip6h + iph_hdr_length);
11920Sstevel@tonic-gate 	if ((uchar_t *)sctpha >= mp->b_wptr) {
11930Sstevel@tonic-gate 		/* not enough data for SCTP header */
11940Sstevel@tonic-gate 		freemsg(mp);
11950Sstevel@tonic-gate 		return;
11960Sstevel@tonic-gate 	}
11970Sstevel@tonic-gate 	switch (icmp6->icmp6_type) {
11980Sstevel@tonic-gate 	case ICMP6_PACKET_TOO_BIG:
11990Sstevel@tonic-gate 		/*
12000Sstevel@tonic-gate 		 * Reduce the MSS based on the new MTU.  This will
12010Sstevel@tonic-gate 		 * eliminate any fragmentation locally.
12020Sstevel@tonic-gate 		 * N.B.  There may well be some funny side-effects on
12030Sstevel@tonic-gate 		 * the local send policy and the remote receive policy.
12040Sstevel@tonic-gate 		 * Pending further research, we provide
12050Sstevel@tonic-gate 		 * sctp_ignore_path_mtu just in case this proves
12060Sstevel@tonic-gate 		 * disastrous somewhere.
12070Sstevel@tonic-gate 		 *
12080Sstevel@tonic-gate 		 * After updating the MSS, retransmit part of the
12090Sstevel@tonic-gate 		 * dropped segment using the new mss by calling
12100Sstevel@tonic-gate 		 * sctp_wput_slow().  Need to adjust all those
12110Sstevel@tonic-gate 		 * params to make sure sctp_wput_slow() work properly.
12120Sstevel@tonic-gate 		 */
12130Sstevel@tonic-gate 		if (sctp_ignore_path_mtu)
12140Sstevel@tonic-gate 			break;
12150Sstevel@tonic-gate 
12160Sstevel@tonic-gate 		/* find the offending faddr */
12170Sstevel@tonic-gate 		fp = sctp_lookup_faddr(sctp, &ip6h->ip6_dst);
12180Sstevel@tonic-gate 		if (fp == NULL) {
12190Sstevel@tonic-gate 			break;
12200Sstevel@tonic-gate 		}
12210Sstevel@tonic-gate 
12220Sstevel@tonic-gate 		new_mtu = ntohs(icmp6->icmp6_mtu);
12230Sstevel@tonic-gate 
12240Sstevel@tonic-gate 		if (new_mtu - sctp->sctp_hdr6_len >= fp->sfa_pmss)
12250Sstevel@tonic-gate 			break;
12260Sstevel@tonic-gate 
12270Sstevel@tonic-gate 		/* Make sure that sfa_pmss is a multiple of SCTP_ALIGN. */
12280Sstevel@tonic-gate 		fp->sfa_pmss = (new_mtu - sctp->sctp_hdr6_len) &
12290Sstevel@tonic-gate 			~(SCTP_ALIGN - 1);
12300Sstevel@tonic-gate 		fp->pmtu_discovered = 1;
12310Sstevel@tonic-gate 
12320Sstevel@tonic-gate 		break;
12330Sstevel@tonic-gate 
12340Sstevel@tonic-gate 	case ICMP6_DST_UNREACH:
12350Sstevel@tonic-gate 		switch (icmp6->icmp6_code) {
12360Sstevel@tonic-gate 		case ICMP6_DST_UNREACH_NOPORT:
12370Sstevel@tonic-gate 			/* make sure the verification tag matches */
12380Sstevel@tonic-gate 			if (!sctp_icmp_verf(sctp, sctpha, mp)) {
12390Sstevel@tonic-gate 				break;
12400Sstevel@tonic-gate 			}
12410Sstevel@tonic-gate 			if (sctp->sctp_state == SCTPS_COOKIE_WAIT ||
12420Sstevel@tonic-gate 			    sctp->sctp_state == SCTPS_COOKIE_ECHOED) {
12430Sstevel@tonic-gate 				BUMP_MIB(&sctp_mib, sctpAborted);
12440Sstevel@tonic-gate 				sctp_clean_death(sctp, ECONNREFUSED);
12450Sstevel@tonic-gate 			}
12460Sstevel@tonic-gate 			break;
12470Sstevel@tonic-gate 
12480Sstevel@tonic-gate 		case ICMP6_DST_UNREACH_ADMIN:
12490Sstevel@tonic-gate 		case ICMP6_DST_UNREACH_NOROUTE:
12500Sstevel@tonic-gate 		case ICMP6_DST_UNREACH_NOTNEIGHBOR:
12510Sstevel@tonic-gate 		case ICMP6_DST_UNREACH_ADDR:
12520Sstevel@tonic-gate 			/* Record the error in case we finally time out. */
12530Sstevel@tonic-gate 			sctp->sctp_client_errno = EHOSTUNREACH;
12540Sstevel@tonic-gate 			break;
12550Sstevel@tonic-gate 		default:
12560Sstevel@tonic-gate 			break;
12570Sstevel@tonic-gate 		}
12580Sstevel@tonic-gate 		break;
12590Sstevel@tonic-gate 
12600Sstevel@tonic-gate 	case ICMP6_PARAM_PROB:
12610Sstevel@tonic-gate 		/* If this corresponds to an ICMP_PROTOCOL_UNREACHABLE */
12620Sstevel@tonic-gate 		if (icmp6->icmp6_code == ICMP6_PARAMPROB_NEXTHEADER &&
12630Sstevel@tonic-gate 		    (uchar_t *)ip6h + icmp6->icmp6_pptr ==
12640Sstevel@tonic-gate 		    (uchar_t *)nexthdrp) {
12650Sstevel@tonic-gate 			/* make sure the verification tag matches */
12660Sstevel@tonic-gate 			if (!sctp_icmp_verf(sctp, sctpha, mp)) {
12670Sstevel@tonic-gate 				break;
12680Sstevel@tonic-gate 			}
12690Sstevel@tonic-gate 			if (sctp->sctp_state == SCTPS_COOKIE_WAIT) {
12700Sstevel@tonic-gate 				BUMP_MIB(&sctp_mib, sctpAborted);
12710Sstevel@tonic-gate 				sctp_clean_death(sctp, ECONNREFUSED);
12720Sstevel@tonic-gate 			}
12730Sstevel@tonic-gate 			break;
12740Sstevel@tonic-gate 		}
12750Sstevel@tonic-gate 		break;
12760Sstevel@tonic-gate 
12770Sstevel@tonic-gate 	case ICMP6_TIME_EXCEEDED:
12780Sstevel@tonic-gate 	default:
12790Sstevel@tonic-gate 		break;
12800Sstevel@tonic-gate 	}
12810Sstevel@tonic-gate 	freemsg(mp);
12820Sstevel@tonic-gate }
12830Sstevel@tonic-gate 
12840Sstevel@tonic-gate /*
12850Sstevel@tonic-gate  * Called by sockfs to create a new sctp instance.
12860Sstevel@tonic-gate  *
12870Sstevel@tonic-gate  * If parent pointer is passed in, inherit settings from it.
12880Sstevel@tonic-gate  */
12890Sstevel@tonic-gate sctp_t *
12900Sstevel@tonic-gate sctp_create(void *sctp_ulpd, sctp_t *parent, int family, int flags,
12910Sstevel@tonic-gate     const sctp_upcalls_t *sctp_upcalls, sctp_sockbuf_limits_t *sbl,
12920Sstevel@tonic-gate     cred_t *credp)
12930Sstevel@tonic-gate {
12940Sstevel@tonic-gate 	sctp_t		*sctp, *psctp;
12950Sstevel@tonic-gate 	conn_t		*sctp_connp;
12960Sstevel@tonic-gate 	mblk_t		*ack_mp, *hb_mp;
12970Sstevel@tonic-gate 	int		sleep = flags & SCTP_CAN_BLOCK ? KM_SLEEP : KM_NOSLEEP;
12980Sstevel@tonic-gate 
12990Sstevel@tonic-gate 	/* User must supply a credential. */
13000Sstevel@tonic-gate 	if (credp == NULL)
13010Sstevel@tonic-gate 		return (NULL);
13020Sstevel@tonic-gate 
13030Sstevel@tonic-gate 	if ((sctp_connp = ipcl_conn_create(IPCL_SCTPCONN, sleep)) == NULL)
13040Sstevel@tonic-gate 		return (NULL);
13050Sstevel@tonic-gate 	psctp = (sctp_t *)parent;
13060Sstevel@tonic-gate 
13070Sstevel@tonic-gate 	sctp = CONN2SCTP(sctp_connp);
13080Sstevel@tonic-gate 
13090Sstevel@tonic-gate 	if ((ack_mp = sctp_timer_alloc(sctp, sctp_ack_timer)) == NULL ||
13100Sstevel@tonic-gate 	    (hb_mp = sctp_timer_alloc(sctp, sctp_heartbeat_timer)) == NULL) {
13110Sstevel@tonic-gate 		if (ack_mp != NULL)
13120Sstevel@tonic-gate 			freeb(ack_mp);
13130Sstevel@tonic-gate 		kmem_cache_free(sctp_conn_cache, sctp_connp);
13140Sstevel@tonic-gate 		return (NULL);
13150Sstevel@tonic-gate 	}
13160Sstevel@tonic-gate 
13170Sstevel@tonic-gate 	sctp->sctp_ack_mp = ack_mp;
13180Sstevel@tonic-gate 	sctp->sctp_heartbeat_mp = hb_mp;
13190Sstevel@tonic-gate 
13200Sstevel@tonic-gate 	switch (family) {
13210Sstevel@tonic-gate 	case AF_INET6:
13220Sstevel@tonic-gate 		sctp_connp->conn_af_isv6 = B_TRUE;
13230Sstevel@tonic-gate 		sctp->sctp_ipversion = IPV6_VERSION;
13240Sstevel@tonic-gate 		sctp->sctp_family = AF_INET6;
13250Sstevel@tonic-gate 		break;
13260Sstevel@tonic-gate 
13270Sstevel@tonic-gate 	case AF_INET:
13280Sstevel@tonic-gate 		sctp_connp->conn_af_isv6 = B_FALSE;
13290Sstevel@tonic-gate 		sctp_connp->conn_pkt_isv6 = B_FALSE;
13300Sstevel@tonic-gate 		sctp->sctp_ipversion = IPV4_VERSION;
13310Sstevel@tonic-gate 		sctp->sctp_family = AF_INET;
13320Sstevel@tonic-gate 		break;
13330Sstevel@tonic-gate 	default:
13340Sstevel@tonic-gate 		ASSERT(0);
13350Sstevel@tonic-gate 		break;
13360Sstevel@tonic-gate 	}
13370Sstevel@tonic-gate 	if (sctp_init_values(sctp, psctp, sleep) != 0) {
13380Sstevel@tonic-gate 		freeb(ack_mp);
13390Sstevel@tonic-gate 		freeb(hb_mp);
13400Sstevel@tonic-gate 		kmem_cache_free(sctp_conn_cache, sctp_connp);
13410Sstevel@tonic-gate 		return (NULL);
13420Sstevel@tonic-gate 	}
13430Sstevel@tonic-gate 	sctp->sctp_cansleep = ((flags & SCTP_CAN_BLOCK) == SCTP_CAN_BLOCK);
13440Sstevel@tonic-gate 
13450Sstevel@tonic-gate 	sctp->sctp_mss = sctp_initial_mtu - ((family == AF_INET6) ?
13460Sstevel@tonic-gate 		sctp->sctp_hdr6_len : sctp->sctp_hdr_len);
13470Sstevel@tonic-gate 
13480Sstevel@tonic-gate 	if (psctp != NULL) {
13490Sstevel@tonic-gate 		RUN_SCTP(psctp);
13500Sstevel@tonic-gate 		/*
13510Sstevel@tonic-gate 		 * Inherit local address list, local port. Parent is either
13520Sstevel@tonic-gate 		 * in SCTPS_BOUND, or SCTPS_LISTEN state.
13530Sstevel@tonic-gate 		 */
13540Sstevel@tonic-gate 		ASSERT((psctp->sctp_state == SCTPS_BOUND) ||
13550Sstevel@tonic-gate 		    (psctp->sctp_state == SCTPS_LISTEN));
13560Sstevel@tonic-gate 		if (sctp_dup_saddrs(psctp, sctp, sleep)) {
13570Sstevel@tonic-gate 			WAKE_SCTP(psctp);
13580Sstevel@tonic-gate 			freeb(ack_mp);
13590Sstevel@tonic-gate 			freeb(hb_mp);
13600Sstevel@tonic-gate 			sctp_headers_free(sctp);
13610Sstevel@tonic-gate 			kmem_cache_free(sctp_conn_cache, sctp_connp);
13620Sstevel@tonic-gate 			return (NULL);
13630Sstevel@tonic-gate 		}
13640Sstevel@tonic-gate 
13650Sstevel@tonic-gate 		/*
13660Sstevel@tonic-gate 		 * If the parent is specified, it'll be immediatelly
13670Sstevel@tonic-gate 		 * followed by sctp_connect(). So don't add this guy to
13680Sstevel@tonic-gate 		 * bind hash.
13690Sstevel@tonic-gate 		 */
13700Sstevel@tonic-gate 		sctp->sctp_lport = psctp->sctp_lport;
13710Sstevel@tonic-gate 		sctp->sctp_state = SCTPS_BOUND;
13720Sstevel@tonic-gate 		sctp->sctp_zoneid = psctp->sctp_zoneid;
13730Sstevel@tonic-gate 		WAKE_SCTP(psctp);
13740Sstevel@tonic-gate 	} else {
13750Sstevel@tonic-gate 		sctp->sctp_zoneid = getzoneid();
13760Sstevel@tonic-gate 	}
13770Sstevel@tonic-gate 
13780Sstevel@tonic-gate 	sctp_connp->conn_cred = credp;
13790Sstevel@tonic-gate 	crhold(credp);
13800Sstevel@tonic-gate 
13810Sstevel@tonic-gate 	/* Initialize SCTP instance values,  our verf tag must never be 0 */
13820Sstevel@tonic-gate 	(void) random_get_pseudo_bytes((uint8_t *)&sctp->sctp_lvtag,
13830Sstevel@tonic-gate 	    sizeof (sctp->sctp_lvtag));
13840Sstevel@tonic-gate 	if (sctp->sctp_lvtag == 0)
13850Sstevel@tonic-gate 		sctp->sctp_lvtag = (uint32_t)gethrtime();
13860Sstevel@tonic-gate 	ASSERT(sctp->sctp_lvtag != 0);
13870Sstevel@tonic-gate 
13880Sstevel@tonic-gate 	sctp->sctp_ltsn = sctp->sctp_lvtag + 1;
13890Sstevel@tonic-gate 	sctp->sctp_lcsn = sctp->sctp_ltsn;
13900Sstevel@tonic-gate 	sctp->sctp_recovery_tsn = sctp->sctp_lastack_rxd = sctp->sctp_ltsn - 1;
13910Sstevel@tonic-gate 	sctp->sctp_adv_pap = sctp->sctp_lastack_rxd;
13920Sstevel@tonic-gate 
13930Sstevel@tonic-gate 	/* Information required by upper layer */
13940Sstevel@tonic-gate 	if (sctp_ulpd != NULL) {
13950Sstevel@tonic-gate 		sctp->sctp_ulpd = sctp_ulpd;
13960Sstevel@tonic-gate 
13970Sstevel@tonic-gate 		ASSERT(sctp_upcalls != NULL);
13980Sstevel@tonic-gate 		bcopy(sctp_upcalls, &sctp->sctp_upcalls,
13990Sstevel@tonic-gate 		    sizeof (sctp_upcalls_t));
14000Sstevel@tonic-gate 		ASSERT(sbl != NULL);
14010Sstevel@tonic-gate 		/* Fill in the socket buffer limits for sctpsockfs */
14020Sstevel@tonic-gate 		sbl->sbl_txlowat = sctp->sctp_xmit_lowater;
14030Sstevel@tonic-gate 		sbl->sbl_txbuf = sctp->sctp_xmit_hiwater;
14040Sstevel@tonic-gate 		sbl->sbl_rxbuf = sctp->sctp_rwnd;
14050Sstevel@tonic-gate 		sbl->sbl_rxlowat = SCTP_RECV_LOWATER;
14060Sstevel@tonic-gate 	}
14070Sstevel@tonic-gate 	/* If no sctp_ulpd, must be creating the default sctp */
14080Sstevel@tonic-gate 	ASSERT(sctp_ulpd != NULL || gsctp == NULL);
14090Sstevel@tonic-gate 
14100Sstevel@tonic-gate 	/* Insert this in the global list. */
14110Sstevel@tonic-gate 	SCTP_LINK(sctp);
14120Sstevel@tonic-gate 
14130Sstevel@tonic-gate 	return (sctp);
14140Sstevel@tonic-gate }
14150Sstevel@tonic-gate 
14160Sstevel@tonic-gate void
14170Sstevel@tonic-gate sctp_ddi_init(void)
14180Sstevel@tonic-gate {
14190Sstevel@tonic-gate 	/* Initialize locks */
14200Sstevel@tonic-gate 	mutex_init(&sctp_g_lock, NULL, MUTEX_DEFAULT, NULL);
14210Sstevel@tonic-gate 	mutex_init(&sctp_epriv_port_lock, NULL, MUTEX_DEFAULT, NULL);
14220Sstevel@tonic-gate 
14230Sstevel@tonic-gate 	/* Initialize SCTP hash arrays. */
14240Sstevel@tonic-gate 	sctp_hash_init();
14250Sstevel@tonic-gate 
14260Sstevel@tonic-gate 	sctp_pad_mp = allocb(SCTP_ALIGN, BPRI_MED);
14270Sstevel@tonic-gate 	bzero(sctp_pad_mp->b_rptr, SCTP_ALIGN);
14280Sstevel@tonic-gate 	ASSERT(sctp_pad_mp);
14290Sstevel@tonic-gate 
14300Sstevel@tonic-gate 	if (!sctp_nd_init()) {
14310Sstevel@tonic-gate 		sctp_nd_free();
14320Sstevel@tonic-gate 	}
14330Sstevel@tonic-gate 
14340Sstevel@tonic-gate 	/* Create sctp_t/conn_t cache */
14350Sstevel@tonic-gate 	sctp_conn_cache_init();
14360Sstevel@tonic-gate 
14370Sstevel@tonic-gate 	/* Create the faddr cache */
14380Sstevel@tonic-gate 	sctp_faddr_init();
14390Sstevel@tonic-gate 
14400Sstevel@tonic-gate 	/* Create the sets cache */
14410Sstevel@tonic-gate 	sctp_sets_init();
14420Sstevel@tonic-gate 
14430Sstevel@tonic-gate 	/* Create the PR-SCTP sets cache */
14440Sstevel@tonic-gate 	sctp_ftsn_sets_init();
14450Sstevel@tonic-gate 
14460Sstevel@tonic-gate 	/* Initialize the recvq taskq. */
14470Sstevel@tonic-gate 	sctp_rq_tq_init();
14480Sstevel@tonic-gate 
14490Sstevel@tonic-gate 	/* saddr init */
14500Sstevel@tonic-gate 	sctp_saddr_init();
14510Sstevel@tonic-gate 
14520Sstevel@tonic-gate 	/* Global SCTP PCB list. */
14530Sstevel@tonic-gate 	list_create(&sctp_g_list, sizeof (sctp_t),
14540Sstevel@tonic-gate 	    offsetof(sctp_t, sctp_list));
14550Sstevel@tonic-gate 
14560Sstevel@tonic-gate 	/* Initialize tables used for CRC calculation */
14570Sstevel@tonic-gate 	sctp_crc32_init();
14580Sstevel@tonic-gate 
14590Sstevel@tonic-gate 	/* Initialize sctp kernel stats. */
14600Sstevel@tonic-gate 	sctp_kstat_init();
14610Sstevel@tonic-gate }
14620Sstevel@tonic-gate 
14630Sstevel@tonic-gate void
14640Sstevel@tonic-gate sctp_ddi_destroy(void)
14650Sstevel@tonic-gate {
14660Sstevel@tonic-gate 	sctp_nd_free();
14670Sstevel@tonic-gate 
14680Sstevel@tonic-gate 	/* Destroy sctp_t/conn_t caches */
14690Sstevel@tonic-gate 	sctp_conn_cache_fini();
14700Sstevel@tonic-gate 
14710Sstevel@tonic-gate 	/* Destroy the faddr cache */
14720Sstevel@tonic-gate 	sctp_faddr_fini();
14730Sstevel@tonic-gate 
14740Sstevel@tonic-gate 	/* Destroy the sets cache */
14750Sstevel@tonic-gate 	sctp_sets_fini();
14760Sstevel@tonic-gate 
14770Sstevel@tonic-gate 	/* Destroy the PR-SCTP sets cache */
14780Sstevel@tonic-gate 	sctp_ftsn_sets_fini();
14790Sstevel@tonic-gate 
14800Sstevel@tonic-gate 	/* Destroy the recvq taskqs. */
14810Sstevel@tonic-gate 	sctp_rq_tq_fini();
14820Sstevel@tonic-gate 
14830Sstevel@tonic-gate 	/* Destroy saddr  */
14840Sstevel@tonic-gate 	sctp_saddr_fini();
14850Sstevel@tonic-gate 
14860Sstevel@tonic-gate 	/* Global SCTP PCB list. */
14870Sstevel@tonic-gate 	list_destroy(&sctp_g_list);
14880Sstevel@tonic-gate 
14890Sstevel@tonic-gate 	/* Destroy SCTP hash arrays. */
14900Sstevel@tonic-gate 	sctp_hash_destroy();
14910Sstevel@tonic-gate 
14920Sstevel@tonic-gate 	/* Destroy SCTP kenrel stats. */
14930Sstevel@tonic-gate 	sctp_kstat_fini();
14940Sstevel@tonic-gate 
14950Sstevel@tonic-gate 	mutex_destroy(&sctp_g_lock);
14960Sstevel@tonic-gate 	mutex_destroy(&sctp_epriv_port_lock);
14970Sstevel@tonic-gate }
14980Sstevel@tonic-gate 
14990Sstevel@tonic-gate void
15000Sstevel@tonic-gate sctp_display_all()
15010Sstevel@tonic-gate {
15020Sstevel@tonic-gate 	sctp_t *sctp_walker;
15030Sstevel@tonic-gate 
15040Sstevel@tonic-gate 	mutex_enter(&sctp_g_lock);
15050Sstevel@tonic-gate 	for (sctp_walker = gsctp; sctp_walker != NULL;
15060Sstevel@tonic-gate 		sctp_walker = (sctp_t *)list_next(&sctp_g_list, sctp_walker)) {
15070Sstevel@tonic-gate 		(void) sctp_display(sctp_walker, NULL);
15080Sstevel@tonic-gate 	}
15090Sstevel@tonic-gate 	mutex_exit(&sctp_g_lock);
15100Sstevel@tonic-gate }
15110Sstevel@tonic-gate 
15120Sstevel@tonic-gate static void
15130Sstevel@tonic-gate sctp_rq_tq_init(void)
15140Sstevel@tonic-gate {
15150Sstevel@tonic-gate 	/*
15160Sstevel@tonic-gate 	 * Initialize the recvq_tq_list and create the first recvq taskq.
15170Sstevel@tonic-gate 	 * What to do if it fails?
15180Sstevel@tonic-gate 	 */
15190Sstevel@tonic-gate 	recvq_tq_list = kmem_zalloc(sctp_recvq_tq_list_max_sz *
15200Sstevel@tonic-gate 	    sizeof (taskq_t *), KM_SLEEP);
15210Sstevel@tonic-gate 	recvq_tq_list[0] = taskq_create("sctp_def_recvq_taskq",
15220Sstevel@tonic-gate 	    MIN(sctp_recvq_tq_thr_max, MAX(sctp_recvq_tq_thr_min, ncpus)),
15230Sstevel@tonic-gate 	    minclsyspri, sctp_recvq_tq_task_min, sctp_recvq_tq_task_max,
15240Sstevel@tonic-gate 	    TASKQ_PREPOPULATE);
15250Sstevel@tonic-gate 	mutex_init(&sctp_rq_tq_lock, NULL, MUTEX_DEFAULT, NULL);
15260Sstevel@tonic-gate }
15270Sstevel@tonic-gate 
15280Sstevel@tonic-gate static void
15290Sstevel@tonic-gate sctp_rq_tq_fini(void)
15300Sstevel@tonic-gate {
15310Sstevel@tonic-gate 	int i;
15320Sstevel@tonic-gate 
15330Sstevel@tonic-gate 	for (i = 0; i < recvq_tq_list_cur_sz; i++) {
15340Sstevel@tonic-gate 		ASSERT(recvq_tq_list[i] != NULL);
15350Sstevel@tonic-gate 		taskq_destroy(recvq_tq_list[i]);
15360Sstevel@tonic-gate 	}
15370Sstevel@tonic-gate 	kmem_free(recvq_tq_list, sctp_recvq_tq_list_max_sz *
15380Sstevel@tonic-gate 	    sizeof (taskq_t *));
15390Sstevel@tonic-gate }
15400Sstevel@tonic-gate 
15410Sstevel@tonic-gate /* Add another taskq for a new ill. */
15420Sstevel@tonic-gate void
15430Sstevel@tonic-gate sctp_inc_taskq(void)
15440Sstevel@tonic-gate {
15450Sstevel@tonic-gate 	taskq_t *tq;
15460Sstevel@tonic-gate 	char tq_name[TASKQ_NAMELEN];
15470Sstevel@tonic-gate 
15480Sstevel@tonic-gate 	mutex_enter(&sctp_rq_tq_lock);
15490Sstevel@tonic-gate 	if (recvq_tq_list_cur_sz + 1 > sctp_recvq_tq_list_max_sz) {
15500Sstevel@tonic-gate 		mutex_exit(&sctp_rq_tq_lock);
15510Sstevel@tonic-gate 		cmn_err(CE_NOTE, "Cannot create more SCTP recvq taskq");
15520Sstevel@tonic-gate 		return;
15530Sstevel@tonic-gate 	}
15540Sstevel@tonic-gate 
15550Sstevel@tonic-gate 	(void) snprintf(tq_name, sizeof (tq_name), "sctp_recvq_taskq_%u",
15560Sstevel@tonic-gate 	    recvq_tq_list_cur_sz);
15570Sstevel@tonic-gate 	tq = taskq_create(tq_name,
15580Sstevel@tonic-gate 	    MIN(sctp_recvq_tq_thr_max, MAX(sctp_recvq_tq_thr_min, ncpus)),
15590Sstevel@tonic-gate 	    minclsyspri, sctp_recvq_tq_task_min, sctp_recvq_tq_task_max,
15600Sstevel@tonic-gate 	    TASKQ_PREPOPULATE);
15610Sstevel@tonic-gate 	if (tq == NULL) {
15620Sstevel@tonic-gate 		mutex_exit(&sctp_rq_tq_lock);
15630Sstevel@tonic-gate 		cmn_err(CE_NOTE, "SCTP recvq taskq creation failed");
15640Sstevel@tonic-gate 		return;
15650Sstevel@tonic-gate 	}
15660Sstevel@tonic-gate 	ASSERT(recvq_tq_list[recvq_tq_list_cur_sz] == NULL);
15670Sstevel@tonic-gate 	recvq_tq_list[recvq_tq_list_cur_sz] = tq;
15680Sstevel@tonic-gate 	atomic_add_32(&recvq_tq_list_cur_sz, 1);
15690Sstevel@tonic-gate 	mutex_exit(&sctp_rq_tq_lock);
15700Sstevel@tonic-gate }
15710Sstevel@tonic-gate 
15720Sstevel@tonic-gate #ifdef DEBUG
15730Sstevel@tonic-gate uint32_t sendq_loop_cnt = 0;
15740Sstevel@tonic-gate uint32_t sendq_collision = 0;
15750Sstevel@tonic-gate uint32_t sendq_empty = 0;
15760Sstevel@tonic-gate #endif
15770Sstevel@tonic-gate 
15780Sstevel@tonic-gate void
15790Sstevel@tonic-gate sctp_add_sendq(sctp_t *sctp, mblk_t *mp)
15800Sstevel@tonic-gate {
15810Sstevel@tonic-gate 	mutex_enter(&sctp->sctp_sendq_lock);
15820Sstevel@tonic-gate 	if (sctp->sctp_sendq == NULL) {
15830Sstevel@tonic-gate 		sctp->sctp_sendq = mp;
15840Sstevel@tonic-gate 		sctp->sctp_sendq_tail = mp;
15850Sstevel@tonic-gate 	} else {
15860Sstevel@tonic-gate 		sctp->sctp_sendq_tail->b_next = mp;
15870Sstevel@tonic-gate 		sctp->sctp_sendq_tail = mp;
15880Sstevel@tonic-gate 	}
15890Sstevel@tonic-gate 	mutex_exit(&sctp->sctp_sendq_lock);
15900Sstevel@tonic-gate }
15910Sstevel@tonic-gate 
15920Sstevel@tonic-gate void
15930Sstevel@tonic-gate sctp_process_sendq(sctp_t *sctp)
15940Sstevel@tonic-gate {
15950Sstevel@tonic-gate 	mblk_t *mp;
15960Sstevel@tonic-gate #ifdef DEBUG
15970Sstevel@tonic-gate 	uint32_t loop_cnt = 0;
15980Sstevel@tonic-gate #endif
15990Sstevel@tonic-gate 
16000Sstevel@tonic-gate 	mutex_enter(&sctp->sctp_sendq_lock);
16010Sstevel@tonic-gate 	if (sctp->sctp_sendq == NULL || sctp->sctp_sendq_sending) {
16020Sstevel@tonic-gate #ifdef DEBUG
16030Sstevel@tonic-gate 		if (sctp->sctp_sendq == NULL)
16040Sstevel@tonic-gate 			sendq_empty++;
16050Sstevel@tonic-gate 		else
16060Sstevel@tonic-gate 			sendq_collision++;
16070Sstevel@tonic-gate #endif
16080Sstevel@tonic-gate 		mutex_exit(&sctp->sctp_sendq_lock);
16090Sstevel@tonic-gate 		return;
16100Sstevel@tonic-gate 	}
16110Sstevel@tonic-gate 	sctp->sctp_sendq_sending = B_TRUE;
16120Sstevel@tonic-gate 
16130Sstevel@tonic-gate 	/*
16140Sstevel@tonic-gate 	 * Note that while we are in this loop, other thread can put
16150Sstevel@tonic-gate 	 * new packets in the receive queue.  We may be looping for
16160Sstevel@tonic-gate 	 * quite a while.  This is OK even for an interrupt thread.
16170Sstevel@tonic-gate 	 * The reason is that SCTP should only able to send a limited
16180Sstevel@tonic-gate 	 * number of packets out in a burst.  So the number of times
16190Sstevel@tonic-gate 	 * we go through this loop should not be many.
16200Sstevel@tonic-gate 	 */
16210Sstevel@tonic-gate 	while ((mp = sctp->sctp_sendq) != NULL) {
16220Sstevel@tonic-gate 		sctp->sctp_sendq = mp->b_next;
16230Sstevel@tonic-gate 		ASSERT(sctp->sctp_connp->conn_ref > 0);
16240Sstevel@tonic-gate 		mutex_exit(&sctp->sctp_sendq_lock);
16250Sstevel@tonic-gate 		mp->b_next = NULL;
16260Sstevel@tonic-gate 		CONN_INC_REF(sctp->sctp_connp);
16270Sstevel@tonic-gate 		mp->b_flag |= MSGHASREF;
16280Sstevel@tonic-gate 		/* If we don't have sctp_current, default to IPv4 */
16290Sstevel@tonic-gate 		IP_PUT(mp, sctp->sctp_connp, sctp->sctp_current == NULL ?
16300Sstevel@tonic-gate 		    B_TRUE : sctp->sctp_current->isv4);
16310Sstevel@tonic-gate 		BUMP_LOCAL(sctp->sctp_opkts);
16320Sstevel@tonic-gate #ifdef DEBUG
16330Sstevel@tonic-gate 		loop_cnt++;
16340Sstevel@tonic-gate #endif
16350Sstevel@tonic-gate 		mutex_enter(&sctp->sctp_sendq_lock);
16360Sstevel@tonic-gate 	}
16370Sstevel@tonic-gate 
16380Sstevel@tonic-gate 	sctp->sctp_sendq_tail = NULL;
16390Sstevel@tonic-gate 	sctp->sctp_sendq_sending = B_FALSE;
16400Sstevel@tonic-gate #ifdef DEBUG
16410Sstevel@tonic-gate 	if (loop_cnt > sendq_loop_cnt)
16420Sstevel@tonic-gate 		sendq_loop_cnt = loop_cnt;
16430Sstevel@tonic-gate #endif
16440Sstevel@tonic-gate 	mutex_exit(&sctp->sctp_sendq_lock);
16450Sstevel@tonic-gate }
16460Sstevel@tonic-gate 
16470Sstevel@tonic-gate #ifdef DEBUG
16480Sstevel@tonic-gate uint32_t recvq_loop_cnt = 0;
16490Sstevel@tonic-gate uint32_t recvq_call = 0;
16500Sstevel@tonic-gate #endif
16510Sstevel@tonic-gate 
16520Sstevel@tonic-gate /*
16530Sstevel@tonic-gate  * Find the next recvq_tq to use.  This routine will go thru all the
16540Sstevel@tonic-gate  * taskqs until it can dispatch a job for the sctp.  If this fails,
16550Sstevel@tonic-gate  * it will create a new taskq and try it.
16560Sstevel@tonic-gate  */
16570Sstevel@tonic-gate static boolean_t
16580Sstevel@tonic-gate sctp_find_next_tq(sctp_t *sctp)
16590Sstevel@tonic-gate {
16600Sstevel@tonic-gate 	int next_tq, try;
16610Sstevel@tonic-gate 	taskq_t *tq;
16620Sstevel@tonic-gate 
16630Sstevel@tonic-gate 	/*
16640Sstevel@tonic-gate 	 * Note that since we don't hold a lock on sctp_rq_tq_lock for
16650Sstevel@tonic-gate 	 * performance reason, recvq_ta_list_cur_sz can be changed during
16660Sstevel@tonic-gate 	 * this loop.  The problem this will create is that the loop may
16670Sstevel@tonic-gate 	 * not have tried all the recvq_tq.  This should be OK.
16680Sstevel@tonic-gate 	 */
16690Sstevel@tonic-gate 	next_tq = atomic_add_32_nv(&recvq_tq_list_cur, 1) %
16700Sstevel@tonic-gate 	    recvq_tq_list_cur_sz;
16710Sstevel@tonic-gate 	for (try = 0; try < recvq_tq_list_cur_sz;
16720Sstevel@tonic-gate 	    try++, next_tq = (next_tq + 1) % recvq_tq_list_cur_sz) {
16730Sstevel@tonic-gate 		tq = recvq_tq_list[next_tq];
16740Sstevel@tonic-gate 		if (taskq_dispatch(tq, sctp_process_recvq, sctp,
16750Sstevel@tonic-gate 		    TQ_NOSLEEP) != NULL) {
16760Sstevel@tonic-gate 			sctp->sctp_recvq_tq = tq;
16770Sstevel@tonic-gate 			return (B_TRUE);
16780Sstevel@tonic-gate 		}
16790Sstevel@tonic-gate 	}
16800Sstevel@tonic-gate 
16810Sstevel@tonic-gate 	/*
16820Sstevel@tonic-gate 	 * Create one more taskq and try it.  Note that sctp_inc_taskq()
16830Sstevel@tonic-gate 	 * may not have created another taskq if the number of recvq
16840Sstevel@tonic-gate 	 * taskqs is at the maximum.  We are probably in a pretty bad
16850Sstevel@tonic-gate 	 * shape if this actually happens...
16860Sstevel@tonic-gate 	 */
16870Sstevel@tonic-gate 	sctp_inc_taskq();
16880Sstevel@tonic-gate 	tq = recvq_tq_list[recvq_tq_list_cur_sz - 1];
16890Sstevel@tonic-gate 	if (taskq_dispatch(tq, sctp_process_recvq, sctp, TQ_NOSLEEP) != NULL) {
16900Sstevel@tonic-gate 		sctp->sctp_recvq_tq = tq;
16910Sstevel@tonic-gate 		return (B_TRUE);
16920Sstevel@tonic-gate 	}
16930Sstevel@tonic-gate 	return (B_FALSE);
16940Sstevel@tonic-gate }
16950Sstevel@tonic-gate 
16960Sstevel@tonic-gate /*
16970Sstevel@tonic-gate  * To add a message to the recvq.  Note that the sctp_timer_fire()
16980Sstevel@tonic-gate  * routine also uses this function to add the timer message to the
16990Sstevel@tonic-gate  * receive queue for later processing.  And it should be the only
17000Sstevel@tonic-gate  * caller of sctp_add_recvq() which sets the try_harder argument
17010Sstevel@tonic-gate  * to B_TRUE.
17020Sstevel@tonic-gate  *
17030Sstevel@tonic-gate  * If the try_harder argument is B_TRUE, this routine sctp_find_next_tq()
17040Sstevel@tonic-gate  * will try very hard to dispatch the task.  Refer to the comment
17050Sstevel@tonic-gate  * for that routine on how it does that.
17060Sstevel@tonic-gate  */
17070Sstevel@tonic-gate boolean_t
17080Sstevel@tonic-gate sctp_add_recvq(sctp_t *sctp, mblk_t *mp, boolean_t caller_hold_lock)
17090Sstevel@tonic-gate {
17100Sstevel@tonic-gate 	if (!caller_hold_lock)
17110Sstevel@tonic-gate 		mutex_enter(&sctp->sctp_recvq_lock);
17120Sstevel@tonic-gate 
17130Sstevel@tonic-gate 	/* If the taskq dispatch has not been scheduled, do it now. */
17140Sstevel@tonic-gate 	if (sctp->sctp_recvq_tq == NULL) {
17150Sstevel@tonic-gate 		ASSERT(sctp->sctp_recvq == NULL);
17160Sstevel@tonic-gate 		if (!sctp_find_next_tq(sctp)) {
17170Sstevel@tonic-gate 			if (!caller_hold_lock)
17180Sstevel@tonic-gate 				mutex_exit(&sctp->sctp_recvq_lock);
17190Sstevel@tonic-gate 			return (B_FALSE);
17200Sstevel@tonic-gate 		}
17210Sstevel@tonic-gate 		/* Make sure the sctp_t will not go away. */
17220Sstevel@tonic-gate 		SCTP_REFHOLD(sctp);
17230Sstevel@tonic-gate 	}
17240Sstevel@tonic-gate 
17250Sstevel@tonic-gate 	if (sctp->sctp_recvq == NULL) {
17260Sstevel@tonic-gate 		sctp->sctp_recvq = mp;
17270Sstevel@tonic-gate 		sctp->sctp_recvq_tail = mp;
17280Sstevel@tonic-gate 	} else {
17290Sstevel@tonic-gate 		sctp->sctp_recvq_tail->b_next = mp;
17300Sstevel@tonic-gate 		sctp->sctp_recvq_tail = mp;
17310Sstevel@tonic-gate 	}
17320Sstevel@tonic-gate 
17330Sstevel@tonic-gate 	if (!caller_hold_lock)
17340Sstevel@tonic-gate 		mutex_exit(&sctp->sctp_recvq_lock);
17350Sstevel@tonic-gate 	return (B_TRUE);
17360Sstevel@tonic-gate }
17370Sstevel@tonic-gate 
17380Sstevel@tonic-gate static void
17390Sstevel@tonic-gate sctp_process_recvq(void *arg)
17400Sstevel@tonic-gate {
17410Sstevel@tonic-gate 	sctp_t		*sctp = (sctp_t *)arg;
17420Sstevel@tonic-gate 	mblk_t		*mp;
17430Sstevel@tonic-gate 	mblk_t		*ipsec_mp;
17440Sstevel@tonic-gate #ifdef DEBUG
17450Sstevel@tonic-gate 	uint32_t	loop_cnt = 0;
17460Sstevel@tonic-gate #endif
17470Sstevel@tonic-gate 
17480Sstevel@tonic-gate #ifdef	_BIG_ENDIAN
17490Sstevel@tonic-gate #define	IPVER(ip6h)	((((uint32_t *)ip6h)[0] >> 28) & 0x7)
17500Sstevel@tonic-gate #else
17510Sstevel@tonic-gate #define	IPVER(ip6h)	((((uint32_t *)ip6h)[0] >> 4) & 0x7)
17520Sstevel@tonic-gate #endif
17530Sstevel@tonic-gate 
17540Sstevel@tonic-gate 	RUN_SCTP(sctp);
17550Sstevel@tonic-gate 	mutex_enter(&sctp->sctp_recvq_lock);
17560Sstevel@tonic-gate 
17570Sstevel@tonic-gate #ifdef DEBUG
17580Sstevel@tonic-gate 	recvq_call++;
17590Sstevel@tonic-gate #endif
17600Sstevel@tonic-gate 	/*
17610Sstevel@tonic-gate 	 * Note that while we are in this loop, other thread can put
17620Sstevel@tonic-gate 	 * new packets in the receive queue.  We may be looping for
17630Sstevel@tonic-gate 	 * quite a while.
17640Sstevel@tonic-gate 	 */
17650Sstevel@tonic-gate 	while ((mp = sctp->sctp_recvq) != NULL) {
17660Sstevel@tonic-gate 		sctp->sctp_recvq = mp->b_next;
17670Sstevel@tonic-gate 		mutex_exit(&sctp->sctp_recvq_lock);
17680Sstevel@tonic-gate 		mp->b_next = NULL;
17690Sstevel@tonic-gate #ifdef DEBUG
17700Sstevel@tonic-gate 		loop_cnt++;
17710Sstevel@tonic-gate #endif
17720Sstevel@tonic-gate 		ipsec_mp = mp->b_prev;
17730Sstevel@tonic-gate 		mp->b_prev = NULL;
17740Sstevel@tonic-gate 		sctp_input_data(sctp, mp, ipsec_mp);
17750Sstevel@tonic-gate 
17760Sstevel@tonic-gate 		mutex_enter(&sctp->sctp_recvq_lock);
17770Sstevel@tonic-gate 	}
17780Sstevel@tonic-gate 
17790Sstevel@tonic-gate 	sctp->sctp_recvq_tail = NULL;
17800Sstevel@tonic-gate 	sctp->sctp_recvq_tq = NULL;
17810Sstevel@tonic-gate 
17820Sstevel@tonic-gate 	mutex_exit(&sctp->sctp_recvq_lock);
17830Sstevel@tonic-gate 
17840Sstevel@tonic-gate 	WAKE_SCTP(sctp);
17850Sstevel@tonic-gate 
17860Sstevel@tonic-gate 	/* We may have sent something when processing the receive queue. */
17870Sstevel@tonic-gate 	sctp_process_sendq(sctp);
17880Sstevel@tonic-gate #ifdef DEBUG
17890Sstevel@tonic-gate 	if (loop_cnt > recvq_loop_cnt)
17900Sstevel@tonic-gate 		recvq_loop_cnt = loop_cnt;
17910Sstevel@tonic-gate #endif
17920Sstevel@tonic-gate 	/* Now it can go away. */
17930Sstevel@tonic-gate 	SCTP_REFRELE(sctp);
17940Sstevel@tonic-gate }
17950Sstevel@tonic-gate 
17960Sstevel@tonic-gate /* ARGSUSED */
17970Sstevel@tonic-gate static int
17980Sstevel@tonic-gate sctp_conn_cache_constructor(void *buf, void *cdrarg, int kmflags)
17990Sstevel@tonic-gate {
18000Sstevel@tonic-gate 	conn_t	*sctp_connp = (conn_t *)buf;
18010Sstevel@tonic-gate 	sctp_t	*sctp = (sctp_t *)&sctp_connp[1];
18020Sstevel@tonic-gate 
18030Sstevel@tonic-gate 	bzero(buf, (char *)&sctp[1] - (char *)buf);
18040Sstevel@tonic-gate 
18050Sstevel@tonic-gate 	ASSERT(sctp_g_q != NULL);
18060Sstevel@tonic-gate 	sctp->sctp_connp = sctp_connp;
18070Sstevel@tonic-gate 	mutex_init(&sctp->sctp_reflock, NULL, MUTEX_DEFAULT, NULL);
18080Sstevel@tonic-gate 	mutex_init(&sctp->sctp_lock, NULL, MUTEX_DEFAULT, NULL);
18090Sstevel@tonic-gate 	mutex_init(&sctp->sctp_recvq_lock, NULL, MUTEX_DEFAULT, NULL);
18100Sstevel@tonic-gate 	cv_init(&sctp->sctp_cv, NULL, CV_DEFAULT, NULL);
18110Sstevel@tonic-gate 	mutex_init(&sctp->sctp_sendq_lock, NULL, MUTEX_DEFAULT, NULL);
18120Sstevel@tonic-gate 
18130Sstevel@tonic-gate 	sctp_connp->conn_rq = sctp_connp->conn_wq = NULL;
18140Sstevel@tonic-gate 	sctp_connp->conn_multicast_loop = IP_DEFAULT_MULTICAST_LOOP;
18150Sstevel@tonic-gate 	sctp_connp->conn_ulp = IPPROTO_SCTP;
18160Sstevel@tonic-gate 	mutex_init(&sctp_connp->conn_lock, NULL, MUTEX_DEFAULT, NULL);
18170Sstevel@tonic-gate 	cv_init(&sctp_connp->conn_cv, NULL, CV_DEFAULT, NULL);
18180Sstevel@tonic-gate 
18190Sstevel@tonic-gate 	return (0);
18200Sstevel@tonic-gate }
18210Sstevel@tonic-gate 
18220Sstevel@tonic-gate /* ARGSUSED */
18230Sstevel@tonic-gate static void
18240Sstevel@tonic-gate sctp_conn_cache_destructor(void *buf, void *cdrarg)
18250Sstevel@tonic-gate {
18260Sstevel@tonic-gate 	conn_t	*sctp_connp = (conn_t *)buf;
18270Sstevel@tonic-gate 	sctp_t	*sctp = (sctp_t *)&sctp_connp[1];
18280Sstevel@tonic-gate 
18290Sstevel@tonic-gate 	ASSERT(!MUTEX_HELD(&sctp->sctp_lock));
18300Sstevel@tonic-gate 	ASSERT(!MUTEX_HELD(&sctp->sctp_reflock));
18310Sstevel@tonic-gate 	ASSERT(!MUTEX_HELD(&sctp->sctp_recvq_lock));
18320Sstevel@tonic-gate 	ASSERT(!MUTEX_HELD(&sctp->sctp_sendq_lock));
18330Sstevel@tonic-gate 	ASSERT(!MUTEX_HELD(&sctp->sctp_connp->conn_lock));
18340Sstevel@tonic-gate 
18350Sstevel@tonic-gate 	ASSERT(sctp->sctp_conn_hash_next == NULL);
18360Sstevel@tonic-gate 	ASSERT(sctp->sctp_conn_hash_prev == NULL);
18370Sstevel@tonic-gate 	ASSERT(sctp->sctp_listen_hash_next == NULL);
18380Sstevel@tonic-gate 	ASSERT(sctp->sctp_listen_hash_prev == NULL);
18390Sstevel@tonic-gate 	ASSERT(sctp->sctp_listen_tfp == NULL);
18400Sstevel@tonic-gate 	ASSERT(sctp->sctp_conn_tfp == NULL);
18410Sstevel@tonic-gate 
18420Sstevel@tonic-gate 	ASSERT(sctp->sctp_faddrs == NULL);
18430Sstevel@tonic-gate 	ASSERT(sctp->sctp_nsaddrs == 0);
18440Sstevel@tonic-gate 
18450Sstevel@tonic-gate 	ASSERT(sctp->sctp_ulpd == NULL);
18460Sstevel@tonic-gate 
18470Sstevel@tonic-gate 	ASSERT(sctp->sctp_lastfaddr == NULL);
18480Sstevel@tonic-gate 	ASSERT(sctp->sctp_primary == NULL);
18490Sstevel@tonic-gate 	ASSERT(sctp->sctp_current == NULL);
18500Sstevel@tonic-gate 	ASSERT(sctp->sctp_lastdata == NULL);
18510Sstevel@tonic-gate 
18520Sstevel@tonic-gate 	ASSERT(sctp->sctp_xmit_head == NULL);
18530Sstevel@tonic-gate 	ASSERT(sctp->sctp_xmit_tail == NULL);
18540Sstevel@tonic-gate 	ASSERT(sctp->sctp_xmit_unsent == NULL);
18550Sstevel@tonic-gate 	ASSERT(sctp->sctp_xmit_unsent_tail == NULL);
18560Sstevel@tonic-gate 
18570Sstevel@tonic-gate 	ASSERT(sctp->sctp_ostrcntrs == NULL);
18580Sstevel@tonic-gate 
18590Sstevel@tonic-gate 	ASSERT(sctp->sctp_sack_info == NULL);
18600Sstevel@tonic-gate 	ASSERT(sctp->sctp_ack_mp == NULL);
18610Sstevel@tonic-gate 	ASSERT(sctp->sctp_instr == NULL);
18620Sstevel@tonic-gate 
18630Sstevel@tonic-gate 	ASSERT(sctp->sctp_iphc == NULL);
18640Sstevel@tonic-gate 	ASSERT(sctp->sctp_iphc6 == NULL);
18650Sstevel@tonic-gate 	ASSERT(sctp->sctp_ipha == NULL);
18660Sstevel@tonic-gate 	ASSERT(sctp->sctp_ip6h == NULL);
18670Sstevel@tonic-gate 	ASSERT(sctp->sctp_sctph == NULL);
18680Sstevel@tonic-gate 	ASSERT(sctp->sctp_sctph6 == NULL);
18690Sstevel@tonic-gate 
18700Sstevel@tonic-gate 	ASSERT(sctp->sctp_cookie_mp == NULL);
18710Sstevel@tonic-gate 
18720Sstevel@tonic-gate 	ASSERT(sctp->sctp_refcnt == 0);
18730Sstevel@tonic-gate 	ASSERT(sctp->sctp_timer_mp == NULL);
18740Sstevel@tonic-gate 	ASSERT(sctp->sctp_connp->conn_ref == 0);
18750Sstevel@tonic-gate 	ASSERT(sctp->sctp_heartbeat_mp == NULL);
18760Sstevel@tonic-gate 	ASSERT(sctp->sctp_ptpbhn == NULL && sctp->sctp_bind_hash == NULL);
18770Sstevel@tonic-gate 
18780Sstevel@tonic-gate 	ASSERT(sctp->sctp_shutdown_faddr == NULL);
18790Sstevel@tonic-gate 
18800Sstevel@tonic-gate 	ASSERT(sctp->sctp_cxmit_list == NULL);
18810Sstevel@tonic-gate 
18820Sstevel@tonic-gate 	ASSERT(sctp->sctp_recvq == NULL);
18830Sstevel@tonic-gate 	ASSERT(sctp->sctp_recvq_tail == NULL);
18840Sstevel@tonic-gate 	ASSERT(sctp->sctp_recvq_tq == NULL);
18850Sstevel@tonic-gate 
18860Sstevel@tonic-gate 	ASSERT(sctp->sctp_sendq == NULL);
18870Sstevel@tonic-gate 	ASSERT(sctp->sctp_sendq_tail == NULL);
18880Sstevel@tonic-gate 	ASSERT(sctp->sctp_sendq_sending == B_FALSE);
18890Sstevel@tonic-gate 
18900Sstevel@tonic-gate 	mutex_destroy(&sctp->sctp_reflock);
18910Sstevel@tonic-gate 	mutex_destroy(&sctp->sctp_lock);
18920Sstevel@tonic-gate 	mutex_destroy(&sctp->sctp_recvq_lock);
18930Sstevel@tonic-gate 	cv_destroy(&sctp->sctp_cv);
18940Sstevel@tonic-gate 	mutex_destroy(&sctp->sctp_sendq_lock);
18950Sstevel@tonic-gate 
18960Sstevel@tonic-gate 	mutex_destroy(&sctp_connp->conn_lock);
18970Sstevel@tonic-gate 	cv_destroy(&sctp_connp->conn_cv);
18980Sstevel@tonic-gate }
18990Sstevel@tonic-gate 
19000Sstevel@tonic-gate static void
19010Sstevel@tonic-gate sctp_conn_cache_init()
19020Sstevel@tonic-gate {
19030Sstevel@tonic-gate 	sctp_conn_cache = kmem_cache_create("sctp_conn_cache",
19040Sstevel@tonic-gate 	    sizeof (sctp_t) + sizeof (conn_t), 0, sctp_conn_cache_constructor,
19050Sstevel@tonic-gate 	    sctp_conn_cache_destructor, NULL, NULL, NULL, 0);
19060Sstevel@tonic-gate }
19070Sstevel@tonic-gate 
19080Sstevel@tonic-gate static void
19090Sstevel@tonic-gate sctp_conn_cache_fini()
19100Sstevel@tonic-gate {
19110Sstevel@tonic-gate 	kmem_cache_destroy(sctp_conn_cache);
19120Sstevel@tonic-gate }
1913