xref: /onnv-gate/usr/src/uts/common/inet/sctp/sctp.c (revision 11042)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51213Skcpoon  * Common Development and Distribution License (the "License").
61213Skcpoon  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
211213Skcpoon 
220Sstevel@tonic-gate /*
238778SErik.Nordmark@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #include <sys/types.h>
280Sstevel@tonic-gate #include <sys/stream.h>
290Sstevel@tonic-gate #include <sys/strsubr.h>
300Sstevel@tonic-gate #include <sys/stropts.h>
310Sstevel@tonic-gate #include <sys/strsun.h>
320Sstevel@tonic-gate #define	_SUN_TPI_VERSION 2
330Sstevel@tonic-gate #include <sys/tihdr.h>
340Sstevel@tonic-gate #include <sys/ddi.h>
350Sstevel@tonic-gate #include <sys/sunddi.h>
360Sstevel@tonic-gate #include <sys/xti_inet.h>
370Sstevel@tonic-gate #include <sys/cmn_err.h>
380Sstevel@tonic-gate #include <sys/debug.h>
390Sstevel@tonic-gate #include <sys/vtrace.h>
400Sstevel@tonic-gate #include <sys/kmem.h>
410Sstevel@tonic-gate #include <sys/cpuvar.h>
420Sstevel@tonic-gate #include <sys/random.h>
431676Sjpk #include <sys/priv.h>
443448Sdh155122 #include <sys/sunldi.h>
450Sstevel@tonic-gate 
460Sstevel@tonic-gate #include <sys/errno.h>
470Sstevel@tonic-gate #include <sys/signal.h>
480Sstevel@tonic-gate #include <sys/socket.h>
490Sstevel@tonic-gate #include <sys/isa_defs.h>
500Sstevel@tonic-gate #include <netinet/in.h>
510Sstevel@tonic-gate #include <netinet/tcp.h>
520Sstevel@tonic-gate #include <netinet/ip6.h>
530Sstevel@tonic-gate #include <netinet/icmp6.h>
540Sstevel@tonic-gate #include <netinet/sctp.h>
550Sstevel@tonic-gate #include <net/if.h>
560Sstevel@tonic-gate 
570Sstevel@tonic-gate #include <inet/common.h>
580Sstevel@tonic-gate #include <inet/ip.h>
59*11042SErik.Nordmark@Sun.COM #include <inet/ip_if.h>
60*11042SErik.Nordmark@Sun.COM #include <inet/ip_ire.h>
610Sstevel@tonic-gate #include <inet/ip6.h>
620Sstevel@tonic-gate #include <inet/mi.h>
630Sstevel@tonic-gate #include <inet/mib2.h>
643448Sdh155122 #include <inet/kstatcom.h>
650Sstevel@tonic-gate #include <inet/nd.h>
660Sstevel@tonic-gate #include <inet/optcom.h>
670Sstevel@tonic-gate #include <inet/ipclassifier.h>
680Sstevel@tonic-gate #include <inet/ipsec_impl.h>
690Sstevel@tonic-gate #include <inet/sctp_ip.h>
700Sstevel@tonic-gate #include <inet/sctp_crc32.h>
710Sstevel@tonic-gate 
720Sstevel@tonic-gate #include "sctp_impl.h"
730Sstevel@tonic-gate #include "sctp_addr.h"
74852Svi117747 #include "sctp_asconf.h"
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 static void	sctp_closei_local(sctp_t *sctp);
800Sstevel@tonic-gate static int	sctp_init_values(sctp_t *, sctp_t *, int);
810Sstevel@tonic-gate static void	sctp_icmp_error_ipv6(sctp_t *sctp, mblk_t *mp);
820Sstevel@tonic-gate static void	sctp_process_recvq(void *);
833448Sdh155122 static void	sctp_rq_tq_init(sctp_stack_t *);
843448Sdh155122 static void	sctp_rq_tq_fini(sctp_stack_t *);
850Sstevel@tonic-gate static void	sctp_conn_cache_init();
860Sstevel@tonic-gate static void	sctp_conn_cache_fini();
870Sstevel@tonic-gate static int	sctp_conn_cache_constructor();
880Sstevel@tonic-gate static void	sctp_conn_cache_destructor();
894691Skcpoon static void	sctp_conn_clear(conn_t *);
90*11042SErik.Nordmark@Sun.COM static void	sctp_notify(void *, ip_xmit_attr_t *, ixa_notify_type_t,
91*11042SErik.Nordmark@Sun.COM     ixa_notify_arg_t);
923448Sdh155122 
933448Sdh155122 static void	*sctp_stack_init(netstackid_t stackid, netstack_t *ns);
943448Sdh155122 static void	sctp_stack_fini(netstackid_t stackid, void *arg);
950Sstevel@tonic-gate 
960Sstevel@tonic-gate /*
970Sstevel@tonic-gate  * SCTP receive queue taskq
980Sstevel@tonic-gate  *
990Sstevel@tonic-gate  * At SCTP initialization time, a default taskq is created for
1000Sstevel@tonic-gate  * servicing packets received when the interrupt thread cannot
1010Sstevel@tonic-gate  * get a hold on the sctp_t.  The number of taskq can be increased in
1020Sstevel@tonic-gate  * sctp_find_next_tq() when an existing taskq cannot be dispatched.
1030Sstevel@tonic-gate  * The taskqs are never removed.  But the max number of taskq which
1040Sstevel@tonic-gate  * can be created is controlled by sctp_recvq_tq_list_max_sz.  Note
1050Sstevel@tonic-gate  * that SCTP recvq taskq is not tied to any specific CPU or ill.
1060Sstevel@tonic-gate  *
1070Sstevel@tonic-gate  * Those taskqs are stored in an array recvq_tq_list.  And they are
1080Sstevel@tonic-gate  * used in a round robin fashion.  The current taskq being used is
1090Sstevel@tonic-gate  * determined by recvq_tq_list_cur.
1100Sstevel@tonic-gate  */
1110Sstevel@tonic-gate 
1123448Sdh155122 /* /etc/system variables */
1130Sstevel@tonic-gate /* The minimum number of threads for each taskq. */
1140Sstevel@tonic-gate int		sctp_recvq_tq_thr_min = 4;
1150Sstevel@tonic-gate /* The maximum number of threads for each taskq. */
1160Sstevel@tonic-gate int		sctp_recvq_tq_thr_max = 16;
1170Sstevel@tonic-gate /* The minimum number of tasks for each taskq. */
1180Sstevel@tonic-gate int		sctp_recvq_tq_task_min = 5;
1190Sstevel@tonic-gate /* The maxiimum number of tasks for each taskq. */
1200Sstevel@tonic-gate int		sctp_recvq_tq_task_max = 50;
1210Sstevel@tonic-gate 
1220Sstevel@tonic-gate /*  sctp_t/conn_t kmem cache */
1230Sstevel@tonic-gate struct kmem_cache	*sctp_conn_cache;
1240Sstevel@tonic-gate 
1250Sstevel@tonic-gate #define	SCTP_CONDEMNED(sctp)				\
1260Sstevel@tonic-gate 	mutex_enter(&(sctp)->sctp_reflock);		\
1270Sstevel@tonic-gate 	((sctp)->sctp_condemned = B_TRUE);		\
1281213Skcpoon 	mutex_exit(&(sctp)->sctp_reflock);
1290Sstevel@tonic-gate 
1300Sstevel@tonic-gate /* Link/unlink a sctp_t to/from the global list. */
1313448Sdh155122 #define	SCTP_LINK(sctp, sctps)				\
1323448Sdh155122 	mutex_enter(&(sctps)->sctps_g_lock);		\
1333448Sdh155122 	list_insert_tail(&sctps->sctps_g_list, (sctp));	\
1343448Sdh155122 	mutex_exit(&(sctps)->sctps_g_lock);
1350Sstevel@tonic-gate 
1363448Sdh155122 #define	SCTP_UNLINK(sctp, sctps)				\
1373448Sdh155122 	mutex_enter(&(sctps)->sctps_g_lock);		\
1380Sstevel@tonic-gate 	ASSERT((sctp)->sctp_condemned);			\
1393448Sdh155122 	list_remove(&(sctps)->sctps_g_list, (sctp));	\
1403448Sdh155122 	mutex_exit(&(sctps)->sctps_g_lock);
1410Sstevel@tonic-gate 
1420Sstevel@tonic-gate /*
143852Svi117747  * Hooks for Sun Cluster. On non-clustered nodes these will remain NULL.
144852Svi117747  * PSARC/2005/602.
145852Svi117747  */
146852Svi117747 void (*cl_sctp_listen)(sa_family_t, uchar_t *, uint_t, in_port_t) = NULL;
147852Svi117747 void (*cl_sctp_unlisten)(sa_family_t, uchar_t *, uint_t, in_port_t) = NULL;
148852Svi117747 void (*cl_sctp_connect)(sa_family_t, uchar_t *, uint_t, in_port_t,
149852Svi117747     uchar_t *, uint_t, in_port_t, boolean_t, cl_sctp_handle_t) = NULL;
150852Svi117747 void (*cl_sctp_disconnect)(sa_family_t, cl_sctp_handle_t) = NULL;
151852Svi117747 void (*cl_sctp_assoc_change)(sa_family_t, uchar_t *, size_t, uint_t,
152852Svi117747     uchar_t *, size_t, uint_t, int, cl_sctp_handle_t) = NULL;
153852Svi117747 void (*cl_sctp_check_addrs)(sa_family_t, in_port_t, uchar_t **, size_t,
154852Svi117747     uint_t *, boolean_t) = NULL;
155852Svi117747 /*
1560Sstevel@tonic-gate  * Return the version number of the SCTP kernel interface.
1570Sstevel@tonic-gate  */
1580Sstevel@tonic-gate int
1590Sstevel@tonic-gate sctp_itf_ver(int cl_ver)
1600Sstevel@tonic-gate {
1610Sstevel@tonic-gate 	if (cl_ver != SCTP_ITF_VER)
1620Sstevel@tonic-gate 		return (-1);
1630Sstevel@tonic-gate 	return (SCTP_ITF_VER);
1640Sstevel@tonic-gate }
1650Sstevel@tonic-gate 
1660Sstevel@tonic-gate /*
1670Sstevel@tonic-gate  * Called when we need a new sctp instantiation but don't really have a
1680Sstevel@tonic-gate  * new q to hang it off of. Copy the priv flag from the passed in structure.
1690Sstevel@tonic-gate  */
1700Sstevel@tonic-gate sctp_t *
1710Sstevel@tonic-gate sctp_create_eager(sctp_t *psctp)
1720Sstevel@tonic-gate {
1730Sstevel@tonic-gate 	sctp_t	*sctp;
1740Sstevel@tonic-gate 	mblk_t	*ack_mp, *hb_mp;
175*11042SErik.Nordmark@Sun.COM 	conn_t	*connp;
176*11042SErik.Nordmark@Sun.COM 	cred_t	*credp;
1773448Sdh155122 	sctp_stack_t	*sctps = psctp->sctp_sctps;
1780Sstevel@tonic-gate 
1793448Sdh155122 	if ((connp = ipcl_conn_create(IPCL_SCTPCONN, KM_NOSLEEP,
1804505Skcpoon 	    sctps->sctps_netstack)) == NULL) {
1810Sstevel@tonic-gate 		return (NULL);
1823448Sdh155122 	}
1831676Sjpk 
1840Sstevel@tonic-gate 	sctp = CONN2SCTP(connp);
1853448Sdh155122 	sctp->sctp_sctps = sctps;
1860Sstevel@tonic-gate 
1874691Skcpoon 	if ((ack_mp = sctp_timer_alloc(sctp, sctp_ack_timer,
1884691Skcpoon 	    KM_NOSLEEP)) == NULL ||
1894691Skcpoon 	    (hb_mp = sctp_timer_alloc(sctp, sctp_heartbeat_timer,
1904691Skcpoon 	    KM_NOSLEEP)) == NULL) {
1910Sstevel@tonic-gate 		if (ack_mp != NULL)
1920Sstevel@tonic-gate 			freeb(ack_mp);
1934691Skcpoon 		sctp_conn_clear(connp);
1943448Sdh155122 		sctp->sctp_sctps = NULL;
1950Sstevel@tonic-gate 		kmem_cache_free(sctp_conn_cache, connp);
1960Sstevel@tonic-gate 		return (NULL);
1970Sstevel@tonic-gate 	}
1980Sstevel@tonic-gate 
1990Sstevel@tonic-gate 	sctp->sctp_ack_mp = ack_mp;
2000Sstevel@tonic-gate 	sctp->sctp_heartbeat_mp = hb_mp;
2010Sstevel@tonic-gate 
2020Sstevel@tonic-gate 	if (sctp_init_values(sctp, psctp, KM_NOSLEEP) != 0) {
2030Sstevel@tonic-gate 		freeb(ack_mp);
2040Sstevel@tonic-gate 		freeb(hb_mp);
2054691Skcpoon 		sctp_conn_clear(connp);
2063448Sdh155122 		sctp->sctp_sctps = NULL;
2070Sstevel@tonic-gate 		kmem_cache_free(sctp_conn_cache, connp);
2080Sstevel@tonic-gate 		return (NULL);
2090Sstevel@tonic-gate 	}
2101676Sjpk 
211*11042SErik.Nordmark@Sun.COM 	if ((credp = psctp->sctp_connp->conn_cred) != NULL) {
2121676Sjpk 		connp->conn_cred = credp;
2131676Sjpk 		crhold(credp);
2140Sstevel@tonic-gate 	}
2152263Ssommerfe 
2160Sstevel@tonic-gate 	sctp->sctp_mss = psctp->sctp_mss;
2170Sstevel@tonic-gate 	sctp->sctp_detached = B_TRUE;
2180Sstevel@tonic-gate 	/*
2190Sstevel@tonic-gate 	 * Link to the global as soon as possible so that this sctp_t
2200Sstevel@tonic-gate 	 * can be found.
2210Sstevel@tonic-gate 	 */
2223448Sdh155122 	SCTP_LINK(sctp, sctps);
2230Sstevel@tonic-gate 
2240Sstevel@tonic-gate 	return (sctp);
2250Sstevel@tonic-gate }
2260Sstevel@tonic-gate 
2270Sstevel@tonic-gate /*
2280Sstevel@tonic-gate  * We are dying for some reason.  Try to do it gracefully.
2290Sstevel@tonic-gate  */
2300Sstevel@tonic-gate void
2310Sstevel@tonic-gate sctp_clean_death(sctp_t *sctp, int err)
2320Sstevel@tonic-gate {
2330Sstevel@tonic-gate 	ASSERT(sctp != NULL);
2340Sstevel@tonic-gate 
2351676Sjpk 	dprint(3, ("sctp_clean_death %p, state %d\n", (void *)sctp,
2361676Sjpk 	    sctp->sctp_state));
2370Sstevel@tonic-gate 
2380Sstevel@tonic-gate 	sctp->sctp_client_errno = err;
2390Sstevel@tonic-gate 	/*
2400Sstevel@tonic-gate 	 * Check to see if we need to notify upper layer.
2410Sstevel@tonic-gate 	 */
2420Sstevel@tonic-gate 	if ((sctp->sctp_state >= SCTPS_COOKIE_WAIT) &&
2430Sstevel@tonic-gate 	    !SCTP_IS_DETACHED(sctp)) {
2440Sstevel@tonic-gate 		if (sctp->sctp_xmit_head || sctp->sctp_xmit_unsent) {
2450Sstevel@tonic-gate 			sctp_regift_xmitlist(sctp);
2460Sstevel@tonic-gate 		}
2478348SEric.Yu@Sun.COM 		if (sctp->sctp_ulp_disconnected(sctp->sctp_ulpd, 0, err)) {
2480Sstevel@tonic-gate 			/*
2490Sstevel@tonic-gate 			 * Socket is gone, detach.
2500Sstevel@tonic-gate 			 */
2510Sstevel@tonic-gate 			sctp->sctp_detached = B_TRUE;
2520Sstevel@tonic-gate 			sctp->sctp_ulpd = NULL;
2538348SEric.Yu@Sun.COM 			sctp->sctp_upcalls = NULL;
2540Sstevel@tonic-gate 		}
2550Sstevel@tonic-gate 	}
2560Sstevel@tonic-gate 
2570Sstevel@tonic-gate 	/* Remove this sctp from all hashes. */
2580Sstevel@tonic-gate 	sctp_closei_local(sctp);
2590Sstevel@tonic-gate 
2600Sstevel@tonic-gate 	/*
2610Sstevel@tonic-gate 	 * If the sctp_t is detached, we need to finish freeing up
2620Sstevel@tonic-gate 	 * the resources.  At this point, ip_fanout_sctp() should have
2630Sstevel@tonic-gate 	 * a hold on this sctp_t.  Some thread doing snmp stuff can
2640Sstevel@tonic-gate 	 * have a hold.  And a taskq can also have a hold waiting to
2650Sstevel@tonic-gate 	 * work.  sctp_unlink() the sctp_t from the global list so
2660Sstevel@tonic-gate 	 * that no new thread can find it.  Then do a SCTP_REFRELE().
2670Sstevel@tonic-gate 	 * The sctp_t will be freed after all those threads are done.
2680Sstevel@tonic-gate 	 */
2690Sstevel@tonic-gate 	if (SCTP_IS_DETACHED(sctp)) {
2700Sstevel@tonic-gate 		SCTP_CONDEMNED(sctp);
2710Sstevel@tonic-gate 		SCTP_REFRELE(sctp);
2720Sstevel@tonic-gate 	}
2730Sstevel@tonic-gate }
2740Sstevel@tonic-gate 
2750Sstevel@tonic-gate /*
2760Sstevel@tonic-gate  * Called by upper layer when it wants to close this association.
2770Sstevel@tonic-gate  * Depending on the state of this assoication, we need to do
2780Sstevel@tonic-gate  * different things.
2790Sstevel@tonic-gate  *
2800Sstevel@tonic-gate  * If the state is below COOKIE_ECHOED or it is COOKIE_ECHOED but with
2810Sstevel@tonic-gate  * no sent data, just remove this sctp from all the hashes.  This
2820Sstevel@tonic-gate  * makes sure that all packets from the other end will go to the default
2830Sstevel@tonic-gate  * sctp handling.  The upper layer will then do a sctp_close() to clean
2840Sstevel@tonic-gate  * up.
2850Sstevel@tonic-gate  *
2860Sstevel@tonic-gate  * Otherwise, check and see if SO_LINGER is set.  If it is set, check
2870Sstevel@tonic-gate  * the value.  If the value is 0, consider this an abortive close.  Send
2880Sstevel@tonic-gate  * an ABORT message and kill the associatiion.
2890Sstevel@tonic-gate  *
2900Sstevel@tonic-gate  */
2910Sstevel@tonic-gate int
2920Sstevel@tonic-gate sctp_disconnect(sctp_t *sctp)
2930Sstevel@tonic-gate {
294*11042SErik.Nordmark@Sun.COM 	int		error = 0;
295*11042SErik.Nordmark@Sun.COM 	conn_t		*connp = sctp->sctp_connp;
2960Sstevel@tonic-gate 
2971676Sjpk 	dprint(3, ("sctp_disconnect %p, state %d\n", (void *)sctp,
2981676Sjpk 	    sctp->sctp_state));
2990Sstevel@tonic-gate 
3000Sstevel@tonic-gate 	RUN_SCTP(sctp);
3010Sstevel@tonic-gate 
3020Sstevel@tonic-gate 	switch (sctp->sctp_state) {
3030Sstevel@tonic-gate 	case SCTPS_IDLE:
3040Sstevel@tonic-gate 	case SCTPS_BOUND:
3050Sstevel@tonic-gate 	case SCTPS_LISTEN:
3060Sstevel@tonic-gate 		break;
3070Sstevel@tonic-gate 	case SCTPS_COOKIE_WAIT:
3080Sstevel@tonic-gate 	case SCTPS_COOKIE_ECHOED:
3090Sstevel@tonic-gate 		/*
3100Sstevel@tonic-gate 		 * Close during the connect 3-way handshake
3110Sstevel@tonic-gate 		 * but here there may or may not be pending data
3120Sstevel@tonic-gate 		 * already on queue. Process almost same as in
3130Sstevel@tonic-gate 		 * the ESTABLISHED state.
3140Sstevel@tonic-gate 		 */
3150Sstevel@tonic-gate 		if (sctp->sctp_xmit_head == NULL &&
3160Sstevel@tonic-gate 		    sctp->sctp_xmit_unsent == NULL) {
3170Sstevel@tonic-gate 			break;
3180Sstevel@tonic-gate 		}
3190Sstevel@tonic-gate 		/* FALLTHRU */
3200Sstevel@tonic-gate 	default:
3210Sstevel@tonic-gate 		/*
3227480SKacheong.Poon@Sun.COM 		 * If SO_LINGER has set a zero linger time, terminate the
3237480SKacheong.Poon@Sun.COM 		 * association and send an ABORT.
3240Sstevel@tonic-gate 		 */
325*11042SErik.Nordmark@Sun.COM 		if (connp->conn_linger && connp->conn_lingertime == 0) {
3267480SKacheong.Poon@Sun.COM 			sctp_user_abort(sctp, NULL);
3277480SKacheong.Poon@Sun.COM 			WAKE_SCTP(sctp);
3287480SKacheong.Poon@Sun.COM 			return (error);
3290Sstevel@tonic-gate 		}
3300Sstevel@tonic-gate 
3310Sstevel@tonic-gate 		/*
3327480SKacheong.Poon@Sun.COM 		 * In there is unread data, send an ABORT and terminate the
3337480SKacheong.Poon@Sun.COM 		 * association.
334852Svi117747 		 */
335852Svi117747 		if (sctp->sctp_rxqueued > 0 || sctp->sctp_irwnd >
336852Svi117747 		    sctp->sctp_rwnd) {
3377480SKacheong.Poon@Sun.COM 			sctp_user_abort(sctp, NULL);
3387480SKacheong.Poon@Sun.COM 			WAKE_SCTP(sctp);
3397480SKacheong.Poon@Sun.COM 			return (error);
340852Svi117747 		}
341852Svi117747 		/*
3420Sstevel@tonic-gate 		 * Transmit the shutdown before detaching the sctp_t.
3430Sstevel@tonic-gate 		 * After sctp_detach returns this queue/perimeter
3440Sstevel@tonic-gate 		 * no longer owns the sctp_t thus others can modify it.
3450Sstevel@tonic-gate 		 */
3460Sstevel@tonic-gate 		sctp_send_shutdown(sctp, 0);
3470Sstevel@tonic-gate 
3480Sstevel@tonic-gate 		/* Pass gathered wisdom to IP for keeping */
349*11042SErik.Nordmark@Sun.COM 		sctp_update_dce(sctp);
3500Sstevel@tonic-gate 
3510Sstevel@tonic-gate 		/*
3520Sstevel@tonic-gate 		 * If lingering on close then wait until the shutdown
3530Sstevel@tonic-gate 		 * is complete, or the SO_LINGER time passes, or an
3540Sstevel@tonic-gate 		 * ABORT is sent/received.  Note that sctp_disconnect()
3550Sstevel@tonic-gate 		 * can be called more than once.  Make sure that only
3560Sstevel@tonic-gate 		 * one thread waits.
3570Sstevel@tonic-gate 		 */
358*11042SErik.Nordmark@Sun.COM 		if (connp->conn_linger && connp->conn_lingertime > 0 &&
3590Sstevel@tonic-gate 		    sctp->sctp_state >= SCTPS_ESTABLISHED &&
3600Sstevel@tonic-gate 		    !sctp->sctp_lingering) {
3610Sstevel@tonic-gate 			clock_t stoptime;	/* in ticks */
3620Sstevel@tonic-gate 			clock_t ret;
3630Sstevel@tonic-gate 
3640Sstevel@tonic-gate 			sctp->sctp_lingering = 1;
3650Sstevel@tonic-gate 			sctp->sctp_client_errno = 0;
366*11042SErik.Nordmark@Sun.COM 			stoptime = lbolt + connp->conn_lingertime * hz;
3670Sstevel@tonic-gate 
3680Sstevel@tonic-gate 			mutex_enter(&sctp->sctp_lock);
3690Sstevel@tonic-gate 			sctp->sctp_running = B_FALSE;
3700Sstevel@tonic-gate 			while (sctp->sctp_state >= SCTPS_ESTABLISHED &&
3710Sstevel@tonic-gate 			    sctp->sctp_client_errno == 0) {
3720Sstevel@tonic-gate 				cv_broadcast(&sctp->sctp_cv);
3730Sstevel@tonic-gate 				ret = cv_timedwait_sig(&sctp->sctp_cv,
3740Sstevel@tonic-gate 				    &sctp->sctp_lock, stoptime);
3750Sstevel@tonic-gate 				if (ret < 0) {
3760Sstevel@tonic-gate 					/* Stoptime has reached. */
3770Sstevel@tonic-gate 					sctp->sctp_client_errno = EWOULDBLOCK;
3780Sstevel@tonic-gate 					break;
3790Sstevel@tonic-gate 				} else if (ret == 0) {
3800Sstevel@tonic-gate 					/* Got a signal. */
3810Sstevel@tonic-gate 					break;
3820Sstevel@tonic-gate 				}
3830Sstevel@tonic-gate 			}
3840Sstevel@tonic-gate 			error = sctp->sctp_client_errno;
3850Sstevel@tonic-gate 			sctp->sctp_client_errno = 0;
3860Sstevel@tonic-gate 			mutex_exit(&sctp->sctp_lock);
3870Sstevel@tonic-gate 		}
3880Sstevel@tonic-gate 
3890Sstevel@tonic-gate 		WAKE_SCTP(sctp);
3900Sstevel@tonic-gate 		return (error);
3910Sstevel@tonic-gate 	}
3920Sstevel@tonic-gate 
3930Sstevel@tonic-gate 
3940Sstevel@tonic-gate 	/* Remove this sctp from all hashes so nobody can find it. */
3950Sstevel@tonic-gate 	sctp_closei_local(sctp);
3960Sstevel@tonic-gate 	WAKE_SCTP(sctp);
3970Sstevel@tonic-gate 	return (error);
3980Sstevel@tonic-gate }
3990Sstevel@tonic-gate 
4000Sstevel@tonic-gate void
4010Sstevel@tonic-gate sctp_close(sctp_t *sctp)
4020Sstevel@tonic-gate {
4031676Sjpk 	dprint(3, ("sctp_close %p, state %d\n", (void *)sctp,
4041676Sjpk 	    sctp->sctp_state));
4050Sstevel@tonic-gate 
4060Sstevel@tonic-gate 	RUN_SCTP(sctp);
4070Sstevel@tonic-gate 	sctp->sctp_detached = 1;
4080Sstevel@tonic-gate 	sctp->sctp_ulpd = NULL;
4098348SEric.Yu@Sun.COM 	sctp->sctp_upcalls = NULL;
4100Sstevel@tonic-gate 	bzero(&sctp->sctp_events, sizeof (sctp->sctp_events));
4110Sstevel@tonic-gate 
4120Sstevel@tonic-gate 	/* If the graceful shutdown has not been completed, just return. */
4130Sstevel@tonic-gate 	if (sctp->sctp_state != SCTPS_IDLE) {
4140Sstevel@tonic-gate 		WAKE_SCTP(sctp);
4150Sstevel@tonic-gate 		return;
4160Sstevel@tonic-gate 	}
4170Sstevel@tonic-gate 
4180Sstevel@tonic-gate 	/*
4190Sstevel@tonic-gate 	 * Since sctp_t is in SCTPS_IDLE state, so the only thread which
4200Sstevel@tonic-gate 	 * can have a hold on the sctp_t is doing snmp stuff.  Just do
4210Sstevel@tonic-gate 	 * a SCTP_REFRELE() here after the SCTP_UNLINK().  It will
4220Sstevel@tonic-gate 	 * be freed when the other thread is done.
4230Sstevel@tonic-gate 	 */
4240Sstevel@tonic-gate 	SCTP_CONDEMNED(sctp);
4250Sstevel@tonic-gate 	WAKE_SCTP(sctp);
4260Sstevel@tonic-gate 	SCTP_REFRELE(sctp);
4270Sstevel@tonic-gate }
4280Sstevel@tonic-gate 
4290Sstevel@tonic-gate /*
4300Sstevel@tonic-gate  * Unlink from global list and do the eager close.
4310Sstevel@tonic-gate  * Remove the refhold implicit in being on the global list.
4320Sstevel@tonic-gate  */
4330Sstevel@tonic-gate void
4340Sstevel@tonic-gate sctp_close_eager(sctp_t *sctp)
4350Sstevel@tonic-gate {
4360Sstevel@tonic-gate 	SCTP_CONDEMNED(sctp);
4370Sstevel@tonic-gate 	sctp_closei_local(sctp);
4380Sstevel@tonic-gate 	SCTP_REFRELE(sctp);
4390Sstevel@tonic-gate }
4400Sstevel@tonic-gate 
4410Sstevel@tonic-gate /*
4420Sstevel@tonic-gate  * The sctp_t is going away. Remove it from all lists and set it
4430Sstevel@tonic-gate  * to SCTPS_IDLE. The caller has to remove it from the
4440Sstevel@tonic-gate  * global list. The freeing up of memory is deferred until
4450Sstevel@tonic-gate  * sctp_free(). This is needed since a thread in sctp_input() might have
4460Sstevel@tonic-gate  * done a SCTP_REFHOLD on this structure before it was removed from the
4470Sstevel@tonic-gate  * hashes.
4480Sstevel@tonic-gate  */
4490Sstevel@tonic-gate static void
4500Sstevel@tonic-gate sctp_closei_local(sctp_t *sctp)
4510Sstevel@tonic-gate {
4520Sstevel@tonic-gate 	mblk_t	*mp;
4530Sstevel@tonic-gate 	conn_t	*connp = sctp->sctp_connp;
4540Sstevel@tonic-gate 
4554505Skcpoon 	/* Sanity check, don't do the same thing twice.  */
4564505Skcpoon 	if (connp->conn_state_flags & CONN_CLOSING) {
4574505Skcpoon 		ASSERT(sctp->sctp_state == SCTPS_IDLE);
4584505Skcpoon 		return;
4594505Skcpoon 	}
4604505Skcpoon 
4610Sstevel@tonic-gate 	/* Stop and free the timers */
4620Sstevel@tonic-gate 	sctp_free_faddr_timers(sctp);
4630Sstevel@tonic-gate 	if ((mp = sctp->sctp_heartbeat_mp) != NULL) {
4640Sstevel@tonic-gate 		sctp_timer_free(mp);
4650Sstevel@tonic-gate 		sctp->sctp_heartbeat_mp = NULL;
4660Sstevel@tonic-gate 	}
4670Sstevel@tonic-gate 	if ((mp = sctp->sctp_ack_mp) != NULL) {
4680Sstevel@tonic-gate 		sctp_timer_free(mp);
4690Sstevel@tonic-gate 		sctp->sctp_ack_mp = NULL;
4700Sstevel@tonic-gate 	}
4710Sstevel@tonic-gate 
4720Sstevel@tonic-gate 	/* Set the CONN_CLOSING flag so that IP will not cache IRE again. */
4730Sstevel@tonic-gate 	mutex_enter(&connp->conn_lock);
4740Sstevel@tonic-gate 	connp->conn_state_flags |= CONN_CLOSING;
4750Sstevel@tonic-gate 	mutex_exit(&connp->conn_lock);
4760Sstevel@tonic-gate 
4770Sstevel@tonic-gate 	/* Remove from all hashes. */
4780Sstevel@tonic-gate 	sctp_bind_hash_remove(sctp);
4790Sstevel@tonic-gate 	sctp_conn_hash_remove(sctp);
4800Sstevel@tonic-gate 	sctp_listen_hash_remove(sctp);
4810Sstevel@tonic-gate 	sctp->sctp_state = SCTPS_IDLE;
4820Sstevel@tonic-gate 
4830Sstevel@tonic-gate 	/*
4840Sstevel@tonic-gate 	 * Clean up the recvq as much as possible.  All those packets
4850Sstevel@tonic-gate 	 * will be silently dropped as this sctp_t is now in idle state.
4860Sstevel@tonic-gate 	 */
4870Sstevel@tonic-gate 	mutex_enter(&sctp->sctp_recvq_lock);
4880Sstevel@tonic-gate 	while ((mp = sctp->sctp_recvq) != NULL) {
4890Sstevel@tonic-gate 		sctp->sctp_recvq = mp->b_next;
4900Sstevel@tonic-gate 		mp->b_next = NULL;
491*11042SErik.Nordmark@Sun.COM 
492*11042SErik.Nordmark@Sun.COM 		if (ip_recv_attr_is_mblk(mp))
493*11042SErik.Nordmark@Sun.COM 			mp = ip_recv_attr_free_mblk(mp);
494*11042SErik.Nordmark@Sun.COM 
4950Sstevel@tonic-gate 		freemsg(mp);
4960Sstevel@tonic-gate 	}
4970Sstevel@tonic-gate 	mutex_exit(&sctp->sctp_recvq_lock);
4980Sstevel@tonic-gate }
4990Sstevel@tonic-gate 
5000Sstevel@tonic-gate /*
5010Sstevel@tonic-gate  * Free memory associated with the sctp/ip header template.
5020Sstevel@tonic-gate  */
5030Sstevel@tonic-gate static void
5040Sstevel@tonic-gate sctp_headers_free(sctp_t *sctp)
5050Sstevel@tonic-gate {
5060Sstevel@tonic-gate 	if (sctp->sctp_iphc != NULL) {
5070Sstevel@tonic-gate 		kmem_free(sctp->sctp_iphc, sctp->sctp_iphc_len);
5080Sstevel@tonic-gate 		sctp->sctp_iphc = NULL;
5090Sstevel@tonic-gate 		sctp->sctp_ipha = NULL;
5100Sstevel@tonic-gate 		sctp->sctp_hdr_len = 0;
5110Sstevel@tonic-gate 		sctp->sctp_ip_hdr_len = 0;
5120Sstevel@tonic-gate 		sctp->sctp_iphc_len = 0;
5130Sstevel@tonic-gate 		sctp->sctp_sctph = NULL;
5140Sstevel@tonic-gate 		sctp->sctp_hdr_len = 0;
5150Sstevel@tonic-gate 	}
5160Sstevel@tonic-gate 	if (sctp->sctp_iphc6 != NULL) {
5170Sstevel@tonic-gate 		kmem_free(sctp->sctp_iphc6, sctp->sctp_iphc6_len);
5180Sstevel@tonic-gate 		sctp->sctp_iphc6 = NULL;
5190Sstevel@tonic-gate 		sctp->sctp_ip6h = NULL;
5200Sstevel@tonic-gate 		sctp->sctp_hdr6_len = 0;
5210Sstevel@tonic-gate 		sctp->sctp_ip_hdr6_len = 0;
5220Sstevel@tonic-gate 		sctp->sctp_iphc6_len = 0;
5230Sstevel@tonic-gate 		sctp->sctp_sctph6 = NULL;
5240Sstevel@tonic-gate 		sctp->sctp_hdr6_len = 0;
5250Sstevel@tonic-gate 	}
5260Sstevel@tonic-gate }
5270Sstevel@tonic-gate 
5280Sstevel@tonic-gate static void
5290Sstevel@tonic-gate sctp_free_xmit_data(sctp_t *sctp)
5300Sstevel@tonic-gate {
5310Sstevel@tonic-gate 	mblk_t	*ump = NULL;
5320Sstevel@tonic-gate 	mblk_t	*nump;
5330Sstevel@tonic-gate 	mblk_t	*mp;
5340Sstevel@tonic-gate 	mblk_t	*nmp;
5350Sstevel@tonic-gate 
5360Sstevel@tonic-gate 	sctp->sctp_xmit_unacked = NULL;
5370Sstevel@tonic-gate 	ump = sctp->sctp_xmit_head;
5380Sstevel@tonic-gate 	sctp->sctp_xmit_tail = sctp->sctp_xmit_head = NULL;
5390Sstevel@tonic-gate free_unsent:
5400Sstevel@tonic-gate 	for (; ump != NULL; ump = nump) {
5410Sstevel@tonic-gate 		for (mp = ump->b_cont; mp != NULL; mp = nmp) {
5420Sstevel@tonic-gate 			nmp = mp->b_next;
5430Sstevel@tonic-gate 			mp->b_next = NULL;
5440Sstevel@tonic-gate 			mp->b_prev = NULL;
5450Sstevel@tonic-gate 			freemsg(mp);
5460Sstevel@tonic-gate 		}
5470Sstevel@tonic-gate 		ASSERT(DB_REF(ump) == 1);
5480Sstevel@tonic-gate 		nump = ump->b_next;
5490Sstevel@tonic-gate 		ump->b_next = NULL;
5500Sstevel@tonic-gate 		ump->b_prev = NULL;
5510Sstevel@tonic-gate 		ump->b_cont = NULL;
5520Sstevel@tonic-gate 		freeb(ump);
5530Sstevel@tonic-gate 	}
5540Sstevel@tonic-gate 	if ((ump = sctp->sctp_xmit_unsent) == NULL) {
5550Sstevel@tonic-gate 		ASSERT(sctp->sctp_xmit_unsent_tail == NULL);
5560Sstevel@tonic-gate 		return;
5570Sstevel@tonic-gate 	}
5580Sstevel@tonic-gate 	sctp->sctp_xmit_unsent = sctp->sctp_xmit_unsent_tail = NULL;
5590Sstevel@tonic-gate 	goto free_unsent;
5600Sstevel@tonic-gate }
5610Sstevel@tonic-gate 
5620Sstevel@tonic-gate /*
5630Sstevel@tonic-gate  * Cleanup all the messages in the stream queue and the reassembly lists.
5640Sstevel@tonic-gate  * If 'free' is true, then delete the streams as well.
5650Sstevel@tonic-gate  */
5660Sstevel@tonic-gate void
5670Sstevel@tonic-gate sctp_instream_cleanup(sctp_t *sctp, boolean_t free)
5680Sstevel@tonic-gate {
5690Sstevel@tonic-gate 	int	i;
5700Sstevel@tonic-gate 	mblk_t	*mp;
5710Sstevel@tonic-gate 	mblk_t	*mp1;
5720Sstevel@tonic-gate 
5730Sstevel@tonic-gate 	if (sctp->sctp_instr != NULL) {
5740Sstevel@tonic-gate 		/* walk thru and flush out anything remaining in the Q */
5750Sstevel@tonic-gate 		for (i = 0; i < sctp->sctp_num_istr; i++) {
5760Sstevel@tonic-gate 			mp = sctp->sctp_instr[i].istr_msgs;
5770Sstevel@tonic-gate 			while (mp != NULL) {
5780Sstevel@tonic-gate 				mp1 = mp->b_next;
5790Sstevel@tonic-gate 				mp->b_next = mp->b_prev = NULL;
5800Sstevel@tonic-gate 				freemsg(mp);
5810Sstevel@tonic-gate 				mp = mp1;
5820Sstevel@tonic-gate 			}
5830Sstevel@tonic-gate 			sctp->sctp_instr[i].istr_msgs = NULL;
5844116Snh145002 			sctp->sctp_instr[i].istr_nmsgs = 0;
5850Sstevel@tonic-gate 			sctp_free_reass((sctp->sctp_instr) + i);
5860Sstevel@tonic-gate 			sctp->sctp_instr[i].nextseq = 0;
5870Sstevel@tonic-gate 		}
5880Sstevel@tonic-gate 		if (free) {
5890Sstevel@tonic-gate 			kmem_free(sctp->sctp_instr,
5900Sstevel@tonic-gate 			    sizeof (*sctp->sctp_instr) * sctp->sctp_num_istr);
5910Sstevel@tonic-gate 			sctp->sctp_instr = NULL;
5920Sstevel@tonic-gate 			sctp->sctp_num_istr = 0;
5930Sstevel@tonic-gate 		}
5940Sstevel@tonic-gate 	}
5950Sstevel@tonic-gate 	/* un-ordered fragments */
5960Sstevel@tonic-gate 	if (sctp->sctp_uo_frags != NULL) {
5970Sstevel@tonic-gate 		for (mp = sctp->sctp_uo_frags; mp != NULL; mp = mp1) {
5980Sstevel@tonic-gate 			mp1 = mp->b_next;
5990Sstevel@tonic-gate 			mp->b_next = mp->b_prev = NULL;
6000Sstevel@tonic-gate 			freemsg(mp);
6010Sstevel@tonic-gate 		}
6020Sstevel@tonic-gate 	}
6030Sstevel@tonic-gate }
6040Sstevel@tonic-gate 
6050Sstevel@tonic-gate /*
6060Sstevel@tonic-gate  * Last reference to the sctp_t is gone. Free all memory associated with it.
6070Sstevel@tonic-gate  * Called from SCTP_REFRELE. Called inline in sctp_close()
6080Sstevel@tonic-gate  */
6090Sstevel@tonic-gate void
6100Sstevel@tonic-gate sctp_free(conn_t *connp)
6110Sstevel@tonic-gate {
6120Sstevel@tonic-gate 	sctp_t *sctp = CONN2SCTP(connp);
6130Sstevel@tonic-gate 	int		cnt;
6143448Sdh155122 	sctp_stack_t	*sctps = sctp->sctp_sctps;
6150Sstevel@tonic-gate 
6163448Sdh155122 	ASSERT(sctps != NULL);
6170Sstevel@tonic-gate 	/* Unlink it from the global list */
6183448Sdh155122 	SCTP_UNLINK(sctp, sctps);
6190Sstevel@tonic-gate 
6200Sstevel@tonic-gate 	ASSERT(connp->conn_ref == 0);
621*11042SErik.Nordmark@Sun.COM 	ASSERT(connp->conn_proto == IPPROTO_SCTP);
6220Sstevel@tonic-gate 	ASSERT(!MUTEX_HELD(&sctp->sctp_reflock));
6230Sstevel@tonic-gate 	ASSERT(sctp->sctp_refcnt == 0);
6240Sstevel@tonic-gate 
6250Sstevel@tonic-gate 	ASSERT(sctp->sctp_ptpbhn == NULL && sctp->sctp_bind_hash == NULL);
6260Sstevel@tonic-gate 	ASSERT(sctp->sctp_conn_hash_next == NULL &&
6270Sstevel@tonic-gate 	    sctp->sctp_conn_hash_prev == NULL);
6280Sstevel@tonic-gate 
6290Sstevel@tonic-gate 
6300Sstevel@tonic-gate 	/* Free up all the resources. */
6310Sstevel@tonic-gate 
6320Sstevel@tonic-gate 	/* blow away sctp stream management */
6330Sstevel@tonic-gate 	if (sctp->sctp_ostrcntrs != NULL) {
6340Sstevel@tonic-gate 		kmem_free(sctp->sctp_ostrcntrs,
6350Sstevel@tonic-gate 		    sizeof (uint16_t) * sctp->sctp_num_ostr);
6360Sstevel@tonic-gate 		sctp->sctp_ostrcntrs = NULL;
6370Sstevel@tonic-gate 	}
6380Sstevel@tonic-gate 	sctp_instream_cleanup(sctp, B_TRUE);
6390Sstevel@tonic-gate 
6400Sstevel@tonic-gate 	/* Remove all data transfer resources. */
6410Sstevel@tonic-gate 	sctp->sctp_istr_nmsgs = 0;
6420Sstevel@tonic-gate 	sctp->sctp_rxqueued = 0;
6430Sstevel@tonic-gate 	sctp_free_xmit_data(sctp);
6440Sstevel@tonic-gate 	sctp->sctp_unacked = 0;
6450Sstevel@tonic-gate 	sctp->sctp_unsent = 0;
646852Svi117747 	if (sctp->sctp_cxmit_list != NULL)
647852Svi117747 		sctp_asconf_free_cxmit(sctp, NULL);
648852Svi117747 
6490Sstevel@tonic-gate 	sctp->sctp_lastdata = NULL;
6500Sstevel@tonic-gate 
6510Sstevel@tonic-gate 	/* Clear out default xmit settings */
6520Sstevel@tonic-gate 	sctp->sctp_def_stream = 0;
6530Sstevel@tonic-gate 	sctp->sctp_def_flags = 0;
6540Sstevel@tonic-gate 	sctp->sctp_def_ppid = 0;
6550Sstevel@tonic-gate 	sctp->sctp_def_context = 0;
6560Sstevel@tonic-gate 	sctp->sctp_def_timetolive = 0;
6570Sstevel@tonic-gate 
6580Sstevel@tonic-gate 	if (sctp->sctp_sack_info != NULL) {
6590Sstevel@tonic-gate 		sctp_free_set(sctp->sctp_sack_info);
6600Sstevel@tonic-gate 		sctp->sctp_sack_info = NULL;
6610Sstevel@tonic-gate 	}
6620Sstevel@tonic-gate 	sctp->sctp_sack_gaps = 0;
6630Sstevel@tonic-gate 
6640Sstevel@tonic-gate 	if (sctp->sctp_cookie_mp != NULL) {
6650Sstevel@tonic-gate 		freemsg(sctp->sctp_cookie_mp);
6660Sstevel@tonic-gate 		sctp->sctp_cookie_mp = NULL;
6670Sstevel@tonic-gate 	}
6680Sstevel@tonic-gate 
6690Sstevel@tonic-gate 	/* Remove all the address resources. */
6700Sstevel@tonic-gate 	sctp_zap_addrs(sctp);
6710Sstevel@tonic-gate 	for (cnt = 0; cnt < SCTP_IPIF_HASH; cnt++) {
6720Sstevel@tonic-gate 		ASSERT(sctp->sctp_saddrs[cnt].ipif_count == 0);
6730Sstevel@tonic-gate 		list_destroy(&sctp->sctp_saddrs[cnt].sctp_ipif_list);
6740Sstevel@tonic-gate 	}
6750Sstevel@tonic-gate 
6760Sstevel@tonic-gate 	if (sctp->sctp_hopopts != NULL) {
6770Sstevel@tonic-gate 		mi_free(sctp->sctp_hopopts);
6780Sstevel@tonic-gate 		sctp->sctp_hopopts = NULL;
6790Sstevel@tonic-gate 		sctp->sctp_hopoptslen = 0;
6800Sstevel@tonic-gate 	}
6810Sstevel@tonic-gate 	ASSERT(sctp->sctp_hopoptslen == 0);
6820Sstevel@tonic-gate 	if (sctp->sctp_dstopts != NULL) {
6830Sstevel@tonic-gate 		mi_free(sctp->sctp_dstopts);
6840Sstevel@tonic-gate 		sctp->sctp_dstopts = NULL;
6850Sstevel@tonic-gate 		sctp->sctp_dstoptslen = 0;
6860Sstevel@tonic-gate 	}
6870Sstevel@tonic-gate 	ASSERT(sctp->sctp_dstoptslen == 0);
688*11042SErik.Nordmark@Sun.COM 	if (sctp->sctp_rthdrdstopts != NULL) {
689*11042SErik.Nordmark@Sun.COM 		mi_free(sctp->sctp_rthdrdstopts);
690*11042SErik.Nordmark@Sun.COM 		sctp->sctp_rthdrdstopts = NULL;
691*11042SErik.Nordmark@Sun.COM 		sctp->sctp_rthdrdstoptslen = 0;
6920Sstevel@tonic-gate 	}
693*11042SErik.Nordmark@Sun.COM 	ASSERT(sctp->sctp_rthdrdstoptslen == 0);
6940Sstevel@tonic-gate 	if (sctp->sctp_rthdr != NULL) {
6950Sstevel@tonic-gate 		mi_free(sctp->sctp_rthdr);
6960Sstevel@tonic-gate 		sctp->sctp_rthdr = NULL;
6970Sstevel@tonic-gate 		sctp->sctp_rthdrlen = 0;
6980Sstevel@tonic-gate 	}
6990Sstevel@tonic-gate 	ASSERT(sctp->sctp_rthdrlen == 0);
7000Sstevel@tonic-gate 	sctp_headers_free(sctp);
7010Sstevel@tonic-gate 
7020Sstevel@tonic-gate 	sctp->sctp_shutdown_faddr = NULL;
7030Sstevel@tonic-gate 
7044964Skcpoon 	if (sctp->sctp_err_chunks != NULL) {
7054964Skcpoon 		freemsg(sctp->sctp_err_chunks);
7064964Skcpoon 		sctp->sctp_err_chunks = NULL;
7074964Skcpoon 		sctp->sctp_err_len = 0;
7084964Skcpoon 	}
7094964Skcpoon 
7100Sstevel@tonic-gate 	/* Clear all the bitfields. */
7110Sstevel@tonic-gate 	bzero(&sctp->sctp_bits, sizeof (sctp->sctp_bits));
7120Sstevel@tonic-gate 
7130Sstevel@tonic-gate 	/* It is time to update the global statistics. */
7143448Sdh155122 	UPDATE_MIB(&sctps->sctps_mib, sctpOutSCTPPkts, sctp->sctp_opkts);
7153448Sdh155122 	UPDATE_MIB(&sctps->sctps_mib, sctpOutCtrlChunks, sctp->sctp_obchunks);
7163448Sdh155122 	UPDATE_MIB(&sctps->sctps_mib, sctpOutOrderChunks, sctp->sctp_odchunks);
7173448Sdh155122 	UPDATE_MIB(&sctps->sctps_mib,
7183448Sdh155122 	    sctpOutUnorderChunks, sctp->sctp_oudchunks);
7193448Sdh155122 	UPDATE_MIB(&sctps->sctps_mib, sctpRetransChunks, sctp->sctp_rxtchunks);
7203448Sdh155122 	UPDATE_MIB(&sctps->sctps_mib, sctpInSCTPPkts, sctp->sctp_ipkts);
7213448Sdh155122 	UPDATE_MIB(&sctps->sctps_mib, sctpInCtrlChunks, sctp->sctp_ibchunks);
7223448Sdh155122 	UPDATE_MIB(&sctps->sctps_mib, sctpInOrderChunks, sctp->sctp_idchunks);
7233448Sdh155122 	UPDATE_MIB(&sctps->sctps_mib,
7243448Sdh155122 	    sctpInUnorderChunks, sctp->sctp_iudchunks);
7253448Sdh155122 	UPDATE_MIB(&sctps->sctps_mib, sctpFragUsrMsgs, sctp->sctp_fragdmsgs);
7263448Sdh155122 	UPDATE_MIB(&sctps->sctps_mib, sctpReasmUsrMsgs, sctp->sctp_reassmsgs);
7270Sstevel@tonic-gate 	sctp->sctp_opkts = 0;
7280Sstevel@tonic-gate 	sctp->sctp_obchunks = 0;
7290Sstevel@tonic-gate 	sctp->sctp_odchunks = 0;
7300Sstevel@tonic-gate 	sctp->sctp_oudchunks = 0;
7310Sstevel@tonic-gate 	sctp->sctp_rxtchunks = 0;
7320Sstevel@tonic-gate 	sctp->sctp_ipkts = 0;
7330Sstevel@tonic-gate 	sctp->sctp_ibchunks = 0;
7340Sstevel@tonic-gate 	sctp->sctp_idchunks = 0;
7350Sstevel@tonic-gate 	sctp->sctp_iudchunks = 0;
7360Sstevel@tonic-gate 	sctp->sctp_fragdmsgs = 0;
7370Sstevel@tonic-gate 	sctp->sctp_reassmsgs = 0;
73810751SGeorge.Shepherd@Sun.COM 	sctp->sctp_outseqtsns = 0;
73910751SGeorge.Shepherd@Sun.COM 	sctp->sctp_osacks = 0;
74010751SGeorge.Shepherd@Sun.COM 	sctp->sctp_isacks = 0;
74110751SGeorge.Shepherd@Sun.COM 	sctp->sctp_idupchunks = 0;
74210751SGeorge.Shepherd@Sun.COM 	sctp->sctp_gapcnt = 0;
74310751SGeorge.Shepherd@Sun.COM 	sctp->sctp_cum_obchunks = 0;
74410751SGeorge.Shepherd@Sun.COM 	sctp->sctp_cum_odchunks = 0;
74510751SGeorge.Shepherd@Sun.COM 	sctp->sctp_cum_oudchunks = 0;
74610751SGeorge.Shepherd@Sun.COM 	sctp->sctp_cum_rxtchunks = 0;
74710751SGeorge.Shepherd@Sun.COM 	sctp->sctp_cum_ibchunks = 0;
74810751SGeorge.Shepherd@Sun.COM 	sctp->sctp_cum_idchunks = 0;
74910751SGeorge.Shepherd@Sun.COM 	sctp->sctp_cum_iudchunks = 0;
7500Sstevel@tonic-gate 
7510Sstevel@tonic-gate 	sctp->sctp_autoclose = 0;
7525586Skcpoon 	sctp->sctp_tx_adaptation_code = 0;
7530Sstevel@tonic-gate 
7542283Skp158701 	sctp->sctp_v6label_len = 0;
7552283Skp158701 	sctp->sctp_v4label_len = 0;
7562283Skp158701 
7573448Sdh155122 	sctp->sctp_sctps = NULL;
7583448Sdh155122 
7594691Skcpoon 	sctp_conn_clear(connp);
7600Sstevel@tonic-gate 	kmem_cache_free(sctp_conn_cache, connp);
7610Sstevel@tonic-gate }
7620Sstevel@tonic-gate 
7630Sstevel@tonic-gate /* Diagnostic routine used to return a string associated with the sctp state. */
7640Sstevel@tonic-gate char *
7650Sstevel@tonic-gate sctp_display(sctp_t *sctp, char *sup_buf)
7660Sstevel@tonic-gate {
7670Sstevel@tonic-gate 	char	*buf;
7680Sstevel@tonic-gate 	char	buf1[30];
7690Sstevel@tonic-gate 	static char	priv_buf[INET6_ADDRSTRLEN * 2 + 80];
7700Sstevel@tonic-gate 	char	*cp;
771*11042SErik.Nordmark@Sun.COM 	conn_t	*connp;
7720Sstevel@tonic-gate 
7730Sstevel@tonic-gate 	if (sctp == NULL)
7740Sstevel@tonic-gate 		return ("NULL_SCTP");
7750Sstevel@tonic-gate 
776*11042SErik.Nordmark@Sun.COM 	connp = sctp->sctp_connp;
7770Sstevel@tonic-gate 	buf = (sup_buf != NULL) ? sup_buf : priv_buf;
7780Sstevel@tonic-gate 
7790Sstevel@tonic-gate 	switch (sctp->sctp_state) {
7800Sstevel@tonic-gate 	case SCTPS_IDLE:
7810Sstevel@tonic-gate 		cp = "SCTP_IDLE";
7820Sstevel@tonic-gate 		break;
7830Sstevel@tonic-gate 	case SCTPS_BOUND:
7840Sstevel@tonic-gate 		cp = "SCTP_BOUND";
7850Sstevel@tonic-gate 		break;
7860Sstevel@tonic-gate 	case SCTPS_LISTEN:
7870Sstevel@tonic-gate 		cp = "SCTP_LISTEN";
7880Sstevel@tonic-gate 		break;
7890Sstevel@tonic-gate 	case SCTPS_COOKIE_WAIT:
7900Sstevel@tonic-gate 		cp = "SCTP_COOKIE_WAIT";
7910Sstevel@tonic-gate 		break;
7920Sstevel@tonic-gate 	case SCTPS_COOKIE_ECHOED:
7930Sstevel@tonic-gate 		cp = "SCTP_COOKIE_ECHOED";
7940Sstevel@tonic-gate 		break;
7950Sstevel@tonic-gate 	case SCTPS_ESTABLISHED:
7960Sstevel@tonic-gate 		cp = "SCTP_ESTABLISHED";
7970Sstevel@tonic-gate 		break;
7980Sstevel@tonic-gate 	case SCTPS_SHUTDOWN_PENDING:
7990Sstevel@tonic-gate 		cp = "SCTP_SHUTDOWN_PENDING";
8000Sstevel@tonic-gate 		break;
8010Sstevel@tonic-gate 	case SCTPS_SHUTDOWN_SENT:
8020Sstevel@tonic-gate 		cp = "SCTPS_SHUTDOWN_SENT";
8030Sstevel@tonic-gate 		break;
8040Sstevel@tonic-gate 	case SCTPS_SHUTDOWN_RECEIVED:
8050Sstevel@tonic-gate 		cp = "SCTPS_SHUTDOWN_RECEIVED";
8060Sstevel@tonic-gate 		break;
8070Sstevel@tonic-gate 	case SCTPS_SHUTDOWN_ACK_SENT:
8080Sstevel@tonic-gate 		cp = "SCTPS_SHUTDOWN_ACK_SENT";
8090Sstevel@tonic-gate 		break;
8100Sstevel@tonic-gate 	default:
8110Sstevel@tonic-gate 		(void) mi_sprintf(buf1, "SCTPUnkState(%d)", sctp->sctp_state);
8120Sstevel@tonic-gate 		cp = buf1;
8130Sstevel@tonic-gate 		break;
8140Sstevel@tonic-gate 	}
8150Sstevel@tonic-gate 	(void) mi_sprintf(buf, "[%u, %u] %s",
816*11042SErik.Nordmark@Sun.COM 	    ntohs(connp->conn_lport), ntohs(connp->conn_fport), cp);
8170Sstevel@tonic-gate 
8180Sstevel@tonic-gate 	return (buf);
8190Sstevel@tonic-gate }
8200Sstevel@tonic-gate 
8210Sstevel@tonic-gate /*
8220Sstevel@tonic-gate  * Initialize protocol control block. If a parent exists, inherit
8230Sstevel@tonic-gate  * all values set through setsockopt().
8240Sstevel@tonic-gate  */
8250Sstevel@tonic-gate static int
8260Sstevel@tonic-gate sctp_init_values(sctp_t *sctp, sctp_t *psctp, int sleep)
8270Sstevel@tonic-gate {
8280Sstevel@tonic-gate 	int	err;
8290Sstevel@tonic-gate 	int	cnt;
8303448Sdh155122 	sctp_stack_t	*sctps = sctp->sctp_sctps;
831*11042SErik.Nordmark@Sun.COM 	conn_t 	*connp;
8320Sstevel@tonic-gate 
833*11042SErik.Nordmark@Sun.COM 	connp = sctp->sctp_connp;
8340Sstevel@tonic-gate 
8350Sstevel@tonic-gate 	sctp->sctp_nsaddrs = 0;
8360Sstevel@tonic-gate 	for (cnt = 0; cnt < SCTP_IPIF_HASH; cnt++) {
8370Sstevel@tonic-gate 		sctp->sctp_saddrs[cnt].ipif_count = 0;
8380Sstevel@tonic-gate 		list_create(&sctp->sctp_saddrs[cnt].sctp_ipif_list,
8390Sstevel@tonic-gate 		    sizeof (sctp_saddr_ipif_t), offsetof(sctp_saddr_ipif_t,
8400Sstevel@tonic-gate 		    saddr_ipif));
8410Sstevel@tonic-gate 	}
842*11042SErik.Nordmark@Sun.COM 	connp->conn_ports = 0;
8430Sstevel@tonic-gate 	sctp->sctp_running = B_FALSE;
8440Sstevel@tonic-gate 	sctp->sctp_state = SCTPS_IDLE;
8450Sstevel@tonic-gate 
8460Sstevel@tonic-gate 	sctp->sctp_refcnt = 1;
8470Sstevel@tonic-gate 
8480Sstevel@tonic-gate 	sctp->sctp_strikes = 0;
8490Sstevel@tonic-gate 
8500Sstevel@tonic-gate 	sctp->sctp_last_mtu_probe = lbolt64;
8513448Sdh155122 	sctp->sctp_mtu_probe_intvl = sctps->sctps_mtu_probe_interval;
8520Sstevel@tonic-gate 
8530Sstevel@tonic-gate 	sctp->sctp_sack_gaps = 0;
8540Sstevel@tonic-gate 	sctp->sctp_sack_toggle = 2;
8550Sstevel@tonic-gate 
8564691Skcpoon 	/* Only need to do the allocation if there is no "cached" one. */
8574691Skcpoon 	if (sctp->sctp_pad_mp == NULL) {
8584691Skcpoon 		if (sleep == KM_SLEEP) {
8594691Skcpoon 			sctp->sctp_pad_mp = allocb_wait(SCTP_ALIGN, BPRI_MED,
8604691Skcpoon 			    STR_NOSIG, NULL);
8614691Skcpoon 		} else {
8624691Skcpoon 			sctp->sctp_pad_mp = allocb(SCTP_ALIGN, BPRI_MED);
8634691Skcpoon 			if (sctp->sctp_pad_mp == NULL)
8644691Skcpoon 				return (ENOMEM);
8654691Skcpoon 		}
8664691Skcpoon 		bzero(sctp->sctp_pad_mp->b_rptr, SCTP_ALIGN);
8674691Skcpoon 	}
8684691Skcpoon 
8690Sstevel@tonic-gate 	if (psctp != NULL) {
8700Sstevel@tonic-gate 		/*
8710Sstevel@tonic-gate 		 * Inherit from parent
872*11042SErik.Nordmark@Sun.COM 		 *
873*11042SErik.Nordmark@Sun.COM 		 * Start by inheriting from the conn_t, including conn_ixa and
874*11042SErik.Nordmark@Sun.COM 		 * conn_xmit_ipp.
8750Sstevel@tonic-gate 		 */
876*11042SErik.Nordmark@Sun.COM 		err = conn_inherit_parent(psctp->sctp_connp, connp);
877*11042SErik.Nordmark@Sun.COM 		if (err != 0)
8784691Skcpoon 			goto failure;
8790Sstevel@tonic-gate 
8800Sstevel@tonic-gate 		sctp->sctp_cookie_lifetime = psctp->sctp_cookie_lifetime;
881*11042SErik.Nordmark@Sun.COM 
8820Sstevel@tonic-gate 		sctp->sctp_cwnd_max = psctp->sctp_cwnd_max;
8830Sstevel@tonic-gate 		sctp->sctp_rwnd = psctp->sctp_rwnd;
884852Svi117747 		sctp->sctp_irwnd = psctp->sctp_rwnd;
8853845Svi117747 		sctp->sctp_pd_point = psctp->sctp_pd_point;
8860Sstevel@tonic-gate 		sctp->sctp_rto_max = psctp->sctp_rto_max;
8870Sstevel@tonic-gate 		sctp->sctp_init_rto_max = psctp->sctp_init_rto_max;
8880Sstevel@tonic-gate 		sctp->sctp_rto_min = psctp->sctp_rto_min;
8890Sstevel@tonic-gate 		sctp->sctp_rto_initial = psctp->sctp_rto_initial;
8900Sstevel@tonic-gate 		sctp->sctp_pa_max_rxt = psctp->sctp_pa_max_rxt;
8910Sstevel@tonic-gate 		sctp->sctp_pp_max_rxt = psctp->sctp_pp_max_rxt;
8920Sstevel@tonic-gate 		sctp->sctp_max_init_rxt = psctp->sctp_max_init_rxt;
8930Sstevel@tonic-gate 
8940Sstevel@tonic-gate 		sctp->sctp_def_stream = psctp->sctp_def_stream;
8950Sstevel@tonic-gate 		sctp->sctp_def_flags = psctp->sctp_def_flags;
8960Sstevel@tonic-gate 		sctp->sctp_def_ppid = psctp->sctp_def_ppid;
8970Sstevel@tonic-gate 		sctp->sctp_def_context = psctp->sctp_def_context;
8980Sstevel@tonic-gate 		sctp->sctp_def_timetolive = psctp->sctp_def_timetolive;
8990Sstevel@tonic-gate 
9000Sstevel@tonic-gate 		sctp->sctp_num_istr = psctp->sctp_num_istr;
9010Sstevel@tonic-gate 		sctp->sctp_num_ostr = psctp->sctp_num_ostr;
9020Sstevel@tonic-gate 
9030Sstevel@tonic-gate 		sctp->sctp_hb_interval = psctp->sctp_hb_interval;
9040Sstevel@tonic-gate 		sctp->sctp_autoclose = psctp->sctp_autoclose;
9055586Skcpoon 		sctp->sctp_tx_adaptation_code = psctp->sctp_tx_adaptation_code;
9060Sstevel@tonic-gate 
9070Sstevel@tonic-gate 		/* xxx should be a better way to copy these flags xxx */
9080Sstevel@tonic-gate 		sctp->sctp_bound_to_all = psctp->sctp_bound_to_all;
9090Sstevel@tonic-gate 		sctp->sctp_cansleep = psctp->sctp_cansleep;
9105586Skcpoon 		sctp->sctp_send_adaptation = psctp->sctp_send_adaptation;
9110Sstevel@tonic-gate 		sctp->sctp_ndelay = psctp->sctp_ndelay;
9120Sstevel@tonic-gate 		sctp->sctp_events = psctp->sctp_events;
9130Sstevel@tonic-gate 	} else {
9140Sstevel@tonic-gate 		/*
9150Sstevel@tonic-gate 		 * Set to system defaults
9160Sstevel@tonic-gate 		 */
9173448Sdh155122 		sctp->sctp_cookie_lifetime =
9183448Sdh155122 		    MSEC_TO_TICK(sctps->sctps_cookie_life);
919*11042SErik.Nordmark@Sun.COM 		connp->conn_sndlowat = sctps->sctps_xmit_lowat;
920*11042SErik.Nordmark@Sun.COM 		connp->conn_sndbuf = sctps->sctps_xmit_hiwat;
921*11042SErik.Nordmark@Sun.COM 		connp->conn_rcvbuf = sctps->sctps_recv_hiwat;
922*11042SErik.Nordmark@Sun.COM 
9233448Sdh155122 		sctp->sctp_cwnd_max = sctps->sctps_cwnd_max_;
924*11042SErik.Nordmark@Sun.COM 		sctp->sctp_rwnd = connp->conn_rcvbuf;
925852Svi117747 		sctp->sctp_irwnd = sctp->sctp_rwnd;
9263845Svi117747 		sctp->sctp_pd_point = sctp->sctp_rwnd;
9273448Sdh155122 		sctp->sctp_rto_max = MSEC_TO_TICK(sctps->sctps_rto_maxg);
9280Sstevel@tonic-gate 		sctp->sctp_init_rto_max = sctp->sctp_rto_max;
9293448Sdh155122 		sctp->sctp_rto_min = MSEC_TO_TICK(sctps->sctps_rto_ming);
9303448Sdh155122 		sctp->sctp_rto_initial = MSEC_TO_TICK(
9313448Sdh155122 		    sctps->sctps_rto_initialg);
9323448Sdh155122 		sctp->sctp_pa_max_rxt = sctps->sctps_pa_max_retr;
9333448Sdh155122 		sctp->sctp_pp_max_rxt = sctps->sctps_pp_max_retr;
9343448Sdh155122 		sctp->sctp_max_init_rxt = sctps->sctps_max_init_retr;
9350Sstevel@tonic-gate 
9363448Sdh155122 		sctp->sctp_num_istr = sctps->sctps_max_in_streams;
9373448Sdh155122 		sctp->sctp_num_ostr = sctps->sctps_initial_out_streams;
9380Sstevel@tonic-gate 
9393448Sdh155122 		sctp->sctp_hb_interval =
9403448Sdh155122 		    MSEC_TO_TICK(sctps->sctps_heartbeat_interval);
941*11042SErik.Nordmark@Sun.COM 
942*11042SErik.Nordmark@Sun.COM 		if (connp->conn_family == AF_INET)
943*11042SErik.Nordmark@Sun.COM 			connp->conn_default_ttl = sctps->sctps_ipv4_ttl;
944*11042SErik.Nordmark@Sun.COM 		else
945*11042SErik.Nordmark@Sun.COM 			connp->conn_default_ttl = sctps->sctps_ipv6_hoplimit;
946*11042SErik.Nordmark@Sun.COM 
947*11042SErik.Nordmark@Sun.COM 		connp->conn_xmit_ipp.ipp_unicast_hops =
948*11042SErik.Nordmark@Sun.COM 		    connp->conn_default_ttl;
949*11042SErik.Nordmark@Sun.COM 
950*11042SErik.Nordmark@Sun.COM 		/*
951*11042SErik.Nordmark@Sun.COM 		 * Initialize the header template
952*11042SErik.Nordmark@Sun.COM 		 */
953*11042SErik.Nordmark@Sun.COM 		if ((err = sctp_build_hdrs(sctp, sleep)) != 0) {
954*11042SErik.Nordmark@Sun.COM 			goto failure;
955*11042SErik.Nordmark@Sun.COM 		}
9560Sstevel@tonic-gate 	}
957*11042SErik.Nordmark@Sun.COM 
9580Sstevel@tonic-gate 	sctp->sctp_understands_asconf = B_TRUE;
9590Sstevel@tonic-gate 	sctp->sctp_understands_addip = B_TRUE;
9600Sstevel@tonic-gate 	sctp->sctp_prsctp_aware = B_FALSE;
9610Sstevel@tonic-gate 
9620Sstevel@tonic-gate 	sctp->sctp_connp->conn_ref = 1;
9630Sstevel@tonic-gate 
9640Sstevel@tonic-gate 	sctp->sctp_prsctpdrop = 0;
9650Sstevel@tonic-gate 	sctp->sctp_msgcount = 0;
9660Sstevel@tonic-gate 
9670Sstevel@tonic-gate 	return (0);
9684691Skcpoon 
9694691Skcpoon failure:
970*11042SErik.Nordmark@Sun.COM 	sctp_headers_free(sctp);
9714691Skcpoon 	return (err);
9720Sstevel@tonic-gate }
9730Sstevel@tonic-gate 
9740Sstevel@tonic-gate /*
9750Sstevel@tonic-gate  * Extracts the init tag from an INIT chunk and checks if it matches
9760Sstevel@tonic-gate  * the sctp's verification tag. Returns 0 if it doesn't match, 1 if
9770Sstevel@tonic-gate  * it does.
9780Sstevel@tonic-gate  */
9790Sstevel@tonic-gate static boolean_t
9800Sstevel@tonic-gate sctp_icmp_verf(sctp_t *sctp, sctp_hdr_t *sh, mblk_t *mp)
9810Sstevel@tonic-gate {
9820Sstevel@tonic-gate 	sctp_chunk_hdr_t *sch;
9830Sstevel@tonic-gate 	uint32_t verf, *vp;
9840Sstevel@tonic-gate 
9850Sstevel@tonic-gate 	sch = (sctp_chunk_hdr_t *)(sh + 1);
9860Sstevel@tonic-gate 	vp = (uint32_t *)(sch + 1);
9870Sstevel@tonic-gate 
9880Sstevel@tonic-gate 	/* Need at least the data chunk hdr and the first 4 bytes of INIT */
9890Sstevel@tonic-gate 	if ((unsigned char *)(vp + 1) > mp->b_wptr) {
9900Sstevel@tonic-gate 		return (B_FALSE);
9910Sstevel@tonic-gate 	}
9920Sstevel@tonic-gate 
9930Sstevel@tonic-gate 	bcopy(vp, &verf, sizeof (verf));
9940Sstevel@tonic-gate 
9950Sstevel@tonic-gate 	if (verf == sctp->sctp_lvtag) {
9960Sstevel@tonic-gate 		return (B_TRUE);
9970Sstevel@tonic-gate 	}
9980Sstevel@tonic-gate 	return (B_FALSE);
9990Sstevel@tonic-gate }
10000Sstevel@tonic-gate 
10010Sstevel@tonic-gate /*
1002*11042SErik.Nordmark@Sun.COM  * Update the SCTP state according to change of PMTU.
1003*11042SErik.Nordmark@Sun.COM  *
1004*11042SErik.Nordmark@Sun.COM  * Path MTU might have changed by either increase or decrease, so need to
1005*11042SErik.Nordmark@Sun.COM  * adjust the MSS based on the value of ixa_pmtu.
1006*11042SErik.Nordmark@Sun.COM  */
1007*11042SErik.Nordmark@Sun.COM static void
1008*11042SErik.Nordmark@Sun.COM sctp_update_pmtu(sctp_t *sctp, sctp_faddr_t *fp, boolean_t decrease_only)
1009*11042SErik.Nordmark@Sun.COM {
1010*11042SErik.Nordmark@Sun.COM 	uint32_t	pmtu;
1011*11042SErik.Nordmark@Sun.COM 	int32_t		mss;
1012*11042SErik.Nordmark@Sun.COM 	ip_xmit_attr_t	*ixa = fp->ixa;
1013*11042SErik.Nordmark@Sun.COM 
1014*11042SErik.Nordmark@Sun.COM 	if (sctp->sctp_state < SCTPS_ESTABLISHED)
1015*11042SErik.Nordmark@Sun.COM 		return;
1016*11042SErik.Nordmark@Sun.COM 
1017*11042SErik.Nordmark@Sun.COM 	/*
1018*11042SErik.Nordmark@Sun.COM 	 * Always call ip_get_pmtu() to make sure that IP has updated
1019*11042SErik.Nordmark@Sun.COM 	 * ixa_flags properly.
1020*11042SErik.Nordmark@Sun.COM 	 */
1021*11042SErik.Nordmark@Sun.COM 	pmtu = ip_get_pmtu(ixa);
1022*11042SErik.Nordmark@Sun.COM 
1023*11042SErik.Nordmark@Sun.COM 	/*
1024*11042SErik.Nordmark@Sun.COM 	 * Calculate the MSS by decreasing the PMTU by sctp_hdr_len and
1025*11042SErik.Nordmark@Sun.COM 	 * IPsec overhead if applied. Make sure to use the most recent
1026*11042SErik.Nordmark@Sun.COM 	 * IPsec information.
1027*11042SErik.Nordmark@Sun.COM 	 */
1028*11042SErik.Nordmark@Sun.COM 	mss = pmtu - conn_ipsec_length(sctp->sctp_connp);
1029*11042SErik.Nordmark@Sun.COM 	if (ixa->ixa_flags & IXAF_IS_IPV4)
1030*11042SErik.Nordmark@Sun.COM 		mss -= sctp->sctp_hdr_len;
1031*11042SErik.Nordmark@Sun.COM 	else
1032*11042SErik.Nordmark@Sun.COM 		mss -= sctp->sctp_hdr6_len;
1033*11042SErik.Nordmark@Sun.COM 
1034*11042SErik.Nordmark@Sun.COM 	/*
1035*11042SErik.Nordmark@Sun.COM 	 * Nothing to change, so just return.
1036*11042SErik.Nordmark@Sun.COM 	 */
1037*11042SErik.Nordmark@Sun.COM 	if (mss == fp->sfa_pmss)
1038*11042SErik.Nordmark@Sun.COM 		return;
1039*11042SErik.Nordmark@Sun.COM 
1040*11042SErik.Nordmark@Sun.COM 	/*
1041*11042SErik.Nordmark@Sun.COM 	 * Currently, for ICMP errors, only PMTU decrease is handled.
1042*11042SErik.Nordmark@Sun.COM 	 */
1043*11042SErik.Nordmark@Sun.COM 	if (mss > fp->sfa_pmss && decrease_only)
1044*11042SErik.Nordmark@Sun.COM 		return;
1045*11042SErik.Nordmark@Sun.COM 
1046*11042SErik.Nordmark@Sun.COM #ifdef DEBUG
1047*11042SErik.Nordmark@Sun.COM 	(void) printf("sctp_update_pmtu mss from %d to %d\n",
1048*11042SErik.Nordmark@Sun.COM 	    fp->sfa_pmss, mss);
1049*11042SErik.Nordmark@Sun.COM #endif
1050*11042SErik.Nordmark@Sun.COM 	DTRACE_PROBE2(sctp_update_pmtu, int32_t, fp->sfa_pmss, uint32_t, mss);
1051*11042SErik.Nordmark@Sun.COM 
1052*11042SErik.Nordmark@Sun.COM 	/*
1053*11042SErik.Nordmark@Sun.COM 	 * Update ixa_fragsize and ixa_pmtu.
1054*11042SErik.Nordmark@Sun.COM 	 */
1055*11042SErik.Nordmark@Sun.COM 	ixa->ixa_fragsize = ixa->ixa_pmtu = pmtu;
1056*11042SErik.Nordmark@Sun.COM 
1057*11042SErik.Nordmark@Sun.COM 	/*
1058*11042SErik.Nordmark@Sun.COM 	 * Make sure that sfa_pmss is a multiple of
1059*11042SErik.Nordmark@Sun.COM 	 * SCTP_ALIGN.
1060*11042SErik.Nordmark@Sun.COM 	 */
1061*11042SErik.Nordmark@Sun.COM 	fp->sfa_pmss = mss & ~(SCTP_ALIGN - 1);
1062*11042SErik.Nordmark@Sun.COM 	fp->pmtu_discovered = 1;
1063*11042SErik.Nordmark@Sun.COM 
1064*11042SErik.Nordmark@Sun.COM #ifdef notyet
1065*11042SErik.Nordmark@Sun.COM 	if (mss < sctp->sctp_sctps->sctps_mss_min)
1066*11042SErik.Nordmark@Sun.COM 		ixa->ixa_flags |= IXAF_PMTU_TOO_SMALL;
1067*11042SErik.Nordmark@Sun.COM #endif
1068*11042SErik.Nordmark@Sun.COM 	if (ixa->ixa_flags & IXAF_PMTU_TOO_SMALL)
1069*11042SErik.Nordmark@Sun.COM 		ixa->ixa_flags &= ~(IXAF_DONTFRAG | IXAF_PMTU_IPV4_DF);
1070*11042SErik.Nordmark@Sun.COM 
1071*11042SErik.Nordmark@Sun.COM 	/*
1072*11042SErik.Nordmark@Sun.COM 	 * If below the min size then ip_get_pmtu cleared IXAF_PMTU_IPV4_DF.
1073*11042SErik.Nordmark@Sun.COM 	 * Make sure to clear IXAF_DONTFRAG, which is used by IP to decide
1074*11042SErik.Nordmark@Sun.COM 	 * whether to fragment the packet.
1075*11042SErik.Nordmark@Sun.COM 	 */
1076*11042SErik.Nordmark@Sun.COM 	if (ixa->ixa_flags & IXAF_IS_IPV4) {
1077*11042SErik.Nordmark@Sun.COM 		if (!(ixa->ixa_flags & IXAF_PMTU_IPV4_DF)) {
1078*11042SErik.Nordmark@Sun.COM 			fp->df = B_FALSE;
1079*11042SErik.Nordmark@Sun.COM 			if (fp == sctp->sctp_current) {
1080*11042SErik.Nordmark@Sun.COM 				sctp->sctp_ipha->
1081*11042SErik.Nordmark@Sun.COM 				    ipha_fragment_offset_and_flags = 0;
1082*11042SErik.Nordmark@Sun.COM 			}
1083*11042SErik.Nordmark@Sun.COM 		}
1084*11042SErik.Nordmark@Sun.COM 	}
1085*11042SErik.Nordmark@Sun.COM }
1086*11042SErik.Nordmark@Sun.COM 
1087*11042SErik.Nordmark@Sun.COM /*
1088*11042SErik.Nordmark@Sun.COM  * Notify function registered with ip_xmit_attr_t. It's called in the context
1089*11042SErik.Nordmark@Sun.COM  * of conn_ip_output so it's safe to update the SCTP state.
1090*11042SErik.Nordmark@Sun.COM  * Currently only used for pmtu changes.
1091*11042SErik.Nordmark@Sun.COM  */
1092*11042SErik.Nordmark@Sun.COM /* ARGSUSED1 */
1093*11042SErik.Nordmark@Sun.COM static void
1094*11042SErik.Nordmark@Sun.COM sctp_notify(void *arg, ip_xmit_attr_t *ixa, ixa_notify_type_t ntype,
1095*11042SErik.Nordmark@Sun.COM     ixa_notify_arg_t narg)
1096*11042SErik.Nordmark@Sun.COM {
1097*11042SErik.Nordmark@Sun.COM 	sctp_t		*sctp = (sctp_t *)arg;
1098*11042SErik.Nordmark@Sun.COM 	sctp_faddr_t	*fp;
1099*11042SErik.Nordmark@Sun.COM 
1100*11042SErik.Nordmark@Sun.COM 	switch (ntype) {
1101*11042SErik.Nordmark@Sun.COM 	case IXAN_PMTU:
1102*11042SErik.Nordmark@Sun.COM 		/* Find the faddr based on the ip_xmit_attr_t pointer */
1103*11042SErik.Nordmark@Sun.COM 		for (fp = sctp->sctp_faddrs; fp != NULL; fp = fp->next) {
1104*11042SErik.Nordmark@Sun.COM 			if (fp->ixa == ixa)
1105*11042SErik.Nordmark@Sun.COM 				break;
1106*11042SErik.Nordmark@Sun.COM 		}
1107*11042SErik.Nordmark@Sun.COM 		if (fp != NULL)
1108*11042SErik.Nordmark@Sun.COM 			sctp_update_pmtu(sctp, fp, B_FALSE);
1109*11042SErik.Nordmark@Sun.COM 		break;
1110*11042SErik.Nordmark@Sun.COM 	default:
1111*11042SErik.Nordmark@Sun.COM 		break;
1112*11042SErik.Nordmark@Sun.COM 	}
1113*11042SErik.Nordmark@Sun.COM }
1114*11042SErik.Nordmark@Sun.COM 
1115*11042SErik.Nordmark@Sun.COM /*
11160Sstevel@tonic-gate  * sctp_icmp_error is called by sctp_input() to process ICMP error messages
1117*11042SErik.Nordmark@Sun.COM  * passed up by IP.  We need to find a sctp_t
11180Sstevel@tonic-gate  * that corresponds to the returned datagram.  Passes the message back in on
11190Sstevel@tonic-gate  * the correct queue once it has located the connection.
11200Sstevel@tonic-gate  * Assumes that IP has pulled up everything up to and including
11210Sstevel@tonic-gate  * the ICMP header.
11220Sstevel@tonic-gate  */
11230Sstevel@tonic-gate void
11240Sstevel@tonic-gate sctp_icmp_error(sctp_t *sctp, mblk_t *mp)
11250Sstevel@tonic-gate {
11260Sstevel@tonic-gate 	icmph_t *icmph;
11270Sstevel@tonic-gate 	ipha_t	*ipha;
11280Sstevel@tonic-gate 	int	iph_hdr_length;
11290Sstevel@tonic-gate 	sctp_hdr_t *sctph;
11300Sstevel@tonic-gate 	in6_addr_t dst;
11310Sstevel@tonic-gate 	sctp_faddr_t *fp;
11323448Sdh155122 	sctp_stack_t	*sctps = sctp->sctp_sctps;
11330Sstevel@tonic-gate 
11341676Sjpk 	dprint(1, ("sctp_icmp_error: sctp=%p, mp=%p\n", (void *)sctp,
11351676Sjpk 	    (void *)mp));
11360Sstevel@tonic-gate 
11370Sstevel@tonic-gate 	ipha = (ipha_t *)mp->b_rptr;
11380Sstevel@tonic-gate 	if (IPH_HDR_VERSION(ipha) != IPV4_VERSION) {
11390Sstevel@tonic-gate 		ASSERT(IPH_HDR_VERSION(ipha) == IPV6_VERSION);
1140*11042SErik.Nordmark@Sun.COM 		sctp_icmp_error_ipv6(sctp, mp);
11410Sstevel@tonic-gate 		return;
11420Sstevel@tonic-gate 	}
11430Sstevel@tonic-gate 
114410828SGeorge.Shepherd@Sun.COM 	/* account for the ip hdr from the icmp message */
11450Sstevel@tonic-gate 	iph_hdr_length = IPH_HDR_LENGTH(ipha);
11460Sstevel@tonic-gate 	icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length];
114710828SGeorge.Shepherd@Sun.COM 	/* now the ip hdr of message resulting in this icmp */
11480Sstevel@tonic-gate 	ipha = (ipha_t *)&icmph[1];
11490Sstevel@tonic-gate 	iph_hdr_length = IPH_HDR_LENGTH(ipha);
11500Sstevel@tonic-gate 	sctph = (sctp_hdr_t *)((char *)ipha + iph_hdr_length);
115110828SGeorge.Shepherd@Sun.COM 	/* first_mp must expose the full sctp header. */
11520Sstevel@tonic-gate 	if ((uchar_t *)(sctph + 1) >= mp->b_wptr) {
11530Sstevel@tonic-gate 		/* not enough data for SCTP header */
1154*11042SErik.Nordmark@Sun.COM 		freemsg(mp);
11550Sstevel@tonic-gate 		return;
11560Sstevel@tonic-gate 	}
11570Sstevel@tonic-gate 
11580Sstevel@tonic-gate 	switch (icmph->icmph_type) {
11590Sstevel@tonic-gate 	case ICMP_DEST_UNREACHABLE:
11600Sstevel@tonic-gate 		switch (icmph->icmph_code) {
11610Sstevel@tonic-gate 		case ICMP_FRAGMENTATION_NEEDED:
11620Sstevel@tonic-gate 			/*
11630Sstevel@tonic-gate 			 * Reduce the MSS based on the new MTU.  This will
11640Sstevel@tonic-gate 			 * eliminate any fragmentation locally.
11650Sstevel@tonic-gate 			 * N.B.  There may well be some funny side-effects on
11660Sstevel@tonic-gate 			 * the local send policy and the remote receive policy.
11670Sstevel@tonic-gate 			 * Pending further research, we provide
11680Sstevel@tonic-gate 			 * sctp_ignore_path_mtu just in case this proves
11690Sstevel@tonic-gate 			 * disastrous somewhere.
11700Sstevel@tonic-gate 			 *
11710Sstevel@tonic-gate 			 * After updating the MSS, retransmit part of the
11720Sstevel@tonic-gate 			 * dropped segment using the new mss by calling
11730Sstevel@tonic-gate 			 * sctp_wput_slow().  Need to adjust all those
11740Sstevel@tonic-gate 			 * params to make sure sctp_wput_slow() work properly.
11750Sstevel@tonic-gate 			 */
11763448Sdh155122 			if (sctps->sctps_ignore_path_mtu)
11770Sstevel@tonic-gate 				break;
11780Sstevel@tonic-gate 
11790Sstevel@tonic-gate 			/* find the offending faddr */
11800Sstevel@tonic-gate 			IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, &dst);
11810Sstevel@tonic-gate 			fp = sctp_lookup_faddr(sctp, &dst);
11820Sstevel@tonic-gate 			if (fp == NULL) {
11830Sstevel@tonic-gate 				break;
11840Sstevel@tonic-gate 			}
1185*11042SErik.Nordmark@Sun.COM 			sctp_update_pmtu(sctp, fp, B_TRUE);
118610828SGeorge.Shepherd@Sun.COM 			/*
118710828SGeorge.Shepherd@Sun.COM 			 * It is possible, even likely that a fast retransmit
118810828SGeorge.Shepherd@Sun.COM 			 * attempt has been dropped by ip as a result of this
118910828SGeorge.Shepherd@Sun.COM 			 * error, retransmission bundles as much as possible.
119010828SGeorge.Shepherd@Sun.COM 			 * A retransmit here prevents significant delays waiting
119110828SGeorge.Shepherd@Sun.COM 			 * on the timer. Analogous to behaviour of TCP after
119210828SGeorge.Shepherd@Sun.COM 			 * ICMP too big.
119310828SGeorge.Shepherd@Sun.COM 			 */
119410828SGeorge.Shepherd@Sun.COM 			sctp_rexmit(sctp, fp);
11950Sstevel@tonic-gate 			break;
11960Sstevel@tonic-gate 		case ICMP_PORT_UNREACHABLE:
11970Sstevel@tonic-gate 		case ICMP_PROTOCOL_UNREACHABLE:
11980Sstevel@tonic-gate 			switch (sctp->sctp_state) {
11990Sstevel@tonic-gate 			case SCTPS_COOKIE_WAIT:
12000Sstevel@tonic-gate 			case SCTPS_COOKIE_ECHOED:
12010Sstevel@tonic-gate 				/* make sure the verification tag matches */
12020Sstevel@tonic-gate 				if (!sctp_icmp_verf(sctp, sctph, mp)) {
12030Sstevel@tonic-gate 					break;
12040Sstevel@tonic-gate 				}
12053448Sdh155122 				BUMP_MIB(&sctps->sctps_mib, sctpAborted);
12063314Skcpoon 				sctp_assoc_event(sctp, SCTP_CANT_STR_ASSOC, 0,
12073314Skcpoon 				    NULL);
12080Sstevel@tonic-gate 				sctp_clean_death(sctp, ECONNREFUSED);
12090Sstevel@tonic-gate 				break;
12100Sstevel@tonic-gate 			}
12110Sstevel@tonic-gate 			break;
12120Sstevel@tonic-gate 		case ICMP_HOST_UNREACHABLE:
12130Sstevel@tonic-gate 		case ICMP_NET_UNREACHABLE:
12140Sstevel@tonic-gate 			/* Record the error in case we finally time out. */
12150Sstevel@tonic-gate 			sctp->sctp_client_errno = (icmph->icmph_code ==
12160Sstevel@tonic-gate 			    ICMP_HOST_UNREACHABLE) ? EHOSTUNREACH : ENETUNREACH;
12170Sstevel@tonic-gate 			break;
12180Sstevel@tonic-gate 		default:
12190Sstevel@tonic-gate 			break;
12200Sstevel@tonic-gate 		}
12210Sstevel@tonic-gate 		break;
12220Sstevel@tonic-gate 	case ICMP_SOURCE_QUENCH: {
12230Sstevel@tonic-gate 		/* Reduce the sending rate as if we got a retransmit timeout */
12240Sstevel@tonic-gate 		break;
12250Sstevel@tonic-gate 	}
12260Sstevel@tonic-gate 	}
1227*11042SErik.Nordmark@Sun.COM 	freemsg(mp);
12280Sstevel@tonic-gate }
12290Sstevel@tonic-gate 
12300Sstevel@tonic-gate /*
12310Sstevel@tonic-gate  * sctp_icmp_error_ipv6() is called by sctp_icmp_error() to process ICMPv6
12320Sstevel@tonic-gate  * error messages passed up by IP.
12330Sstevel@tonic-gate  * Assumes that IP has pulled up all the extension headers as well
12340Sstevel@tonic-gate  * as the ICMPv6 header.
12350Sstevel@tonic-gate  */
12360Sstevel@tonic-gate static void
12370Sstevel@tonic-gate sctp_icmp_error_ipv6(sctp_t *sctp, mblk_t *mp)
12380Sstevel@tonic-gate {
12390Sstevel@tonic-gate 	icmp6_t *icmp6;
12400Sstevel@tonic-gate 	ip6_t	*ip6h;
12410Sstevel@tonic-gate 	uint16_t	iph_hdr_length;
12420Sstevel@tonic-gate 	sctp_hdr_t *sctpha;
12430Sstevel@tonic-gate 	uint8_t	*nexthdrp;
12440Sstevel@tonic-gate 	sctp_faddr_t *fp;
12453448Sdh155122 	sctp_stack_t	*sctps = sctp->sctp_sctps;
12460Sstevel@tonic-gate 
12470Sstevel@tonic-gate 	ip6h = (ip6_t *)mp->b_rptr;
12480Sstevel@tonic-gate 	iph_hdr_length = (ip6h->ip6_nxt != IPPROTO_SCTP) ?
12490Sstevel@tonic-gate 	    ip_hdr_length_v6(mp, ip6h) : IPV6_HDR_LEN;
12500Sstevel@tonic-gate 
12510Sstevel@tonic-gate 	icmp6 = (icmp6_t *)&mp->b_rptr[iph_hdr_length];
12520Sstevel@tonic-gate 	ip6h = (ip6_t *)&icmp6[1];
12530Sstevel@tonic-gate 	if (!ip_hdr_length_nexthdr_v6(mp, ip6h, &iph_hdr_length, &nexthdrp)) {
12540Sstevel@tonic-gate 		freemsg(mp);
12550Sstevel@tonic-gate 		return;
12560Sstevel@tonic-gate 	}
12570Sstevel@tonic-gate 	ASSERT(*nexthdrp == IPPROTO_SCTP);
12580Sstevel@tonic-gate 
12590Sstevel@tonic-gate 	/* XXX need ifindex to find connection */
12600Sstevel@tonic-gate 	sctpha = (sctp_hdr_t *)((char *)ip6h + iph_hdr_length);
12610Sstevel@tonic-gate 	if ((uchar_t *)sctpha >= mp->b_wptr) {
12620Sstevel@tonic-gate 		/* not enough data for SCTP header */
12630Sstevel@tonic-gate 		freemsg(mp);
12640Sstevel@tonic-gate 		return;
12650Sstevel@tonic-gate 	}
12660Sstevel@tonic-gate 	switch (icmp6->icmp6_type) {
12670Sstevel@tonic-gate 	case ICMP6_PACKET_TOO_BIG:
12680Sstevel@tonic-gate 		/*
12690Sstevel@tonic-gate 		 * Reduce the MSS based on the new MTU.  This will
12700Sstevel@tonic-gate 		 * eliminate any fragmentation locally.
12710Sstevel@tonic-gate 		 * N.B.  There may well be some funny side-effects on
12720Sstevel@tonic-gate 		 * the local send policy and the remote receive policy.
12730Sstevel@tonic-gate 		 * Pending further research, we provide
12740Sstevel@tonic-gate 		 * sctp_ignore_path_mtu just in case this proves
12750Sstevel@tonic-gate 		 * disastrous somewhere.
12760Sstevel@tonic-gate 		 *
12770Sstevel@tonic-gate 		 * After updating the MSS, retransmit part of the
12780Sstevel@tonic-gate 		 * dropped segment using the new mss by calling
12790Sstevel@tonic-gate 		 * sctp_wput_slow().  Need to adjust all those
12800Sstevel@tonic-gate 		 * params to make sure sctp_wput_slow() work properly.
12810Sstevel@tonic-gate 		 */
12823448Sdh155122 		if (sctps->sctps_ignore_path_mtu)
12830Sstevel@tonic-gate 			break;
12840Sstevel@tonic-gate 
12850Sstevel@tonic-gate 		/* find the offending faddr */
12860Sstevel@tonic-gate 		fp = sctp_lookup_faddr(sctp, &ip6h->ip6_dst);
12870Sstevel@tonic-gate 		if (fp == NULL) {
12880Sstevel@tonic-gate 			break;
12890Sstevel@tonic-gate 		}
12900Sstevel@tonic-gate 
1291*11042SErik.Nordmark@Sun.COM 		sctp_update_pmtu(sctp, fp, B_TRUE);
1292*11042SErik.Nordmark@Sun.COM 		/*
1293*11042SErik.Nordmark@Sun.COM 		 * It is possible, even likely that a fast retransmit
1294*11042SErik.Nordmark@Sun.COM 		 * attempt has been dropped by ip as a result of this
1295*11042SErik.Nordmark@Sun.COM 		 * error, retransmission bundles as much as possible.
1296*11042SErik.Nordmark@Sun.COM 		 * A retransmit here prevents significant delays waiting
1297*11042SErik.Nordmark@Sun.COM 		 * on the timer. Analogous to behaviour of TCP after
1298*11042SErik.Nordmark@Sun.COM 		 * ICMP too big.
1299*11042SErik.Nordmark@Sun.COM 		 */
1300*11042SErik.Nordmark@Sun.COM 		sctp_rexmit(sctp, fp);
13010Sstevel@tonic-gate 		break;
13020Sstevel@tonic-gate 
13030Sstevel@tonic-gate 	case ICMP6_DST_UNREACH:
13040Sstevel@tonic-gate 		switch (icmp6->icmp6_code) {
13050Sstevel@tonic-gate 		case ICMP6_DST_UNREACH_NOPORT:
13060Sstevel@tonic-gate 			/* make sure the verification tag matches */
13070Sstevel@tonic-gate 			if (!sctp_icmp_verf(sctp, sctpha, mp)) {
13080Sstevel@tonic-gate 				break;
13090Sstevel@tonic-gate 			}
13100Sstevel@tonic-gate 			if (sctp->sctp_state == SCTPS_COOKIE_WAIT ||
13110Sstevel@tonic-gate 			    sctp->sctp_state == SCTPS_COOKIE_ECHOED) {
13123448Sdh155122 				BUMP_MIB(&sctps->sctps_mib, sctpAborted);
13133314Skcpoon 				sctp_assoc_event(sctp, SCTP_CANT_STR_ASSOC, 0,
13143314Skcpoon 				    NULL);
13150Sstevel@tonic-gate 				sctp_clean_death(sctp, ECONNREFUSED);
13160Sstevel@tonic-gate 			}
13170Sstevel@tonic-gate 			break;
13180Sstevel@tonic-gate 
13190Sstevel@tonic-gate 		case ICMP6_DST_UNREACH_ADMIN:
13200Sstevel@tonic-gate 		case ICMP6_DST_UNREACH_NOROUTE:
13210Sstevel@tonic-gate 		case ICMP6_DST_UNREACH_NOTNEIGHBOR:
13220Sstevel@tonic-gate 		case ICMP6_DST_UNREACH_ADDR:
13230Sstevel@tonic-gate 			/* Record the error in case we finally time out. */
13240Sstevel@tonic-gate 			sctp->sctp_client_errno = EHOSTUNREACH;
13250Sstevel@tonic-gate 			break;
13260Sstevel@tonic-gate 		default:
13270Sstevel@tonic-gate 			break;
13280Sstevel@tonic-gate 		}
13290Sstevel@tonic-gate 		break;
13300Sstevel@tonic-gate 
13310Sstevel@tonic-gate 	case ICMP6_PARAM_PROB:
13320Sstevel@tonic-gate 		/* If this corresponds to an ICMP_PROTOCOL_UNREACHABLE */
13330Sstevel@tonic-gate 		if (icmp6->icmp6_code == ICMP6_PARAMPROB_NEXTHEADER &&
13340Sstevel@tonic-gate 		    (uchar_t *)ip6h + icmp6->icmp6_pptr ==
13350Sstevel@tonic-gate 		    (uchar_t *)nexthdrp) {
13360Sstevel@tonic-gate 			/* make sure the verification tag matches */
13370Sstevel@tonic-gate 			if (!sctp_icmp_verf(sctp, sctpha, mp)) {
13380Sstevel@tonic-gate 				break;
13390Sstevel@tonic-gate 			}
13400Sstevel@tonic-gate 			if (sctp->sctp_state == SCTPS_COOKIE_WAIT) {
13413448Sdh155122 				BUMP_MIB(&sctps->sctps_mib, sctpAborted);
13423314Skcpoon 				sctp_assoc_event(sctp, SCTP_CANT_STR_ASSOC, 0,
13433314Skcpoon 				    NULL);
13440Sstevel@tonic-gate 				sctp_clean_death(sctp, ECONNREFUSED);
13450Sstevel@tonic-gate 			}
13460Sstevel@tonic-gate 			break;
13470Sstevel@tonic-gate 		}
13480Sstevel@tonic-gate 		break;
13490Sstevel@tonic-gate 
13500Sstevel@tonic-gate 	case ICMP6_TIME_EXCEEDED:
13510Sstevel@tonic-gate 	default:
13520Sstevel@tonic-gate 		break;
13530Sstevel@tonic-gate 	}
13540Sstevel@tonic-gate 	freemsg(mp);
13550Sstevel@tonic-gate }
13560Sstevel@tonic-gate 
13570Sstevel@tonic-gate /*
13580Sstevel@tonic-gate  * Called by sockfs to create a new sctp instance.
13590Sstevel@tonic-gate  *
13600Sstevel@tonic-gate  * If parent pointer is passed in, inherit settings from it.
13610Sstevel@tonic-gate  */
13620Sstevel@tonic-gate sctp_t *
1363*11042SErik.Nordmark@Sun.COM sctp_create(void *ulpd, sctp_t *parent, int family, int type, int flags,
13648348SEric.Yu@Sun.COM     sock_upcalls_t *upcalls, sctp_sockbuf_limits_t *sbl,
13650Sstevel@tonic-gate     cred_t *credp)
13660Sstevel@tonic-gate {
13670Sstevel@tonic-gate 	sctp_t		*sctp, *psctp;
1368*11042SErik.Nordmark@Sun.COM 	conn_t		*connp;
13690Sstevel@tonic-gate 	mblk_t		*ack_mp, *hb_mp;
13700Sstevel@tonic-gate 	int		sleep = flags & SCTP_CAN_BLOCK ? KM_SLEEP : KM_NOSLEEP;
13713448Sdh155122 	zoneid_t	zoneid;
13723448Sdh155122 	sctp_stack_t	*sctps;
13730Sstevel@tonic-gate 
13740Sstevel@tonic-gate 	/* User must supply a credential. */
13750Sstevel@tonic-gate 	if (credp == NULL)
13760Sstevel@tonic-gate 		return (NULL);
13770Sstevel@tonic-gate 
13783448Sdh155122 	psctp = (sctp_t *)parent;
13793448Sdh155122 	if (psctp != NULL) {
13803448Sdh155122 		sctps = psctp->sctp_sctps;
13813448Sdh155122 		/* Increase here to have common decrease at end */
13823448Sdh155122 		netstack_hold(sctps->sctps_netstack);
13833448Sdh155122 	} else {
13843448Sdh155122 		netstack_t *ns;
13853448Sdh155122 
13863448Sdh155122 		ns = netstack_find_by_cred(credp);
13873448Sdh155122 		ASSERT(ns != NULL);
13883448Sdh155122 		sctps = ns->netstack_sctp;
13893448Sdh155122 		ASSERT(sctps != NULL);
13903448Sdh155122 
13913448Sdh155122 		/*
13923448Sdh155122 		 * For exclusive stacks we set the zoneid to zero
13933448Sdh155122 		 * to make SCTP operate as if in the global zone.
13943448Sdh155122 		 */
13953448Sdh155122 		if (sctps->sctps_netstack->netstack_stackid !=
13963448Sdh155122 		    GLOBAL_NETSTACKID)
13973448Sdh155122 			zoneid = GLOBAL_ZONEID;
13983448Sdh155122 		else
13993448Sdh155122 			zoneid = crgetzoneid(credp);
14003448Sdh155122 	}
1401*11042SErik.Nordmark@Sun.COM 	if ((connp = ipcl_conn_create(IPCL_SCTPCONN, sleep,
14024505Skcpoon 	    sctps->sctps_netstack)) == NULL) {
14033448Sdh155122 		netstack_rele(sctps->sctps_netstack);
14043448Sdh155122 		SCTP_KSTAT(sctps, sctp_conn_create);
14050Sstevel@tonic-gate 		return (NULL);
14061735Skcpoon 	}
14073448Sdh155122 	/*
14083448Sdh155122 	 * ipcl_conn_create did a netstack_hold. Undo the hold that was
14093448Sdh155122 	 * done at top of sctp_create.
14103448Sdh155122 	 */
14113448Sdh155122 	netstack_rele(sctps->sctps_netstack);
1412*11042SErik.Nordmark@Sun.COM 	sctp = CONN2SCTP(connp);
14133448Sdh155122 	sctp->sctp_sctps = sctps;
14141676Sjpk 
14154691Skcpoon 	if ((ack_mp = sctp_timer_alloc(sctp, sctp_ack_timer, sleep)) == NULL ||
14164691Skcpoon 	    (hb_mp = sctp_timer_alloc(sctp, sctp_heartbeat_timer,
14174691Skcpoon 	    sleep)) == NULL) {
14180Sstevel@tonic-gate 		if (ack_mp != NULL)
14190Sstevel@tonic-gate 			freeb(ack_mp);
1420*11042SErik.Nordmark@Sun.COM 		sctp_conn_clear(connp);
14213448Sdh155122 		sctp->sctp_sctps = NULL;
1422*11042SErik.Nordmark@Sun.COM 		kmem_cache_free(sctp_conn_cache, connp);
14230Sstevel@tonic-gate 		return (NULL);
14240Sstevel@tonic-gate 	}
14250Sstevel@tonic-gate 
14260Sstevel@tonic-gate 	sctp->sctp_ack_mp = ack_mp;
14270Sstevel@tonic-gate 	sctp->sctp_heartbeat_mp = hb_mp;
14280Sstevel@tonic-gate 
1429*11042SErik.Nordmark@Sun.COM 	/*
1430*11042SErik.Nordmark@Sun.COM 	 * Have conn_ip_output drop packets should our outer source
1431*11042SErik.Nordmark@Sun.COM 	 * go invalid, and tell us about mtu changes.
1432*11042SErik.Nordmark@Sun.COM 	 */
1433*11042SErik.Nordmark@Sun.COM 	connp->conn_ixa->ixa_flags |= IXAF_SET_ULP_CKSUM | IXAF_VERIFY_SOURCE |
1434*11042SErik.Nordmark@Sun.COM 	    IXAF_VERIFY_PMTU;
1435*11042SErik.Nordmark@Sun.COM 	connp->conn_family = family;
1436*11042SErik.Nordmark@Sun.COM 	connp->conn_so_type = type;
14370Sstevel@tonic-gate 
14380Sstevel@tonic-gate 	if (sctp_init_values(sctp, psctp, sleep) != 0) {
14390Sstevel@tonic-gate 		freeb(ack_mp);
14400Sstevel@tonic-gate 		freeb(hb_mp);
1441*11042SErik.Nordmark@Sun.COM 		sctp_conn_clear(connp);
14423448Sdh155122 		sctp->sctp_sctps = NULL;
1443*11042SErik.Nordmark@Sun.COM 		kmem_cache_free(sctp_conn_cache, connp);
14440Sstevel@tonic-gate 		return (NULL);
14450Sstevel@tonic-gate 	}
14460Sstevel@tonic-gate 	sctp->sctp_cansleep = ((flags & SCTP_CAN_BLOCK) == SCTP_CAN_BLOCK);
14470Sstevel@tonic-gate 
14483448Sdh155122 	sctp->sctp_mss = sctps->sctps_initial_mtu - ((family == AF_INET6) ?
14494505Skcpoon 	    sctp->sctp_hdr6_len : sctp->sctp_hdr_len);
14500Sstevel@tonic-gate 
14510Sstevel@tonic-gate 	if (psctp != NULL) {
1452*11042SErik.Nordmark@Sun.COM 		conn_t	*pconnp = psctp->sctp_connp;
1453*11042SErik.Nordmark@Sun.COM 
14540Sstevel@tonic-gate 		RUN_SCTP(psctp);
14550Sstevel@tonic-gate 		/*
14560Sstevel@tonic-gate 		 * Inherit local address list, local port. Parent is either
14570Sstevel@tonic-gate 		 * in SCTPS_BOUND, or SCTPS_LISTEN state.
14580Sstevel@tonic-gate 		 */
14590Sstevel@tonic-gate 		ASSERT((psctp->sctp_state == SCTPS_BOUND) ||
14600Sstevel@tonic-gate 		    (psctp->sctp_state == SCTPS_LISTEN));
14610Sstevel@tonic-gate 		if (sctp_dup_saddrs(psctp, sctp, sleep)) {
14620Sstevel@tonic-gate 			WAKE_SCTP(psctp);
14630Sstevel@tonic-gate 			freeb(ack_mp);
14640Sstevel@tonic-gate 			freeb(hb_mp);
14650Sstevel@tonic-gate 			sctp_headers_free(sctp);
1466*11042SErik.Nordmark@Sun.COM 			sctp_conn_clear(connp);
14673448Sdh155122 			sctp->sctp_sctps = NULL;
1468*11042SErik.Nordmark@Sun.COM 			kmem_cache_free(sctp_conn_cache, connp);
14690Sstevel@tonic-gate 			return (NULL);
14700Sstevel@tonic-gate 		}
14710Sstevel@tonic-gate 
14720Sstevel@tonic-gate 		/*
14730Sstevel@tonic-gate 		 * If the parent is specified, it'll be immediatelly
14740Sstevel@tonic-gate 		 * followed by sctp_connect(). So don't add this guy to
14750Sstevel@tonic-gate 		 * bind hash.
14760Sstevel@tonic-gate 		 */
1477*11042SErik.Nordmark@Sun.COM 		connp->conn_lport = pconnp->conn_lport;
14780Sstevel@tonic-gate 		sctp->sctp_state = SCTPS_BOUND;
14790Sstevel@tonic-gate 		WAKE_SCTP(psctp);
14800Sstevel@tonic-gate 	} else {
1481*11042SErik.Nordmark@Sun.COM 		ASSERT(connp->conn_cred == NULL);
1482*11042SErik.Nordmark@Sun.COM 		connp->conn_zoneid = zoneid;
1483*11042SErik.Nordmark@Sun.COM 		/*
1484*11042SErik.Nordmark@Sun.COM 		 * conn_allzones can not be set this early, hence
1485*11042SErik.Nordmark@Sun.COM 		 * no IPCL_ZONEID
1486*11042SErik.Nordmark@Sun.COM 		 */
1487*11042SErik.Nordmark@Sun.COM 		connp->conn_ixa->ixa_zoneid = zoneid;
1488*11042SErik.Nordmark@Sun.COM 		connp->conn_open_time = lbolt64;
1489*11042SErik.Nordmark@Sun.COM 		connp->conn_cred = credp;
1490*11042SErik.Nordmark@Sun.COM 		crhold(credp);
1491*11042SErik.Nordmark@Sun.COM 		connp->conn_cpid = curproc->p_pid;
14928778SErik.Nordmark@Sun.COM 
1493*11042SErik.Nordmark@Sun.COM 		/*
1494*11042SErik.Nordmark@Sun.COM 		 * If the caller has the process-wide flag set, then default to
1495*11042SErik.Nordmark@Sun.COM 		 * MAC exempt mode.  This allows read-down to unlabeled hosts.
1496*11042SErik.Nordmark@Sun.COM 		 */
1497*11042SErik.Nordmark@Sun.COM 		if (getpflags(NET_MAC_AWARE, credp) != 0)
1498*11042SErik.Nordmark@Sun.COM 			connp->conn_mac_mode = CONN_MAC_AWARE;
14990Sstevel@tonic-gate 
1500*11042SErik.Nordmark@Sun.COM 		connp->conn_zone_is_global =
1501*11042SErik.Nordmark@Sun.COM 		    (crgetzoneid(credp) == GLOBAL_ZONEID);
1502*11042SErik.Nordmark@Sun.COM 	}
15031676Sjpk 
15040Sstevel@tonic-gate 	/* Initialize SCTP instance values,  our verf tag must never be 0 */
15050Sstevel@tonic-gate 	(void) random_get_pseudo_bytes((uint8_t *)&sctp->sctp_lvtag,
15060Sstevel@tonic-gate 	    sizeof (sctp->sctp_lvtag));
15070Sstevel@tonic-gate 	if (sctp->sctp_lvtag == 0)
15080Sstevel@tonic-gate 		sctp->sctp_lvtag = (uint32_t)gethrtime();
15090Sstevel@tonic-gate 	ASSERT(sctp->sctp_lvtag != 0);
15100Sstevel@tonic-gate 
15110Sstevel@tonic-gate 	sctp->sctp_ltsn = sctp->sctp_lvtag + 1;
15120Sstevel@tonic-gate 	sctp->sctp_lcsn = sctp->sctp_ltsn;
15130Sstevel@tonic-gate 	sctp->sctp_recovery_tsn = sctp->sctp_lastack_rxd = sctp->sctp_ltsn - 1;
15140Sstevel@tonic-gate 	sctp->sctp_adv_pap = sctp->sctp_lastack_rxd;
15150Sstevel@tonic-gate 
15160Sstevel@tonic-gate 	/* Information required by upper layer */
1517*11042SErik.Nordmark@Sun.COM 	ASSERT(ulpd != NULL);
1518*11042SErik.Nordmark@Sun.COM 	sctp->sctp_ulpd = ulpd;
15190Sstevel@tonic-gate 
1520*11042SErik.Nordmark@Sun.COM 	ASSERT(upcalls != NULL);
1521*11042SErik.Nordmark@Sun.COM 	sctp->sctp_upcalls = upcalls;
1522*11042SErik.Nordmark@Sun.COM 	ASSERT(sbl != NULL);
1523*11042SErik.Nordmark@Sun.COM 	/* Fill in the socket buffer limits for sctpsockfs */
1524*11042SErik.Nordmark@Sun.COM 	sbl->sbl_txlowat = connp->conn_sndlowat;
1525*11042SErik.Nordmark@Sun.COM 	sbl->sbl_txbuf = connp->conn_sndbuf;
1526*11042SErik.Nordmark@Sun.COM 	sbl->sbl_rxbuf = sctp->sctp_rwnd;
1527*11042SErik.Nordmark@Sun.COM 	sbl->sbl_rxlowat = SCTP_RECV_LOWATER;
15280Sstevel@tonic-gate 
15290Sstevel@tonic-gate 	/* Insert this in the global list. */
15303448Sdh155122 	SCTP_LINK(sctp, sctps);
15310Sstevel@tonic-gate 
15320Sstevel@tonic-gate 	return (sctp);
15330Sstevel@tonic-gate }
15340Sstevel@tonic-gate 
15353448Sdh155122 /* Run at module load time */
15363448Sdh155122 void
15373448Sdh155122 sctp_ddi_g_init(void)
15383448Sdh155122 {
15390Sstevel@tonic-gate 	/* Create sctp_t/conn_t cache */
15400Sstevel@tonic-gate 	sctp_conn_cache_init();
15410Sstevel@tonic-gate 
15420Sstevel@tonic-gate 	/* Create the faddr cache */
15430Sstevel@tonic-gate 	sctp_faddr_init();
15440Sstevel@tonic-gate 
15450Sstevel@tonic-gate 	/* Create the sets cache */
15460Sstevel@tonic-gate 	sctp_sets_init();
15470Sstevel@tonic-gate 
15480Sstevel@tonic-gate 	/* Create the PR-SCTP sets cache */
15490Sstevel@tonic-gate 	sctp_ftsn_sets_init();
15500Sstevel@tonic-gate 
15510Sstevel@tonic-gate 	/* Initialize tables used for CRC calculation */
15520Sstevel@tonic-gate 	sctp_crc32_init();
15530Sstevel@tonic-gate 
15543448Sdh155122 	/*
15553448Sdh155122 	 * We want to be informed each time a stack is created or
15563448Sdh155122 	 * destroyed in the kernel, so we can maintain the
15573448Sdh155122 	 * set of sctp_stack_t's.
15583448Sdh155122 	 */
1559*11042SErik.Nordmark@Sun.COM 	netstack_register(NS_SCTP, sctp_stack_init, NULL, sctp_stack_fini);
15600Sstevel@tonic-gate }
15610Sstevel@tonic-gate 
15623448Sdh155122 static void *
15633448Sdh155122 sctp_stack_init(netstackid_t stackid, netstack_t *ns)
15640Sstevel@tonic-gate {
15653448Sdh155122 	sctp_stack_t	*sctps;
15663448Sdh155122 
15673448Sdh155122 	sctps = kmem_zalloc(sizeof (*sctps), KM_SLEEP);
15683448Sdh155122 	sctps->sctps_netstack = ns;
15693448Sdh155122 
15703448Sdh155122 	/* Initialize locks */
15713448Sdh155122 	mutex_init(&sctps->sctps_g_lock, NULL, MUTEX_DEFAULT, NULL);
15723448Sdh155122 	mutex_init(&sctps->sctps_epriv_port_lock, NULL, MUTEX_DEFAULT, NULL);
15733448Sdh155122 	sctps->sctps_g_num_epriv_ports = SCTP_NUM_EPRIV_PORTS;
15743448Sdh155122 	sctps->sctps_g_epriv_ports[0] = 2049;
15753448Sdh155122 	sctps->sctps_g_epriv_ports[1] = 4045;
15763448Sdh155122 
15773448Sdh155122 	/* Initialize SCTP hash arrays. */
15783448Sdh155122 	sctp_hash_init(sctps);
15793448Sdh155122 
15803448Sdh155122 	if (!sctp_nd_init(sctps)) {
15813448Sdh155122 		sctp_nd_free(sctps);
15823448Sdh155122 	}
15830Sstevel@tonic-gate 
15843448Sdh155122 	/* Initialize the recvq taskq. */
15853448Sdh155122 	sctp_rq_tq_init(sctps);
15863448Sdh155122 
15873448Sdh155122 	/* saddr init */
15883448Sdh155122 	sctp_saddr_init(sctps);
15893448Sdh155122 
15903448Sdh155122 	/* Global SCTP PCB list. */
15913448Sdh155122 	list_create(&sctps->sctps_g_list, sizeof (sctp_t),
15923448Sdh155122 	    offsetof(sctp_t, sctp_list));
15933448Sdh155122 
15943448Sdh155122 	/* Initialize sctp kernel stats. */
15953448Sdh155122 	sctps->sctps_mibkp = sctp_kstat_init(stackid);
15963448Sdh155122 	sctps->sctps_kstat =
15973448Sdh155122 	    sctp_kstat2_init(stackid, &sctps->sctps_statistics);
15983448Sdh155122 
15993448Sdh155122 	return (sctps);
16003448Sdh155122 }
16013448Sdh155122 
16023448Sdh155122 /*
16033448Sdh155122  * Called when the module is about to be unloaded.
16043448Sdh155122  */
16053448Sdh155122 void
16063448Sdh155122 sctp_ddi_g_destroy(void)
16073448Sdh155122 {
16080Sstevel@tonic-gate 	/* Destroy sctp_t/conn_t caches */
16090Sstevel@tonic-gate 	sctp_conn_cache_fini();
16100Sstevel@tonic-gate 
16110Sstevel@tonic-gate 	/* Destroy the faddr cache */
16120Sstevel@tonic-gate 	sctp_faddr_fini();
16130Sstevel@tonic-gate 
16140Sstevel@tonic-gate 	/* Destroy the sets cache */
16150Sstevel@tonic-gate 	sctp_sets_fini();
16160Sstevel@tonic-gate 
16170Sstevel@tonic-gate 	/* Destroy the PR-SCTP sets cache */
16180Sstevel@tonic-gate 	sctp_ftsn_sets_fini();
16190Sstevel@tonic-gate 
16203448Sdh155122 	netstack_unregister(NS_SCTP);
16213448Sdh155122 }
16223448Sdh155122 
16233448Sdh155122 /*
16243448Sdh155122  * Free the SCTP stack instance.
16253448Sdh155122  */
16263448Sdh155122 static void
16273448Sdh155122 sctp_stack_fini(netstackid_t stackid, void *arg)
16283448Sdh155122 {
16293448Sdh155122 	sctp_stack_t *sctps = (sctp_stack_t *)arg;
16303448Sdh155122 
16313448Sdh155122 	sctp_nd_free(sctps);
16323448Sdh155122 
16330Sstevel@tonic-gate 	/* Destroy the recvq taskqs. */
16343448Sdh155122 	sctp_rq_tq_fini(sctps);
16350Sstevel@tonic-gate 
16360Sstevel@tonic-gate 	/* Destroy saddr  */
16373448Sdh155122 	sctp_saddr_fini(sctps);
16380Sstevel@tonic-gate 
16390Sstevel@tonic-gate 	/* Global SCTP PCB list. */
16403448Sdh155122 	list_destroy(&sctps->sctps_g_list);
16410Sstevel@tonic-gate 
16420Sstevel@tonic-gate 	/* Destroy SCTP hash arrays. */
16433448Sdh155122 	sctp_hash_destroy(sctps);
16443448Sdh155122 
16453448Sdh155122 	/* Destroy SCTP kernel stats. */
16463448Sdh155122 	sctp_kstat2_fini(stackid, sctps->sctps_kstat);
16473448Sdh155122 	sctps->sctps_kstat = NULL;
16483448Sdh155122 	bzero(&sctps->sctps_statistics, sizeof (sctps->sctps_statistics));
16493448Sdh155122 
16503448Sdh155122 	sctp_kstat_fini(stackid, sctps->sctps_mibkp);
16513448Sdh155122 	sctps->sctps_mibkp = NULL;
16520Sstevel@tonic-gate 
16533448Sdh155122 	mutex_destroy(&sctps->sctps_g_lock);
16543448Sdh155122 	mutex_destroy(&sctps->sctps_epriv_port_lock);
16553448Sdh155122 
16563448Sdh155122 	kmem_free(sctps, sizeof (*sctps));
16570Sstevel@tonic-gate }
16580Sstevel@tonic-gate 
16590Sstevel@tonic-gate void
16603448Sdh155122 sctp_display_all(sctp_stack_t *sctps)
16610Sstevel@tonic-gate {
16620Sstevel@tonic-gate 	sctp_t *sctp_walker;
16630Sstevel@tonic-gate 
16643448Sdh155122 	mutex_enter(&sctps->sctps_g_lock);
1665*11042SErik.Nordmark@Sun.COM 	for (sctp_walker = list_head(&sctps->sctps_g_list);
1666*11042SErik.Nordmark@Sun.COM 	    sctp_walker != NULL;
16673448Sdh155122 	    sctp_walker = (sctp_t *)list_next(&sctps->sctps_g_list,
16683448Sdh155122 	    sctp_walker)) {
16690Sstevel@tonic-gate 		(void) sctp_display(sctp_walker, NULL);
16700Sstevel@tonic-gate 	}
16713448Sdh155122 	mutex_exit(&sctps->sctps_g_lock);
16720Sstevel@tonic-gate }
16730Sstevel@tonic-gate 
16740Sstevel@tonic-gate static void
16753448Sdh155122 sctp_rq_tq_init(sctp_stack_t *sctps)
16760Sstevel@tonic-gate {
16773448Sdh155122 	sctps->sctps_recvq_tq_list_max_sz = 16;
16783448Sdh155122 	sctps->sctps_recvq_tq_list_cur_sz = 1;
16790Sstevel@tonic-gate 	/*
16800Sstevel@tonic-gate 	 * Initialize the recvq_tq_list and create the first recvq taskq.
16810Sstevel@tonic-gate 	 * What to do if it fails?
16820Sstevel@tonic-gate 	 */
16833448Sdh155122 	sctps->sctps_recvq_tq_list =
16843448Sdh155122 	    kmem_zalloc(sctps->sctps_recvq_tq_list_max_sz * sizeof (taskq_t *),
16854505Skcpoon 	    KM_SLEEP);
16863448Sdh155122 	sctps->sctps_recvq_tq_list[0] = taskq_create("sctp_def_recvq_taskq",
16870Sstevel@tonic-gate 	    MIN(sctp_recvq_tq_thr_max, MAX(sctp_recvq_tq_thr_min, ncpus)),
16880Sstevel@tonic-gate 	    minclsyspri, sctp_recvq_tq_task_min, sctp_recvq_tq_task_max,
16890Sstevel@tonic-gate 	    TASKQ_PREPOPULATE);
16903448Sdh155122 	mutex_init(&sctps->sctps_rq_tq_lock, NULL, MUTEX_DEFAULT, NULL);
16910Sstevel@tonic-gate }
16920Sstevel@tonic-gate 
16930Sstevel@tonic-gate static void
16943448Sdh155122 sctp_rq_tq_fini(sctp_stack_t *sctps)
16950Sstevel@tonic-gate {
16960Sstevel@tonic-gate 	int i;
16970Sstevel@tonic-gate 
16983448Sdh155122 	for (i = 0; i < sctps->sctps_recvq_tq_list_cur_sz; i++) {
16993448Sdh155122 		ASSERT(sctps->sctps_recvq_tq_list[i] != NULL);
17003448Sdh155122 		taskq_destroy(sctps->sctps_recvq_tq_list[i]);
17010Sstevel@tonic-gate 	}
17023448Sdh155122 	kmem_free(sctps->sctps_recvq_tq_list,
17033448Sdh155122 	    sctps->sctps_recvq_tq_list_max_sz * sizeof (taskq_t *));
17043448Sdh155122 	sctps->sctps_recvq_tq_list = NULL;
17050Sstevel@tonic-gate }
17060Sstevel@tonic-gate 
17070Sstevel@tonic-gate /* Add another taskq for a new ill. */
17080Sstevel@tonic-gate void
17093448Sdh155122 sctp_inc_taskq(sctp_stack_t *sctps)
17100Sstevel@tonic-gate {
17110Sstevel@tonic-gate 	taskq_t *tq;
17120Sstevel@tonic-gate 	char tq_name[TASKQ_NAMELEN];
17130Sstevel@tonic-gate 
17143448Sdh155122 	mutex_enter(&sctps->sctps_rq_tq_lock);
17153448Sdh155122 	if (sctps->sctps_recvq_tq_list_cur_sz + 1 >
17163448Sdh155122 	    sctps->sctps_recvq_tq_list_max_sz) {
17173448Sdh155122 		mutex_exit(&sctps->sctps_rq_tq_lock);
17180Sstevel@tonic-gate 		cmn_err(CE_NOTE, "Cannot create more SCTP recvq taskq");
17190Sstevel@tonic-gate 		return;
17200Sstevel@tonic-gate 	}
17210Sstevel@tonic-gate 
17220Sstevel@tonic-gate 	(void) snprintf(tq_name, sizeof (tq_name), "sctp_recvq_taskq_%u",
17233448Sdh155122 	    sctps->sctps_recvq_tq_list_cur_sz);
17240Sstevel@tonic-gate 	tq = taskq_create(tq_name,
17250Sstevel@tonic-gate 	    MIN(sctp_recvq_tq_thr_max, MAX(sctp_recvq_tq_thr_min, ncpus)),
17260Sstevel@tonic-gate 	    minclsyspri, sctp_recvq_tq_task_min, sctp_recvq_tq_task_max,
17270Sstevel@tonic-gate 	    TASKQ_PREPOPULATE);
17280Sstevel@tonic-gate 	if (tq == NULL) {
17293448Sdh155122 		mutex_exit(&sctps->sctps_rq_tq_lock);
17300Sstevel@tonic-gate 		cmn_err(CE_NOTE, "SCTP recvq taskq creation failed");
17310Sstevel@tonic-gate 		return;
17320Sstevel@tonic-gate 	}
17333448Sdh155122 	ASSERT(sctps->sctps_recvq_tq_list[
17344505Skcpoon 	    sctps->sctps_recvq_tq_list_cur_sz] == NULL);
17353448Sdh155122 	sctps->sctps_recvq_tq_list[sctps->sctps_recvq_tq_list_cur_sz] = tq;
17363448Sdh155122 	atomic_add_32(&sctps->sctps_recvq_tq_list_cur_sz, 1);
17373448Sdh155122 	mutex_exit(&sctps->sctps_rq_tq_lock);
17380Sstevel@tonic-gate }
17390Sstevel@tonic-gate 
17400Sstevel@tonic-gate #ifdef DEBUG
17410Sstevel@tonic-gate uint32_t recvq_loop_cnt = 0;
17420Sstevel@tonic-gate uint32_t recvq_call = 0;
17430Sstevel@tonic-gate #endif
17440Sstevel@tonic-gate 
17450Sstevel@tonic-gate /*
17460Sstevel@tonic-gate  * Find the next recvq_tq to use.  This routine will go thru all the
17470Sstevel@tonic-gate  * taskqs until it can dispatch a job for the sctp.  If this fails,
17480Sstevel@tonic-gate  * it will create a new taskq and try it.
17490Sstevel@tonic-gate  */
17500Sstevel@tonic-gate static boolean_t
17510Sstevel@tonic-gate sctp_find_next_tq(sctp_t *sctp)
17520Sstevel@tonic-gate {
17530Sstevel@tonic-gate 	int next_tq, try;
17540Sstevel@tonic-gate 	taskq_t *tq;
17553448Sdh155122 	sctp_stack_t	*sctps = sctp->sctp_sctps;
17560Sstevel@tonic-gate 
17570Sstevel@tonic-gate 	/*
17580Sstevel@tonic-gate 	 * Note that since we don't hold a lock on sctp_rq_tq_lock for
17590Sstevel@tonic-gate 	 * performance reason, recvq_ta_list_cur_sz can be changed during
17600Sstevel@tonic-gate 	 * this loop.  The problem this will create is that the loop may
17610Sstevel@tonic-gate 	 * not have tried all the recvq_tq.  This should be OK.
17620Sstevel@tonic-gate 	 */
17633448Sdh155122 	next_tq = atomic_add_32_nv(&sctps->sctps_recvq_tq_list_cur, 1) %
17643448Sdh155122 	    sctps->sctps_recvq_tq_list_cur_sz;
17653448Sdh155122 	for (try = 0; try < sctps->sctps_recvq_tq_list_cur_sz; try++) {
17663448Sdh155122 		tq = sctps->sctps_recvq_tq_list[next_tq];
17670Sstevel@tonic-gate 		if (taskq_dispatch(tq, sctp_process_recvq, sctp,
17680Sstevel@tonic-gate 		    TQ_NOSLEEP) != NULL) {
17690Sstevel@tonic-gate 			sctp->sctp_recvq_tq = tq;
17700Sstevel@tonic-gate 			return (B_TRUE);
17710Sstevel@tonic-gate 		}
17723448Sdh155122 		next_tq = (next_tq + 1) % sctps->sctps_recvq_tq_list_cur_sz;
17730Sstevel@tonic-gate 	}
17740Sstevel@tonic-gate 
17750Sstevel@tonic-gate 	/*
17760Sstevel@tonic-gate 	 * Create one more taskq and try it.  Note that sctp_inc_taskq()
17770Sstevel@tonic-gate 	 * may not have created another taskq if the number of recvq
17780Sstevel@tonic-gate 	 * taskqs is at the maximum.  We are probably in a pretty bad
17790Sstevel@tonic-gate 	 * shape if this actually happens...
17800Sstevel@tonic-gate 	 */
17813448Sdh155122 	sctp_inc_taskq(sctps);
17823448Sdh155122 	tq = sctps->sctps_recvq_tq_list[sctps->sctps_recvq_tq_list_cur_sz - 1];
17830Sstevel@tonic-gate 	if (taskq_dispatch(tq, sctp_process_recvq, sctp, TQ_NOSLEEP) != NULL) {
17840Sstevel@tonic-gate 		sctp->sctp_recvq_tq = tq;
17850Sstevel@tonic-gate 		return (B_TRUE);
17860Sstevel@tonic-gate 	}
17873448Sdh155122 	SCTP_KSTAT(sctps, sctp_find_next_tq);
17880Sstevel@tonic-gate 	return (B_FALSE);
17890Sstevel@tonic-gate }
17900Sstevel@tonic-gate 
17910Sstevel@tonic-gate /*
17920Sstevel@tonic-gate  * To add a message to the recvq.  Note that the sctp_timer_fire()
17930Sstevel@tonic-gate  * routine also uses this function to add the timer message to the
17940Sstevel@tonic-gate  * receive queue for later processing.  And it should be the only
17950Sstevel@tonic-gate  * caller of sctp_add_recvq() which sets the try_harder argument
17960Sstevel@tonic-gate  * to B_TRUE.
17970Sstevel@tonic-gate  *
17980Sstevel@tonic-gate  * If the try_harder argument is B_TRUE, this routine sctp_find_next_tq()
17990Sstevel@tonic-gate  * will try very hard to dispatch the task.  Refer to the comment
18000Sstevel@tonic-gate  * for that routine on how it does that.
1801*11042SErik.Nordmark@Sun.COM  *
1802*11042SErik.Nordmark@Sun.COM  * On failure the message has been freed i.e., this routine always consumes the
1803*11042SErik.Nordmark@Sun.COM  * message. It bumps ipIfStatsInDiscards and and uses ip_drop_input to drop.
18040Sstevel@tonic-gate  */
1805*11042SErik.Nordmark@Sun.COM void
1806*11042SErik.Nordmark@Sun.COM sctp_add_recvq(sctp_t *sctp, mblk_t *mp, boolean_t caller_hold_lock,
1807*11042SErik.Nordmark@Sun.COM     ip_recv_attr_t *ira)
18080Sstevel@tonic-gate {
1809*11042SErik.Nordmark@Sun.COM 	mblk_t	*attrmp;
1810*11042SErik.Nordmark@Sun.COM 	ip_stack_t	*ipst = sctp->sctp_sctps->sctps_netstack->netstack_ip;
1811*11042SErik.Nordmark@Sun.COM 
1812*11042SErik.Nordmark@Sun.COM 	ASSERT(ira->ira_ill == NULL);
1813*11042SErik.Nordmark@Sun.COM 
18140Sstevel@tonic-gate 	if (!caller_hold_lock)
18150Sstevel@tonic-gate 		mutex_enter(&sctp->sctp_recvq_lock);
18160Sstevel@tonic-gate 
18170Sstevel@tonic-gate 	/* If the taskq dispatch has not been scheduled, do it now. */
18180Sstevel@tonic-gate 	if (sctp->sctp_recvq_tq == NULL) {
18190Sstevel@tonic-gate 		ASSERT(sctp->sctp_recvq == NULL);
18200Sstevel@tonic-gate 		if (!sctp_find_next_tq(sctp)) {
18210Sstevel@tonic-gate 			if (!caller_hold_lock)
18220Sstevel@tonic-gate 				mutex_exit(&sctp->sctp_recvq_lock);
1823*11042SErik.Nordmark@Sun.COM 			BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsInDiscards);
1824*11042SErik.Nordmark@Sun.COM 			ip_drop_input("ipIfStatsInDiscards", mp, NULL);
1825*11042SErik.Nordmark@Sun.COM 			freemsg(mp);
1826*11042SErik.Nordmark@Sun.COM 			return;
18270Sstevel@tonic-gate 		}
18280Sstevel@tonic-gate 		/* Make sure the sctp_t will not go away. */
18290Sstevel@tonic-gate 		SCTP_REFHOLD(sctp);
18300Sstevel@tonic-gate 	}
18310Sstevel@tonic-gate 
1832*11042SErik.Nordmark@Sun.COM 	attrmp = ip_recv_attr_to_mblk(ira);
1833*11042SErik.Nordmark@Sun.COM 	if (attrmp == NULL) {
1834*11042SErik.Nordmark@Sun.COM 		if (!caller_hold_lock)
1835*11042SErik.Nordmark@Sun.COM 			mutex_exit(&sctp->sctp_recvq_lock);
1836*11042SErik.Nordmark@Sun.COM 		BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsInDiscards);
1837*11042SErik.Nordmark@Sun.COM 		ip_drop_input("ipIfStatsInDiscards", mp, NULL);
1838*11042SErik.Nordmark@Sun.COM 		freemsg(mp);
1839*11042SErik.Nordmark@Sun.COM 		return;
1840*11042SErik.Nordmark@Sun.COM 	}
1841*11042SErik.Nordmark@Sun.COM 	ASSERT(attrmp->b_cont == NULL);
1842*11042SErik.Nordmark@Sun.COM 	attrmp->b_cont = mp;
1843*11042SErik.Nordmark@Sun.COM 	mp = attrmp;
1844*11042SErik.Nordmark@Sun.COM 
18450Sstevel@tonic-gate 	if (sctp->sctp_recvq == NULL) {
18460Sstevel@tonic-gate 		sctp->sctp_recvq = mp;
18470Sstevel@tonic-gate 		sctp->sctp_recvq_tail = mp;
18480Sstevel@tonic-gate 	} else {
18490Sstevel@tonic-gate 		sctp->sctp_recvq_tail->b_next = mp;
18500Sstevel@tonic-gate 		sctp->sctp_recvq_tail = mp;
18510Sstevel@tonic-gate 	}
18520Sstevel@tonic-gate 
18530Sstevel@tonic-gate 	if (!caller_hold_lock)
18540Sstevel@tonic-gate 		mutex_exit(&sctp->sctp_recvq_lock);
18550Sstevel@tonic-gate }
18560Sstevel@tonic-gate 
18570Sstevel@tonic-gate static void
18580Sstevel@tonic-gate sctp_process_recvq(void *arg)
18590Sstevel@tonic-gate {
18600Sstevel@tonic-gate 	sctp_t		*sctp = (sctp_t *)arg;
18610Sstevel@tonic-gate 	mblk_t		*mp;
18620Sstevel@tonic-gate #ifdef DEBUG
18630Sstevel@tonic-gate 	uint32_t	loop_cnt = 0;
18640Sstevel@tonic-gate #endif
1865*11042SErik.Nordmark@Sun.COM 	ip_recv_attr_t	iras;
18660Sstevel@tonic-gate 
18670Sstevel@tonic-gate #ifdef	_BIG_ENDIAN
18680Sstevel@tonic-gate #define	IPVER(ip6h)	((((uint32_t *)ip6h)[0] >> 28) & 0x7)
18690Sstevel@tonic-gate #else
18700Sstevel@tonic-gate #define	IPVER(ip6h)	((((uint32_t *)ip6h)[0] >> 4) & 0x7)
18710Sstevel@tonic-gate #endif
18720Sstevel@tonic-gate 
18730Sstevel@tonic-gate 	RUN_SCTP(sctp);
18740Sstevel@tonic-gate 	mutex_enter(&sctp->sctp_recvq_lock);
18750Sstevel@tonic-gate 
18760Sstevel@tonic-gate #ifdef DEBUG
18770Sstevel@tonic-gate 	recvq_call++;
18780Sstevel@tonic-gate #endif
18790Sstevel@tonic-gate 	/*
18800Sstevel@tonic-gate 	 * Note that while we are in this loop, other thread can put
18810Sstevel@tonic-gate 	 * new packets in the receive queue.  We may be looping for
18820Sstevel@tonic-gate 	 * quite a while.
18830Sstevel@tonic-gate 	 */
18840Sstevel@tonic-gate 	while ((mp = sctp->sctp_recvq) != NULL) {
1885*11042SErik.Nordmark@Sun.COM 		mblk_t *data_mp;
1886*11042SErik.Nordmark@Sun.COM 
18870Sstevel@tonic-gate 		sctp->sctp_recvq = mp->b_next;
18880Sstevel@tonic-gate 		mutex_exit(&sctp->sctp_recvq_lock);
18890Sstevel@tonic-gate 		mp->b_next = NULL;
18900Sstevel@tonic-gate #ifdef DEBUG
18910Sstevel@tonic-gate 		loop_cnt++;
18920Sstevel@tonic-gate #endif
18930Sstevel@tonic-gate 		mp->b_prev = NULL;
18940Sstevel@tonic-gate 
1895*11042SErik.Nordmark@Sun.COM 		data_mp = mp->b_cont;
1896*11042SErik.Nordmark@Sun.COM 		mp->b_cont = NULL;
1897*11042SErik.Nordmark@Sun.COM 		if (!ip_recv_attr_from_mblk(mp, &iras)) {
1898*11042SErik.Nordmark@Sun.COM 			ip_drop_input("ip_recv_attr_from_mblk", mp, NULL);
1899*11042SErik.Nordmark@Sun.COM 			freemsg(mp);
1900*11042SErik.Nordmark@Sun.COM 			ira_cleanup(&iras, B_TRUE);
1901*11042SErik.Nordmark@Sun.COM 			continue;
1902*11042SErik.Nordmark@Sun.COM 		}
1903*11042SErik.Nordmark@Sun.COM 
1904*11042SErik.Nordmark@Sun.COM 		if (iras.ira_flags & IRAF_ICMP_ERROR)
1905*11042SErik.Nordmark@Sun.COM 			sctp_icmp_error(sctp, data_mp);
1906*11042SErik.Nordmark@Sun.COM 		else
1907*11042SErik.Nordmark@Sun.COM 			sctp_input_data(sctp, data_mp, &iras);
1908*11042SErik.Nordmark@Sun.COM 
1909*11042SErik.Nordmark@Sun.COM 		ira_cleanup(&iras, B_TRUE);
19100Sstevel@tonic-gate 		mutex_enter(&sctp->sctp_recvq_lock);
19110Sstevel@tonic-gate 	}
19120Sstevel@tonic-gate 
19130Sstevel@tonic-gate 	sctp->sctp_recvq_tail = NULL;
19140Sstevel@tonic-gate 	sctp->sctp_recvq_tq = NULL;
19150Sstevel@tonic-gate 
19160Sstevel@tonic-gate 	mutex_exit(&sctp->sctp_recvq_lock);
19170Sstevel@tonic-gate 
19180Sstevel@tonic-gate 	WAKE_SCTP(sctp);
19190Sstevel@tonic-gate 
19200Sstevel@tonic-gate #ifdef DEBUG
19210Sstevel@tonic-gate 	if (loop_cnt > recvq_loop_cnt)
19220Sstevel@tonic-gate 		recvq_loop_cnt = loop_cnt;
19230Sstevel@tonic-gate #endif
19240Sstevel@tonic-gate 	/* Now it can go away. */
19250Sstevel@tonic-gate 	SCTP_REFRELE(sctp);
19260Sstevel@tonic-gate }
19270Sstevel@tonic-gate 
19280Sstevel@tonic-gate /* ARGSUSED */
19290Sstevel@tonic-gate static int
19300Sstevel@tonic-gate sctp_conn_cache_constructor(void *buf, void *cdrarg, int kmflags)
19310Sstevel@tonic-gate {
1932*11042SErik.Nordmark@Sun.COM 	conn_t	*connp = (conn_t *)buf;
1933*11042SErik.Nordmark@Sun.COM 	sctp_t	*sctp = (sctp_t *)&connp[1];
19340Sstevel@tonic-gate 
1935*11042SErik.Nordmark@Sun.COM 	bzero(connp, sizeof (conn_t));
19360Sstevel@tonic-gate 	bzero(buf, (char *)&sctp[1] - (char *)buf);
19370Sstevel@tonic-gate 
19380Sstevel@tonic-gate 	mutex_init(&sctp->sctp_reflock, NULL, MUTEX_DEFAULT, NULL);
19390Sstevel@tonic-gate 	mutex_init(&sctp->sctp_lock, NULL, MUTEX_DEFAULT, NULL);
19400Sstevel@tonic-gate 	mutex_init(&sctp->sctp_recvq_lock, NULL, MUTEX_DEFAULT, NULL);
19410Sstevel@tonic-gate 	cv_init(&sctp->sctp_cv, NULL, CV_DEFAULT, NULL);
19420Sstevel@tonic-gate 
1943*11042SErik.Nordmark@Sun.COM 	mutex_init(&connp->conn_lock, NULL, MUTEX_DEFAULT, NULL);
1944*11042SErik.Nordmark@Sun.COM 	cv_init(&connp->conn_cv, NULL, CV_DEFAULT, NULL);
1945*11042SErik.Nordmark@Sun.COM 	connp->conn_flags = IPCL_SCTPCONN;
1946*11042SErik.Nordmark@Sun.COM 	connp->conn_proto = IPPROTO_SCTP;
1947*11042SErik.Nordmark@Sun.COM 	connp->conn_sctp = sctp;
1948*11042SErik.Nordmark@Sun.COM 	sctp->sctp_connp = connp;
1949*11042SErik.Nordmark@Sun.COM 	rw_init(&connp->conn_ilg_lock, NULL, RW_DEFAULT, NULL);
1950*11042SErik.Nordmark@Sun.COM 
1951*11042SErik.Nordmark@Sun.COM 	connp->conn_ixa = kmem_zalloc(sizeof (ip_xmit_attr_t), kmflags);
1952*11042SErik.Nordmark@Sun.COM 	if (connp->conn_ixa == NULL) {
1953*11042SErik.Nordmark@Sun.COM 		return (ENOMEM);
1954*11042SErik.Nordmark@Sun.COM 	}
1955*11042SErik.Nordmark@Sun.COM 	connp->conn_ixa->ixa_refcnt = 1;
1956*11042SErik.Nordmark@Sun.COM 	connp->conn_ixa->ixa_protocol = connp->conn_proto;
1957*11042SErik.Nordmark@Sun.COM 	connp->conn_ixa->ixa_xmit_hint = CONN_TO_XMIT_HINT(connp);
19580Sstevel@tonic-gate 	return (0);
19590Sstevel@tonic-gate }
19600Sstevel@tonic-gate 
19610Sstevel@tonic-gate /* ARGSUSED */
19620Sstevel@tonic-gate static void
19630Sstevel@tonic-gate sctp_conn_cache_destructor(void *buf, void *cdrarg)
19640Sstevel@tonic-gate {
1965*11042SErik.Nordmark@Sun.COM 	conn_t	*connp = (conn_t *)buf;
1966*11042SErik.Nordmark@Sun.COM 	sctp_t	*sctp = (sctp_t *)&connp[1];
19670Sstevel@tonic-gate 
1968*11042SErik.Nordmark@Sun.COM 	ASSERT(sctp->sctp_connp == connp);
19690Sstevel@tonic-gate 	ASSERT(!MUTEX_HELD(&sctp->sctp_lock));
19700Sstevel@tonic-gate 	ASSERT(!MUTEX_HELD(&sctp->sctp_reflock));
19710Sstevel@tonic-gate 	ASSERT(!MUTEX_HELD(&sctp->sctp_recvq_lock));
19720Sstevel@tonic-gate 
19730Sstevel@tonic-gate 	ASSERT(sctp->sctp_conn_hash_next == NULL);
19740Sstevel@tonic-gate 	ASSERT(sctp->sctp_conn_hash_prev == NULL);
19750Sstevel@tonic-gate 	ASSERT(sctp->sctp_listen_hash_next == NULL);
19760Sstevel@tonic-gate 	ASSERT(sctp->sctp_listen_hash_prev == NULL);
19770Sstevel@tonic-gate 	ASSERT(sctp->sctp_listen_tfp == NULL);
19780Sstevel@tonic-gate 	ASSERT(sctp->sctp_conn_tfp == NULL);
19790Sstevel@tonic-gate 
19800Sstevel@tonic-gate 	ASSERT(sctp->sctp_faddrs == NULL);
19810Sstevel@tonic-gate 	ASSERT(sctp->sctp_nsaddrs == 0);
19820Sstevel@tonic-gate 
19830Sstevel@tonic-gate 	ASSERT(sctp->sctp_ulpd == NULL);
19840Sstevel@tonic-gate 
19850Sstevel@tonic-gate 	ASSERT(sctp->sctp_lastfaddr == NULL);
19860Sstevel@tonic-gate 	ASSERT(sctp->sctp_primary == NULL);
19870Sstevel@tonic-gate 	ASSERT(sctp->sctp_current == NULL);
19880Sstevel@tonic-gate 	ASSERT(sctp->sctp_lastdata == NULL);
19890Sstevel@tonic-gate 
19900Sstevel@tonic-gate 	ASSERT(sctp->sctp_xmit_head == NULL);
19910Sstevel@tonic-gate 	ASSERT(sctp->sctp_xmit_tail == NULL);
19920Sstevel@tonic-gate 	ASSERT(sctp->sctp_xmit_unsent == NULL);
19930Sstevel@tonic-gate 	ASSERT(sctp->sctp_xmit_unsent_tail == NULL);
19940Sstevel@tonic-gate 
19950Sstevel@tonic-gate 	ASSERT(sctp->sctp_ostrcntrs == NULL);
19960Sstevel@tonic-gate 
19970Sstevel@tonic-gate 	ASSERT(sctp->sctp_sack_info == NULL);
19980Sstevel@tonic-gate 	ASSERT(sctp->sctp_ack_mp == NULL);
19990Sstevel@tonic-gate 	ASSERT(sctp->sctp_instr == NULL);
20000Sstevel@tonic-gate 
20010Sstevel@tonic-gate 	ASSERT(sctp->sctp_iphc == NULL);
20020Sstevel@tonic-gate 	ASSERT(sctp->sctp_iphc6 == NULL);
20030Sstevel@tonic-gate 	ASSERT(sctp->sctp_ipha == NULL);
20040Sstevel@tonic-gate 	ASSERT(sctp->sctp_ip6h == NULL);
20050Sstevel@tonic-gate 	ASSERT(sctp->sctp_sctph == NULL);
20060Sstevel@tonic-gate 	ASSERT(sctp->sctp_sctph6 == NULL);
20070Sstevel@tonic-gate 
20080Sstevel@tonic-gate 	ASSERT(sctp->sctp_cookie_mp == NULL);
20090Sstevel@tonic-gate 
20100Sstevel@tonic-gate 	ASSERT(sctp->sctp_refcnt == 0);
20110Sstevel@tonic-gate 	ASSERT(sctp->sctp_timer_mp == NULL);
20120Sstevel@tonic-gate 	ASSERT(sctp->sctp_connp->conn_ref == 0);
20130Sstevel@tonic-gate 	ASSERT(sctp->sctp_heartbeat_mp == NULL);
20140Sstevel@tonic-gate 	ASSERT(sctp->sctp_ptpbhn == NULL && sctp->sctp_bind_hash == NULL);
20150Sstevel@tonic-gate 
20160Sstevel@tonic-gate 	ASSERT(sctp->sctp_shutdown_faddr == NULL);
20170Sstevel@tonic-gate 
20180Sstevel@tonic-gate 	ASSERT(sctp->sctp_cxmit_list == NULL);
20190Sstevel@tonic-gate 
20200Sstevel@tonic-gate 	ASSERT(sctp->sctp_recvq == NULL);
20210Sstevel@tonic-gate 	ASSERT(sctp->sctp_recvq_tail == NULL);
20220Sstevel@tonic-gate 	ASSERT(sctp->sctp_recvq_tq == NULL);
20230Sstevel@tonic-gate 
20244691Skcpoon 	/*
20254691Skcpoon 	 * sctp_pad_mp can be NULL if the memory allocation fails
20264691Skcpoon 	 * in sctp_init_values() and the conn_t is freed.
20274691Skcpoon 	 */
20284691Skcpoon 	if (sctp->sctp_pad_mp != NULL) {
20294691Skcpoon 		freeb(sctp->sctp_pad_mp);
20304691Skcpoon 		sctp->sctp_pad_mp = NULL;
20314691Skcpoon 	}
20324691Skcpoon 
20330Sstevel@tonic-gate 	mutex_destroy(&sctp->sctp_reflock);
20340Sstevel@tonic-gate 	mutex_destroy(&sctp->sctp_lock);
20350Sstevel@tonic-gate 	mutex_destroy(&sctp->sctp_recvq_lock);
20360Sstevel@tonic-gate 	cv_destroy(&sctp->sctp_cv);
2037*11042SErik.Nordmark@Sun.COM 
2038*11042SErik.Nordmark@Sun.COM 	mutex_destroy(&connp->conn_lock);
2039*11042SErik.Nordmark@Sun.COM 	cv_destroy(&connp->conn_cv);
2040*11042SErik.Nordmark@Sun.COM 	rw_destroy(&connp->conn_ilg_lock);
20410Sstevel@tonic-gate 
2042*11042SErik.Nordmark@Sun.COM 	/* Can be NULL if constructor failed */
2043*11042SErik.Nordmark@Sun.COM 	if (connp->conn_ixa != NULL) {
2044*11042SErik.Nordmark@Sun.COM 		ASSERT(connp->conn_ixa->ixa_refcnt == 1);
2045*11042SErik.Nordmark@Sun.COM 		ASSERT(connp->conn_ixa->ixa_ire == NULL);
2046*11042SErik.Nordmark@Sun.COM 		ASSERT(connp->conn_ixa->ixa_nce == NULL);
2047*11042SErik.Nordmark@Sun.COM 		ixa_refrele(connp->conn_ixa);
2048*11042SErik.Nordmark@Sun.COM 	}
20490Sstevel@tonic-gate }
20500Sstevel@tonic-gate 
20510Sstevel@tonic-gate static void
20520Sstevel@tonic-gate sctp_conn_cache_init()
20530Sstevel@tonic-gate {
20540Sstevel@tonic-gate 	sctp_conn_cache = kmem_cache_create("sctp_conn_cache",
20550Sstevel@tonic-gate 	    sizeof (sctp_t) + sizeof (conn_t), 0, sctp_conn_cache_constructor,
20560Sstevel@tonic-gate 	    sctp_conn_cache_destructor, NULL, NULL, NULL, 0);
20570Sstevel@tonic-gate }
20580Sstevel@tonic-gate 
20590Sstevel@tonic-gate static void
20600Sstevel@tonic-gate sctp_conn_cache_fini()
20610Sstevel@tonic-gate {
20620Sstevel@tonic-gate 	kmem_cache_destroy(sctp_conn_cache);
20630Sstevel@tonic-gate }
20644691Skcpoon 
20654691Skcpoon void
20664691Skcpoon sctp_conn_init(conn_t *connp)
20674691Skcpoon {
2068*11042SErik.Nordmark@Sun.COM 	ASSERT(connp->conn_flags == IPCL_SCTPCONN);
20694691Skcpoon 	connp->conn_rq = connp->conn_wq = NULL;
2070*11042SErik.Nordmark@Sun.COM 	connp->conn_ixa->ixa_flags |= IXAF_SET_ULP_CKSUM | IXAF_VERIFY_SOURCE |
2071*11042SErik.Nordmark@Sun.COM 	    IXAF_VERIFY_PMTU;
2072*11042SErik.Nordmark@Sun.COM 
2073*11042SErik.Nordmark@Sun.COM 	ASSERT(connp->conn_proto == IPPROTO_SCTP);
2074*11042SErik.Nordmark@Sun.COM 	ASSERT(connp->conn_ixa->ixa_protocol == connp->conn_proto);
20754691Skcpoon 	connp->conn_state_flags |= CONN_INCIPIENT;
2076*11042SErik.Nordmark@Sun.COM 
2077*11042SErik.Nordmark@Sun.COM 	ASSERT(connp->conn_sctp != NULL);
2078*11042SErik.Nordmark@Sun.COM 
2079*11042SErik.Nordmark@Sun.COM 	/*
2080*11042SErik.Nordmark@Sun.COM 	 * Register sctp_notify to listen to capability changes detected by IP.
2081*11042SErik.Nordmark@Sun.COM 	 * This upcall is made in the context of the call to conn_ip_output
2082*11042SErik.Nordmark@Sun.COM 	 * thus it holds whatever locks sctp holds across conn_ip_output.
2083*11042SErik.Nordmark@Sun.COM 	 */
2084*11042SErik.Nordmark@Sun.COM 	connp->conn_ixa->ixa_notify = sctp_notify;
2085*11042SErik.Nordmark@Sun.COM 	connp->conn_ixa->ixa_notify_cookie = connp->conn_sctp;
20864691Skcpoon }
20874691Skcpoon 
20884691Skcpoon static void
20894691Skcpoon sctp_conn_clear(conn_t *connp)
20904691Skcpoon {
20914691Skcpoon 	/* Clean up conn_t stuff */
2092*11042SErik.Nordmark@Sun.COM 	if (connp->conn_latch != NULL) {
2093*11042SErik.Nordmark@Sun.COM 		IPLATCH_REFRELE(connp->conn_latch);
2094*11042SErik.Nordmark@Sun.COM 		connp->conn_latch = NULL;
2095*11042SErik.Nordmark@Sun.COM 	}
2096*11042SErik.Nordmark@Sun.COM 	if (connp->conn_latch_in_policy != NULL) {
2097*11042SErik.Nordmark@Sun.COM 		IPPOL_REFRELE(connp->conn_latch_in_policy);
2098*11042SErik.Nordmark@Sun.COM 		connp->conn_latch_in_policy = NULL;
2099*11042SErik.Nordmark@Sun.COM 	}
2100*11042SErik.Nordmark@Sun.COM 	if (connp->conn_latch_in_action != NULL) {
2101*11042SErik.Nordmark@Sun.COM 		IPACT_REFRELE(connp->conn_latch_in_action);
2102*11042SErik.Nordmark@Sun.COM 		connp->conn_latch_in_action = NULL;
2103*11042SErik.Nordmark@Sun.COM 	}
2104*11042SErik.Nordmark@Sun.COM 	if (connp->conn_policy != NULL) {
21054691Skcpoon 		IPPH_REFRELE(connp->conn_policy, connp->conn_netstack);
2106*11042SErik.Nordmark@Sun.COM 		connp->conn_policy = NULL;
2107*11042SErik.Nordmark@Sun.COM 	}
2108*11042SErik.Nordmark@Sun.COM 	if (connp->conn_ipsec_opt_mp != NULL) {
21094691Skcpoon 		freemsg(connp->conn_ipsec_opt_mp);
2110*11042SErik.Nordmark@Sun.COM 		connp->conn_ipsec_opt_mp = NULL;
2111*11042SErik.Nordmark@Sun.COM 	}
21124691Skcpoon 	netstack_rele(connp->conn_netstack);
2113*11042SErik.Nordmark@Sun.COM 	connp->conn_netstack = NULL;
2114*11042SErik.Nordmark@Sun.COM 
2115*11042SErik.Nordmark@Sun.COM 	/* Leave conn_ixa and other constructed fields in place */
2116*11042SErik.Nordmark@Sun.COM 	ipcl_conn_cleanup(connp);
21174691Skcpoon }
2118