xref: /titanic_52/usr/src/uts/common/inet/sctp/sctp.c (revision c2cb63342c63c60cee771d1af82f377e34aa1217)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <sys/types.h>
30 #include <sys/stream.h>
31 #include <sys/strsubr.h>
32 #include <sys/stropts.h>
33 #include <sys/strsun.h>
34 #define	_SUN_TPI_VERSION 2
35 #include <sys/tihdr.h>
36 #include <sys/ddi.h>
37 #include <sys/sunddi.h>
38 #include <sys/xti_inet.h>
39 #include <sys/cmn_err.h>
40 #include <sys/debug.h>
41 #include <sys/vtrace.h>
42 #include <sys/kmem.h>
43 #include <sys/cpuvar.h>
44 #include <sys/random.h>
45 #include <sys/priv.h>
46 #include <sys/sunldi.h>
47 
48 #include <sys/errno.h>
49 #include <sys/signal.h>
50 #include <sys/socket.h>
51 #include <sys/isa_defs.h>
52 #include <netinet/in.h>
53 #include <netinet/tcp.h>
54 #include <netinet/ip6.h>
55 #include <netinet/icmp6.h>
56 #include <netinet/sctp.h>
57 #include <net/if.h>
58 
59 #include <inet/common.h>
60 #include <inet/ip.h>
61 #include <inet/ip6.h>
62 #include <inet/mi.h>
63 #include <inet/mib2.h>
64 #include <inet/kstatcom.h>
65 #include <inet/nd.h>
66 #include <inet/optcom.h>
67 #include <inet/ipclassifier.h>
68 #include <inet/ipsec_impl.h>
69 #include <inet/sctp_ip.h>
70 #include <inet/sctp_crc32.h>
71 
72 #include "sctp_impl.h"
73 #include "sctp_addr.h"
74 #include "sctp_asconf.h"
75 
76 extern major_t SCTP6_MAJ;
77 extern major_t SCTP_MAJ;
78 
79 int sctpdebug;
80 sin6_t	sctp_sin6_null;	/* Zero address for quick clears */
81 
82 /*
83  * Have to ensure that sctp_g_q_close is not done by an
84  * interrupt thread.
85  */
86 static taskq_t *sctp_taskq;
87 
88 static void	sctp_closei_local(sctp_t *sctp);
89 static int	sctp_init_values(sctp_t *, sctp_t *, int);
90 static void	sctp_icmp_error_ipv6(sctp_t *sctp, mblk_t *mp);
91 static void	sctp_process_recvq(void *);
92 static void	sctp_rq_tq_init(sctp_stack_t *);
93 static void	sctp_rq_tq_fini(sctp_stack_t *);
94 static void	sctp_conn_cache_init();
95 static void	sctp_conn_cache_fini();
96 static int	sctp_conn_cache_constructor();
97 static void	sctp_conn_cache_destructor();
98 void		sctp_g_q_setup(sctp_stack_t *);
99 void		sctp_g_q_create(sctp_stack_t *);
100 void		sctp_g_q_destroy(sctp_stack_t *);
101 
102 static void	*sctp_stack_init(netstackid_t stackid, netstack_t *ns);
103 static void	sctp_stack_shutdown(netstackid_t stackid, void *arg);
104 static void	sctp_stack_fini(netstackid_t stackid, void *arg);
105 
106 /*
107  * SCTP receive queue taskq
108  *
109  * At SCTP initialization time, a default taskq is created for
110  * servicing packets received when the interrupt thread cannot
111  * get a hold on the sctp_t.  The number of taskq can be increased in
112  * sctp_find_next_tq() when an existing taskq cannot be dispatched.
113  * The taskqs are never removed.  But the max number of taskq which
114  * can be created is controlled by sctp_recvq_tq_list_max_sz.  Note
115  * that SCTP recvq taskq is not tied to any specific CPU or ill.
116  *
117  * Those taskqs are stored in an array recvq_tq_list.  And they are
118  * used in a round robin fashion.  The current taskq being used is
119  * determined by recvq_tq_list_cur.
120  */
121 
122 /* /etc/system variables */
123 /* The minimum number of threads for each taskq. */
124 int		sctp_recvq_tq_thr_min = 4;
125 /* The maximum number of threads for each taskq. */
126 int		sctp_recvq_tq_thr_max = 16;
127 /* The minimum number of tasks for each taskq. */
128 int		sctp_recvq_tq_task_min = 5;
129 /* The maxiimum number of tasks for each taskq. */
130 int		sctp_recvq_tq_task_max = 50;
131 
132 /*  sctp_t/conn_t kmem cache */
133 struct kmem_cache	*sctp_conn_cache;
134 
135 #define	SCTP_CONDEMNED(sctp)				\
136 	mutex_enter(&(sctp)->sctp_reflock);		\
137 	((sctp)->sctp_condemned = B_TRUE);		\
138 	mutex_exit(&(sctp)->sctp_reflock);
139 
140 /* Link/unlink a sctp_t to/from the global list. */
141 #define	SCTP_LINK(sctp, sctps)				\
142 	mutex_enter(&(sctps)->sctps_g_lock);		\
143 	list_insert_tail(&sctps->sctps_g_list, (sctp));	\
144 	mutex_exit(&(sctps)->sctps_g_lock);
145 
146 #define	SCTP_UNLINK(sctp, sctps)				\
147 	mutex_enter(&(sctps)->sctps_g_lock);		\
148 	ASSERT((sctp)->sctp_condemned);			\
149 	list_remove(&(sctps)->sctps_g_list, (sctp));	\
150 	mutex_exit(&(sctps)->sctps_g_lock);
151 
152 /*
153  * Hooks for Sun Cluster. On non-clustered nodes these will remain NULL.
154  * PSARC/2005/602.
155  */
156 void (*cl_sctp_listen)(sa_family_t, uchar_t *, uint_t, in_port_t) = NULL;
157 void (*cl_sctp_unlisten)(sa_family_t, uchar_t *, uint_t, in_port_t) = NULL;
158 void (*cl_sctp_connect)(sa_family_t, uchar_t *, uint_t, in_port_t,
159     uchar_t *, uint_t, in_port_t, boolean_t, cl_sctp_handle_t) = NULL;
160 void (*cl_sctp_disconnect)(sa_family_t, cl_sctp_handle_t) = NULL;
161 void (*cl_sctp_assoc_change)(sa_family_t, uchar_t *, size_t, uint_t,
162     uchar_t *, size_t, uint_t, int, cl_sctp_handle_t) = NULL;
163 void (*cl_sctp_check_addrs)(sa_family_t, in_port_t, uchar_t **, size_t,
164     uint_t *, boolean_t) = NULL;
165 /*
166  * Return the version number of the SCTP kernel interface.
167  */
168 int
169 sctp_itf_ver(int cl_ver)
170 {
171 	if (cl_ver != SCTP_ITF_VER)
172 		return (-1);
173 	return (SCTP_ITF_VER);
174 }
175 
176 /*
177  * Called when we need a new sctp instantiation but don't really have a
178  * new q to hang it off of. Copy the priv flag from the passed in structure.
179  */
180 sctp_t *
181 sctp_create_eager(sctp_t *psctp)
182 {
183 	sctp_t	*sctp;
184 	mblk_t	*ack_mp, *hb_mp;
185 	conn_t	*connp, *pconnp;
186 	cred_t *credp;
187 	sctp_stack_t	*sctps = psctp->sctp_sctps;
188 
189 	if ((connp = ipcl_conn_create(IPCL_SCTPCONN, KM_NOSLEEP,
190 		    sctps->sctps_netstack)) == NULL) {
191 		return (NULL);
192 	}
193 
194 	connp->conn_ulp_labeled = is_system_labeled();
195 
196 	sctp = CONN2SCTP(connp);
197 	sctp->sctp_sctps = sctps;
198 
199 	if ((ack_mp = sctp_timer_alloc(sctp, sctp_ack_timer)) == NULL ||
200 	    (hb_mp = sctp_timer_alloc(sctp, sctp_heartbeat_timer)) == NULL) {
201 		if (ack_mp != NULL)
202 			freeb(ack_mp);
203 		netstack_rele(sctps->sctps_netstack);
204 		connp->conn_netstack = NULL;
205 		sctp->sctp_sctps = NULL;
206 		SCTP_G_Q_REFRELE(sctps);
207 		kmem_cache_free(sctp_conn_cache, connp);
208 		return (NULL);
209 	}
210 
211 	sctp->sctp_ack_mp = ack_mp;
212 	sctp->sctp_heartbeat_mp = hb_mp;
213 
214 	/* Inherit information from the "parent" */
215 	sctp->sctp_ipversion = psctp->sctp_ipversion;
216 	sctp->sctp_family = psctp->sctp_family;
217 	pconnp = psctp->sctp_connp;
218 	connp->conn_af_isv6 = pconnp->conn_af_isv6;
219 	connp->conn_pkt_isv6 = pconnp->conn_pkt_isv6;
220 	connp->conn_ipv6_v6only = pconnp->conn_ipv6_v6only;
221 	if (sctp_init_values(sctp, psctp, KM_NOSLEEP) != 0) {
222 		freeb(ack_mp);
223 		freeb(hb_mp);
224 		netstack_rele(sctps->sctps_netstack);
225 		connp->conn_netstack = NULL;
226 		sctp->sctp_sctps = NULL;
227 		SCTP_G_Q_REFRELE(sctps);
228 		kmem_cache_free(sctp_conn_cache, connp);
229 		return (NULL);
230 	}
231 
232 	/*
233 	 * If the parent is multilevel, then we'll fix up the remote cred
234 	 * when we do sctp_accept_comm.
235 	 */
236 	if ((credp = pconnp->conn_cred) != NULL) {
237 		connp->conn_cred = credp;
238 		crhold(credp);
239 		/*
240 		 * If the caller has the process-wide flag set, then default to
241 		 * MAC exempt mode.  This allows read-down to unlabeled hosts.
242 		 */
243 		if (getpflags(NET_MAC_AWARE, credp) != 0)
244 			connp->conn_mac_exempt = B_TRUE;
245 	}
246 
247 	connp->conn_allzones = pconnp->conn_allzones;
248 	connp->conn_zoneid = pconnp->conn_zoneid;
249 
250 	sctp->sctp_mss = psctp->sctp_mss;
251 	sctp->sctp_detached = B_TRUE;
252 	/*
253 	 * Link to the global as soon as possible so that this sctp_t
254 	 * can be found.
255 	 */
256 	SCTP_LINK(sctp, sctps);
257 
258 	return (sctp);
259 }
260 
261 /*
262  * We are dying for some reason.  Try to do it gracefully.
263  */
264 void
265 sctp_clean_death(sctp_t *sctp, int err)
266 {
267 	ASSERT(sctp != NULL);
268 	ASSERT((sctp->sctp_family == AF_INET &&
269 	    sctp->sctp_ipversion == IPV4_VERSION) ||
270 	    (sctp->sctp_family == AF_INET6 &&
271 	    (sctp->sctp_ipversion == IPV4_VERSION ||
272 	    sctp->sctp_ipversion == IPV6_VERSION)));
273 
274 	dprint(3, ("sctp_clean_death %p, state %d\n", (void *)sctp,
275 	    sctp->sctp_state));
276 
277 	sctp->sctp_client_errno = err;
278 	/*
279 	 * Check to see if we need to notify upper layer.
280 	 */
281 	if ((sctp->sctp_state >= SCTPS_COOKIE_WAIT) &&
282 	    !SCTP_IS_DETACHED(sctp)) {
283 		if (sctp->sctp_xmit_head || sctp->sctp_xmit_unsent) {
284 			sctp_regift_xmitlist(sctp);
285 		}
286 		if (sctp->sctp_ulp_disconnected(sctp->sctp_ulpd, err)) {
287 			/*
288 			 * Socket is gone, detach.
289 			 */
290 			sctp->sctp_detached = B_TRUE;
291 			sctp->sctp_ulpd = NULL;
292 			bzero(&sctp->sctp_upcalls, sizeof (sctp_upcalls_t));
293 		}
294 	}
295 
296 	/* Remove this sctp from all hashes. */
297 	sctp_closei_local(sctp);
298 
299 	/*
300 	 * If the sctp_t is detached, we need to finish freeing up
301 	 * the resources.  At this point, ip_fanout_sctp() should have
302 	 * a hold on this sctp_t.  Some thread doing snmp stuff can
303 	 * have a hold.  And a taskq can also have a hold waiting to
304 	 * work.  sctp_unlink() the sctp_t from the global list so
305 	 * that no new thread can find it.  Then do a SCTP_REFRELE().
306 	 * The sctp_t will be freed after all those threads are done.
307 	 */
308 	if (SCTP_IS_DETACHED(sctp)) {
309 		SCTP_CONDEMNED(sctp);
310 		SCTP_REFRELE(sctp);
311 	}
312 }
313 
314 /*
315  * Called by upper layer when it wants to close this association.
316  * Depending on the state of this assoication, we need to do
317  * different things.
318  *
319  * If the state is below COOKIE_ECHOED or it is COOKIE_ECHOED but with
320  * no sent data, just remove this sctp from all the hashes.  This
321  * makes sure that all packets from the other end will go to the default
322  * sctp handling.  The upper layer will then do a sctp_close() to clean
323  * up.
324  *
325  * Otherwise, check and see if SO_LINGER is set.  If it is set, check
326  * the value.  If the value is 0, consider this an abortive close.  Send
327  * an ABORT message and kill the associatiion.
328  *
329  */
330 int
331 sctp_disconnect(sctp_t *sctp)
332 {
333 	int	error = 0;
334 
335 	dprint(3, ("sctp_disconnect %p, state %d\n", (void *)sctp,
336 	    sctp->sctp_state));
337 
338 	RUN_SCTP(sctp);
339 
340 	switch (sctp->sctp_state) {
341 	case SCTPS_IDLE:
342 	case SCTPS_BOUND:
343 	case SCTPS_LISTEN:
344 		break;
345 	case SCTPS_COOKIE_WAIT:
346 	case SCTPS_COOKIE_ECHOED:
347 		/*
348 		 * Close during the connect 3-way handshake
349 		 * but here there may or may not be pending data
350 		 * already on queue. Process almost same as in
351 		 * the ESTABLISHED state.
352 		 */
353 		if (sctp->sctp_xmit_head == NULL &&
354 		    sctp->sctp_xmit_unsent == NULL) {
355 			break;
356 		}
357 		/* FALLTHRU */
358 	default:
359 		/*
360 		 * If SO_LINGER has set a zero linger time, abort the
361 		 * connection with a reset.
362 		 */
363 		if (sctp->sctp_linger && sctp->sctp_lingertime == 0) {
364 			sctp_user_abort(sctp, NULL, B_FALSE);
365 			break;
366 		}
367 
368 		/*
369 		 * In there is unread data, send an ABORT
370 		 */
371 		if (sctp->sctp_rxqueued > 0 || sctp->sctp_irwnd >
372 		    sctp->sctp_rwnd) {
373 			sctp_user_abort(sctp, NULL, B_FALSE);
374 			break;
375 		}
376 		/*
377 		 * Transmit the shutdown before detaching the sctp_t.
378 		 * After sctp_detach returns this queue/perimeter
379 		 * no longer owns the sctp_t thus others can modify it.
380 		 */
381 		sctp_send_shutdown(sctp, 0);
382 
383 		/* Pass gathered wisdom to IP for keeping */
384 		sctp_update_ire(sctp);
385 
386 		/*
387 		 * If lingering on close then wait until the shutdown
388 		 * is complete, or the SO_LINGER time passes, or an
389 		 * ABORT is sent/received.  Note that sctp_disconnect()
390 		 * can be called more than once.  Make sure that only
391 		 * one thread waits.
392 		 */
393 		if (sctp->sctp_linger && sctp->sctp_lingertime > 0 &&
394 		    sctp->sctp_state >= SCTPS_ESTABLISHED &&
395 		    !sctp->sctp_lingering) {
396 			clock_t stoptime;	/* in ticks */
397 			clock_t ret;
398 
399 			/*
400 			 * Process the sendq to send the SHUTDOWN out
401 			 * before waiting.
402 			 */
403 			sctp_process_sendq(sctp);
404 
405 			sctp->sctp_lingering = 1;
406 			sctp->sctp_client_errno = 0;
407 			stoptime = lbolt + sctp->sctp_lingertime;
408 
409 			mutex_enter(&sctp->sctp_lock);
410 			sctp->sctp_running = B_FALSE;
411 			while (sctp->sctp_state >= SCTPS_ESTABLISHED &&
412 			    sctp->sctp_client_errno == 0) {
413 				cv_broadcast(&sctp->sctp_cv);
414 				ret = cv_timedwait_sig(&sctp->sctp_cv,
415 				    &sctp->sctp_lock, stoptime);
416 				if (ret < 0) {
417 					/* Stoptime has reached. */
418 					sctp->sctp_client_errno = EWOULDBLOCK;
419 					break;
420 				} else if (ret == 0) {
421 					/* Got a signal. */
422 					break;
423 				}
424 			}
425 			error = sctp->sctp_client_errno;
426 			sctp->sctp_client_errno = 0;
427 			mutex_exit(&sctp->sctp_lock);
428 		}
429 
430 		WAKE_SCTP(sctp);
431 		sctp_process_sendq(sctp);
432 		return (error);
433 	}
434 
435 
436 	/* Remove this sctp from all hashes so nobody can find it. */
437 	sctp_closei_local(sctp);
438 	WAKE_SCTP(sctp);
439 	return (error);
440 }
441 
442 void
443 sctp_close(sctp_t *sctp)
444 {
445 	dprint(3, ("sctp_close %p, state %d\n", (void *)sctp,
446 	    sctp->sctp_state));
447 
448 	RUN_SCTP(sctp);
449 	sctp->sctp_detached = 1;
450 	sctp->sctp_ulpd = NULL;
451 	bzero(&sctp->sctp_upcalls, sizeof (sctp_upcalls_t));
452 	bzero(&sctp->sctp_events, sizeof (sctp->sctp_events));
453 
454 	/* If the graceful shutdown has not been completed, just return. */
455 	if (sctp->sctp_state != SCTPS_IDLE) {
456 		WAKE_SCTP(sctp);
457 		return;
458 	}
459 
460 	/*
461 	 * Since sctp_t is in SCTPS_IDLE state, so the only thread which
462 	 * can have a hold on the sctp_t is doing snmp stuff.  Just do
463 	 * a SCTP_REFRELE() here after the SCTP_UNLINK().  It will
464 	 * be freed when the other thread is done.
465 	 */
466 	SCTP_CONDEMNED(sctp);
467 	WAKE_SCTP(sctp);
468 	SCTP_REFRELE(sctp);
469 }
470 
471 /*
472  * Unlink from global list and do the eager close.
473  * Remove the refhold implicit in being on the global list.
474  */
475 void
476 sctp_close_eager(sctp_t *sctp)
477 {
478 	SCTP_CONDEMNED(sctp);
479 	sctp_closei_local(sctp);
480 	SCTP_REFRELE(sctp);
481 }
482 
483 /*
484  * The sctp_t is going away. Remove it from all lists and set it
485  * to SCTPS_IDLE. The caller has to remove it from the
486  * global list. The freeing up of memory is deferred until
487  * sctp_free(). This is needed since a thread in sctp_input() might have
488  * done a SCTP_REFHOLD on this structure before it was removed from the
489  * hashes.
490  */
491 static void
492 sctp_closei_local(sctp_t *sctp)
493 {
494 	mblk_t	*mp;
495 	ire_t	*ire = NULL;
496 	conn_t	*connp = sctp->sctp_connp;
497 
498 	/* Stop and free the timers */
499 	sctp_free_faddr_timers(sctp);
500 	if ((mp = sctp->sctp_heartbeat_mp) != NULL) {
501 		sctp_timer_free(mp);
502 		sctp->sctp_heartbeat_mp = NULL;
503 	}
504 	if ((mp = sctp->sctp_ack_mp) != NULL) {
505 		sctp_timer_free(mp);
506 		sctp->sctp_ack_mp = NULL;
507 	}
508 
509 	/* Set the CONN_CLOSING flag so that IP will not cache IRE again. */
510 	mutex_enter(&connp->conn_lock);
511 	connp->conn_state_flags |= CONN_CLOSING;
512 	ire = connp->conn_ire_cache;
513 	connp->conn_ire_cache = NULL;
514 	mutex_exit(&connp->conn_lock);
515 	if (ire != NULL)
516 		IRE_REFRELE_NOTR(ire);
517 
518 	/* Remove from all hashes. */
519 	sctp_bind_hash_remove(sctp);
520 	sctp_conn_hash_remove(sctp);
521 	sctp_listen_hash_remove(sctp);
522 	sctp->sctp_state = SCTPS_IDLE;
523 
524 	/*
525 	 * Clean up the recvq as much as possible.  All those packets
526 	 * will be silently dropped as this sctp_t is now in idle state.
527 	 */
528 	mutex_enter(&sctp->sctp_recvq_lock);
529 	while ((mp = sctp->sctp_recvq) != NULL) {
530 		mblk_t *ipsec_mp;
531 
532 		sctp->sctp_recvq = mp->b_next;
533 		mp->b_next = NULL;
534 		if ((ipsec_mp = mp->b_prev) != NULL) {
535 			freeb(ipsec_mp);
536 			mp->b_prev = NULL;
537 		}
538 		freemsg(mp);
539 	}
540 	mutex_exit(&sctp->sctp_recvq_lock);
541 }
542 
543 /*
544  * Free memory associated with the sctp/ip header template.
545  */
546 static void
547 sctp_headers_free(sctp_t *sctp)
548 {
549 	if (sctp->sctp_iphc != NULL) {
550 		kmem_free(sctp->sctp_iphc, sctp->sctp_iphc_len);
551 		sctp->sctp_iphc = NULL;
552 		sctp->sctp_ipha = NULL;
553 		sctp->sctp_hdr_len = 0;
554 		sctp->sctp_ip_hdr_len = 0;
555 		sctp->sctp_iphc_len = 0;
556 		sctp->sctp_sctph = NULL;
557 		sctp->sctp_hdr_len = 0;
558 	}
559 	if (sctp->sctp_iphc6 != NULL) {
560 		kmem_free(sctp->sctp_iphc6, sctp->sctp_iphc6_len);
561 		sctp->sctp_iphc6 = NULL;
562 		sctp->sctp_ip6h = NULL;
563 		sctp->sctp_hdr6_len = 0;
564 		sctp->sctp_ip_hdr6_len = 0;
565 		sctp->sctp_iphc6_len = 0;
566 		sctp->sctp_sctph6 = NULL;
567 		sctp->sctp_hdr6_len = 0;
568 	}
569 }
570 
571 static void
572 sctp_free_xmit_data(sctp_t *sctp)
573 {
574 	mblk_t	*ump = NULL;
575 	mblk_t	*nump;
576 	mblk_t	*mp;
577 	mblk_t	*nmp;
578 
579 	sctp->sctp_xmit_unacked = NULL;
580 	ump = sctp->sctp_xmit_head;
581 	sctp->sctp_xmit_tail = sctp->sctp_xmit_head = NULL;
582 free_unsent:
583 	for (; ump != NULL; ump = nump) {
584 		for (mp = ump->b_cont; mp != NULL; mp = nmp) {
585 			nmp = mp->b_next;
586 			mp->b_next = NULL;
587 			mp->b_prev = NULL;
588 			freemsg(mp);
589 		}
590 		ASSERT(DB_REF(ump) == 1);
591 		nump = ump->b_next;
592 		ump->b_next = NULL;
593 		ump->b_prev = NULL;
594 		ump->b_cont = NULL;
595 		freeb(ump);
596 	}
597 	if ((ump = sctp->sctp_xmit_unsent) == NULL) {
598 		ASSERT(sctp->sctp_xmit_unsent_tail == NULL);
599 		return;
600 	}
601 	sctp->sctp_xmit_unsent = sctp->sctp_xmit_unsent_tail = NULL;
602 	goto free_unsent;
603 }
604 
605 /*
606  * Cleanup all the messages in the stream queue and the reassembly lists.
607  * If 'free' is true, then delete the streams as well.
608  */
609 void
610 sctp_instream_cleanup(sctp_t *sctp, boolean_t free)
611 {
612 	int	i;
613 	mblk_t	*mp;
614 	mblk_t	*mp1;
615 
616 	if (sctp->sctp_instr != NULL) {
617 		/* walk thru and flush out anything remaining in the Q */
618 		for (i = 0; i < sctp->sctp_num_istr; i++) {
619 			mp = sctp->sctp_instr[i].istr_msgs;
620 			while (mp != NULL) {
621 				mp1 = mp->b_next;
622 				mp->b_next = mp->b_prev = NULL;
623 				freemsg(mp);
624 				mp = mp1;
625 			}
626 			sctp->sctp_instr[i].istr_msgs = NULL;
627 			sctp_free_reass((sctp->sctp_instr) + i);
628 			sctp->sctp_instr[i].nextseq = 0;
629 		}
630 		if (free) {
631 			kmem_free(sctp->sctp_instr,
632 			    sizeof (*sctp->sctp_instr) * sctp->sctp_num_istr);
633 			sctp->sctp_instr = NULL;
634 			sctp->sctp_num_istr = 0;
635 		}
636 	}
637 	/* un-ordered fragments */
638 	if (sctp->sctp_uo_frags != NULL) {
639 		for (mp = sctp->sctp_uo_frags; mp != NULL; mp = mp1) {
640 			mp1 = mp->b_next;
641 			mp->b_next = mp->b_prev = NULL;
642 			freemsg(mp);
643 		}
644 	}
645 }
646 
647 /*
648  * Last reference to the sctp_t is gone. Free all memory associated with it.
649  * Called from SCTP_REFRELE. Called inline in sctp_close()
650  */
651 void
652 sctp_free(conn_t *connp)
653 {
654 	sctp_t *sctp = CONN2SCTP(connp);
655 	int		cnt;
656 	sctp_stack_t	*sctps = sctp->sctp_sctps;
657 	netstack_t	*ns;
658 
659 	ASSERT(sctps != NULL);
660 	/* Unlink it from the global list */
661 	SCTP_UNLINK(sctp, sctps);
662 
663 	ASSERT(connp->conn_ref == 0);
664 	ASSERT(connp->conn_ulp == IPPROTO_SCTP);
665 	ASSERT(!MUTEX_HELD(&sctp->sctp_reflock));
666 	ASSERT(sctp->sctp_refcnt == 0);
667 
668 	ASSERT(sctp->sctp_ptpbhn == NULL && sctp->sctp_bind_hash == NULL);
669 	ASSERT(sctp->sctp_conn_hash_next == NULL &&
670 	    sctp->sctp_conn_hash_prev == NULL);
671 
672 
673 	/* Free up all the resources. */
674 
675 	/* blow away sctp stream management */
676 	if (sctp->sctp_ostrcntrs != NULL) {
677 		kmem_free(sctp->sctp_ostrcntrs,
678 		    sizeof (uint16_t) * sctp->sctp_num_ostr);
679 		sctp->sctp_ostrcntrs = NULL;
680 	}
681 	sctp_instream_cleanup(sctp, B_TRUE);
682 
683 	/* Remove all data transfer resources. */
684 	sctp->sctp_istr_nmsgs = 0;
685 	sctp->sctp_rxqueued = 0;
686 	sctp_free_xmit_data(sctp);
687 	sctp->sctp_unacked = 0;
688 	sctp->sctp_unsent = 0;
689 	if (sctp->sctp_cxmit_list != NULL)
690 		sctp_asconf_free_cxmit(sctp, NULL);
691 
692 	sctp->sctp_lastdata = NULL;
693 
694 	/* Clear out default xmit settings */
695 	sctp->sctp_def_stream = 0;
696 	sctp->sctp_def_flags = 0;
697 	sctp->sctp_def_ppid = 0;
698 	sctp->sctp_def_context = 0;
699 	sctp->sctp_def_timetolive = 0;
700 
701 	if (sctp->sctp_sack_info != NULL) {
702 		sctp_free_set(sctp->sctp_sack_info);
703 		sctp->sctp_sack_info = NULL;
704 	}
705 	sctp->sctp_sack_gaps = 0;
706 
707 	if (sctp->sctp_cookie_mp != NULL) {
708 		freemsg(sctp->sctp_cookie_mp);
709 		sctp->sctp_cookie_mp = NULL;
710 	}
711 
712 	/* Remove all the address resources. */
713 	sctp_zap_addrs(sctp);
714 	for (cnt = 0; cnt < SCTP_IPIF_HASH; cnt++) {
715 		ASSERT(sctp->sctp_saddrs[cnt].ipif_count == 0);
716 		list_destroy(&sctp->sctp_saddrs[cnt].sctp_ipif_list);
717 	}
718 
719 	ip6_pkt_free(&sctp->sctp_sticky_ipp);
720 
721 	if (sctp->sctp_hopopts != NULL) {
722 		mi_free(sctp->sctp_hopopts);
723 		sctp->sctp_hopopts = NULL;
724 		sctp->sctp_hopoptslen = 0;
725 	}
726 	ASSERT(sctp->sctp_hopoptslen == 0);
727 	if (sctp->sctp_dstopts != NULL) {
728 		mi_free(sctp->sctp_dstopts);
729 		sctp->sctp_dstopts = NULL;
730 		sctp->sctp_dstoptslen = 0;
731 	}
732 	ASSERT(sctp->sctp_dstoptslen == 0);
733 	if (sctp->sctp_rtdstopts != NULL) {
734 		mi_free(sctp->sctp_rtdstopts);
735 		sctp->sctp_rtdstopts = NULL;
736 		sctp->sctp_rtdstoptslen = 0;
737 	}
738 	ASSERT(sctp->sctp_rtdstoptslen == 0);
739 	if (sctp->sctp_rthdr != NULL) {
740 		mi_free(sctp->sctp_rthdr);
741 		sctp->sctp_rthdr = NULL;
742 		sctp->sctp_rthdrlen = 0;
743 	}
744 	ASSERT(sctp->sctp_rthdrlen == 0);
745 	sctp_headers_free(sctp);
746 
747 	sctp->sctp_shutdown_faddr = NULL;
748 
749 	/* Clear all the bitfields. */
750 	bzero(&sctp->sctp_bits, sizeof (sctp->sctp_bits));
751 
752 	/* It is time to update the global statistics. */
753 	UPDATE_MIB(&sctps->sctps_mib, sctpOutSCTPPkts, sctp->sctp_opkts);
754 	UPDATE_MIB(&sctps->sctps_mib, sctpOutCtrlChunks, sctp->sctp_obchunks);
755 	UPDATE_MIB(&sctps->sctps_mib, sctpOutOrderChunks, sctp->sctp_odchunks);
756 	UPDATE_MIB(&sctps->sctps_mib,
757 	    sctpOutUnorderChunks, sctp->sctp_oudchunks);
758 	UPDATE_MIB(&sctps->sctps_mib, sctpRetransChunks, sctp->sctp_rxtchunks);
759 	UPDATE_MIB(&sctps->sctps_mib, sctpInSCTPPkts, sctp->sctp_ipkts);
760 	UPDATE_MIB(&sctps->sctps_mib, sctpInCtrlChunks, sctp->sctp_ibchunks);
761 	UPDATE_MIB(&sctps->sctps_mib, sctpInOrderChunks, sctp->sctp_idchunks);
762 	UPDATE_MIB(&sctps->sctps_mib,
763 	    sctpInUnorderChunks, sctp->sctp_iudchunks);
764 	UPDATE_MIB(&sctps->sctps_mib, sctpFragUsrMsgs, sctp->sctp_fragdmsgs);
765 	UPDATE_MIB(&sctps->sctps_mib, sctpReasmUsrMsgs, sctp->sctp_reassmsgs);
766 	sctp->sctp_opkts = 0;
767 	sctp->sctp_obchunks = 0;
768 	sctp->sctp_odchunks = 0;
769 	sctp->sctp_oudchunks = 0;
770 	sctp->sctp_rxtchunks = 0;
771 	sctp->sctp_ipkts = 0;
772 	sctp->sctp_ibchunks = 0;
773 	sctp->sctp_idchunks = 0;
774 	sctp->sctp_iudchunks = 0;
775 	sctp->sctp_fragdmsgs = 0;
776 	sctp->sctp_reassmsgs = 0;
777 
778 	sctp->sctp_autoclose = 0;
779 	sctp->sctp_tx_adaption_code = 0;
780 
781 	sctp->sctp_v6label_len = 0;
782 	sctp->sctp_v4label_len = 0;
783 
784 	/* Clean up conn_t stuff */
785 	connp->conn_policy_cached = B_FALSE;
786 	if (connp->conn_latch != NULL) {
787 		IPLATCH_REFRELE(connp->conn_latch, connp->conn_netstack);
788 		connp->conn_latch = NULL;
789 	}
790 	if (connp->conn_policy != NULL) {
791 		IPPH_REFRELE(connp->conn_policy, connp->conn_netstack);
792 		connp->conn_policy = NULL;
793 	}
794 	if (connp->conn_ipsec_opt_mp != NULL) {
795 		freemsg(connp->conn_ipsec_opt_mp);
796 		connp->conn_ipsec_opt_mp = NULL;
797 	}
798 	if (connp->conn_cred != NULL) {
799 		crfree(connp->conn_cred);
800 		connp->conn_cred = NULL;
801 	}
802 
803 	/* Every sctp_t holds one reference on the default queue */
804 	sctp->sctp_sctps = NULL;
805 	SCTP_G_Q_REFRELE(sctps);
806 
807 	ns = connp->conn_netstack;
808 	connp->conn_netstack = NULL;
809 	netstack_rele(ns);
810 	kmem_cache_free(sctp_conn_cache, connp);
811 }
812 
813 /* Diagnostic routine used to return a string associated with the sctp state. */
814 char *
815 sctp_display(sctp_t *sctp, char *sup_buf)
816 {
817 	char	*buf;
818 	char	buf1[30];
819 	static char	priv_buf[INET6_ADDRSTRLEN * 2 + 80];
820 	char	*cp;
821 
822 	if (sctp == NULL)
823 		return ("NULL_SCTP");
824 
825 	buf = (sup_buf != NULL) ? sup_buf : priv_buf;
826 
827 	switch (sctp->sctp_state) {
828 	case SCTPS_IDLE:
829 		cp = "SCTP_IDLE";
830 		break;
831 	case SCTPS_BOUND:
832 		cp = "SCTP_BOUND";
833 		break;
834 	case SCTPS_LISTEN:
835 		cp = "SCTP_LISTEN";
836 		break;
837 	case SCTPS_COOKIE_WAIT:
838 		cp = "SCTP_COOKIE_WAIT";
839 		break;
840 	case SCTPS_COOKIE_ECHOED:
841 		cp = "SCTP_COOKIE_ECHOED";
842 		break;
843 	case SCTPS_ESTABLISHED:
844 		cp = "SCTP_ESTABLISHED";
845 		break;
846 	case SCTPS_SHUTDOWN_PENDING:
847 		cp = "SCTP_SHUTDOWN_PENDING";
848 		break;
849 	case SCTPS_SHUTDOWN_SENT:
850 		cp = "SCTPS_SHUTDOWN_SENT";
851 		break;
852 	case SCTPS_SHUTDOWN_RECEIVED:
853 		cp = "SCTPS_SHUTDOWN_RECEIVED";
854 		break;
855 	case SCTPS_SHUTDOWN_ACK_SENT:
856 		cp = "SCTPS_SHUTDOWN_ACK_SENT";
857 		break;
858 	default:
859 		(void) mi_sprintf(buf1, "SCTPUnkState(%d)", sctp->sctp_state);
860 		cp = buf1;
861 		break;
862 	}
863 	(void) mi_sprintf(buf, "[%u, %u] %s",
864 	    ntohs(sctp->sctp_lport), ntohs(sctp->sctp_fport), cp);
865 
866 	return (buf);
867 }
868 
869 /*
870  * Initialize protocol control block. If a parent exists, inherit
871  * all values set through setsockopt().
872  */
873 static int
874 sctp_init_values(sctp_t *sctp, sctp_t *psctp, int sleep)
875 {
876 	int	err;
877 	int	cnt;
878 	sctp_stack_t	*sctps = sctp->sctp_sctps;
879 	conn_t 	*connp, *pconnp;
880 
881 	ASSERT((sctp->sctp_family == AF_INET &&
882 	    sctp->sctp_ipversion == IPV4_VERSION) ||
883 	    (sctp->sctp_family == AF_INET6 &&
884 	    (sctp->sctp_ipversion == IPV4_VERSION ||
885 	    sctp->sctp_ipversion == IPV6_VERSION)));
886 
887 	sctp->sctp_nsaddrs = 0;
888 	for (cnt = 0; cnt < SCTP_IPIF_HASH; cnt++) {
889 		sctp->sctp_saddrs[cnt].ipif_count = 0;
890 		list_create(&sctp->sctp_saddrs[cnt].sctp_ipif_list,
891 		    sizeof (sctp_saddr_ipif_t), offsetof(sctp_saddr_ipif_t,
892 		    saddr_ipif));
893 	}
894 	sctp->sctp_ports = 0;
895 	sctp->sctp_running = B_FALSE;
896 	sctp->sctp_state = SCTPS_IDLE;
897 
898 	sctp->sctp_refcnt = 1;
899 
900 	sctp->sctp_strikes = 0;
901 
902 	sctp->sctp_last_mtu_probe = lbolt64;
903 	sctp->sctp_mtu_probe_intvl = sctps->sctps_mtu_probe_interval;
904 
905 	sctp->sctp_sack_gaps = 0;
906 	sctp->sctp_sack_toggle = 2;
907 
908 	if (psctp != NULL) {
909 		/*
910 		 * Inherit from parent
911 		 */
912 		sctp->sctp_iphc = kmem_zalloc(psctp->sctp_iphc_len,
913 		    KM_NOSLEEP);
914 		if (sctp->sctp_iphc == NULL)
915 			return (ENOMEM);
916 		sctp->sctp_iphc_len = psctp->sctp_iphc_len;
917 		sctp->sctp_hdr_len = psctp->sctp_hdr_len;
918 
919 		sctp->sctp_iphc6 = kmem_zalloc(psctp->sctp_iphc6_len,
920 		    KM_NOSLEEP);
921 		if (sctp->sctp_iphc6 == NULL) {
922 			sctp->sctp_iphc6_len = 0;
923 			return (ENOMEM);
924 		}
925 		sctp->sctp_iphc6_len = psctp->sctp_iphc6_len;
926 		sctp->sctp_hdr6_len = psctp->sctp_hdr6_len;
927 
928 		sctp->sctp_ip_hdr_len = psctp->sctp_ip_hdr_len;
929 		sctp->sctp_ip_hdr6_len = psctp->sctp_ip_hdr6_len;
930 
931 		/*
932 		 * Copy the IP+SCTP header templates from listener
933 		 */
934 		bcopy(psctp->sctp_iphc, sctp->sctp_iphc,
935 		    psctp->sctp_hdr_len);
936 		sctp->sctp_ipha = (ipha_t *)sctp->sctp_iphc;
937 		sctp->sctp_sctph = (sctp_hdr_t *)(sctp->sctp_iphc +
938 		    sctp->sctp_ip_hdr_len);
939 
940 		bcopy(psctp->sctp_iphc6, sctp->sctp_iphc6,
941 		    psctp->sctp_hdr6_len);
942 		if (((ip6i_t *)(sctp->sctp_iphc6))->ip6i_nxt == IPPROTO_RAW) {
943 			sctp->sctp_ip6h = (ip6_t *)(sctp->sctp_iphc6 +
944 			    sizeof (ip6i_t));
945 		} else {
946 			sctp->sctp_ip6h = (ip6_t *)sctp->sctp_iphc6;
947 		}
948 		sctp->sctp_sctph6 = (sctp_hdr_t *)(sctp->sctp_iphc6 +
949 		    sctp->sctp_ip_hdr6_len);
950 
951 		sctp->sctp_cookie_lifetime = psctp->sctp_cookie_lifetime;
952 		sctp->sctp_xmit_lowater = psctp->sctp_xmit_lowater;
953 		sctp->sctp_xmit_hiwater = psctp->sctp_xmit_hiwater;
954 		sctp->sctp_cwnd_max = psctp->sctp_cwnd_max;
955 		sctp->sctp_rwnd = psctp->sctp_rwnd;
956 		sctp->sctp_irwnd = psctp->sctp_rwnd;
957 		sctp->sctp_pd_point = psctp->sctp_pd_point;
958 		sctp->sctp_rto_max = psctp->sctp_rto_max;
959 		sctp->sctp_init_rto_max = psctp->sctp_init_rto_max;
960 		sctp->sctp_rto_min = psctp->sctp_rto_min;
961 		sctp->sctp_rto_initial = psctp->sctp_rto_initial;
962 		sctp->sctp_pa_max_rxt = psctp->sctp_pa_max_rxt;
963 		sctp->sctp_pp_max_rxt = psctp->sctp_pp_max_rxt;
964 		sctp->sctp_max_init_rxt = psctp->sctp_max_init_rxt;
965 
966 		sctp->sctp_def_stream = psctp->sctp_def_stream;
967 		sctp->sctp_def_flags = psctp->sctp_def_flags;
968 		sctp->sctp_def_ppid = psctp->sctp_def_ppid;
969 		sctp->sctp_def_context = psctp->sctp_def_context;
970 		sctp->sctp_def_timetolive = psctp->sctp_def_timetolive;
971 
972 		sctp->sctp_num_istr = psctp->sctp_num_istr;
973 		sctp->sctp_num_ostr = psctp->sctp_num_ostr;
974 
975 		sctp->sctp_hb_interval = psctp->sctp_hb_interval;
976 		sctp->sctp_autoclose = psctp->sctp_autoclose;
977 		sctp->sctp_tx_adaption_code = psctp->sctp_tx_adaption_code;
978 
979 		/* xxx should be a better way to copy these flags xxx */
980 		sctp->sctp_debug = psctp->sctp_debug;
981 		sctp->sctp_bound_to_all = psctp->sctp_bound_to_all;
982 		sctp->sctp_cansleep = psctp->sctp_cansleep;
983 		sctp->sctp_send_adaption = psctp->sctp_send_adaption;
984 		sctp->sctp_ndelay = psctp->sctp_ndelay;
985 		sctp->sctp_events = psctp->sctp_events;
986 		sctp->sctp_ipv6_recvancillary = psctp->sctp_ipv6_recvancillary;
987 
988 		/* Copy IP-layer options */
989 		connp = sctp->sctp_connp;
990 		pconnp = psctp->sctp_connp;
991 
992 		connp->conn_broadcast = pconnp->conn_broadcast;
993 		connp->conn_loopback = pconnp->conn_loopback;
994 		connp->conn_dontroute = pconnp->conn_dontroute;
995 		connp->conn_reuseaddr = pconnp->conn_reuseaddr;
996 
997 	} else {
998 		/*
999 		 * Initialize the header template
1000 		 */
1001 		if ((err = sctp_header_init_ipv4(sctp, sleep)) != 0) {
1002 			return (err);
1003 		}
1004 		if ((err = sctp_header_init_ipv6(sctp, sleep)) != 0) {
1005 			return (err);
1006 		}
1007 
1008 		/*
1009 		 * Set to system defaults
1010 		 */
1011 		sctp->sctp_cookie_lifetime =
1012 		    MSEC_TO_TICK(sctps->sctps_cookie_life);
1013 		sctp->sctp_xmit_lowater = sctps->sctps_xmit_lowat;
1014 		sctp->sctp_xmit_hiwater = sctps->sctps_xmit_hiwat;
1015 		sctp->sctp_cwnd_max = sctps->sctps_cwnd_max_;
1016 		sctp->sctp_rwnd = sctps->sctps_recv_hiwat;
1017 		sctp->sctp_irwnd = sctp->sctp_rwnd;
1018 		sctp->sctp_pd_point = sctp->sctp_rwnd;
1019 		sctp->sctp_rto_max = MSEC_TO_TICK(sctps->sctps_rto_maxg);
1020 		sctp->sctp_init_rto_max = sctp->sctp_rto_max;
1021 		sctp->sctp_rto_min = MSEC_TO_TICK(sctps->sctps_rto_ming);
1022 		sctp->sctp_rto_initial = MSEC_TO_TICK(
1023 		    sctps->sctps_rto_initialg);
1024 		sctp->sctp_pa_max_rxt = sctps->sctps_pa_max_retr;
1025 		sctp->sctp_pp_max_rxt = sctps->sctps_pp_max_retr;
1026 		sctp->sctp_max_init_rxt = sctps->sctps_max_init_retr;
1027 
1028 		sctp->sctp_num_istr = sctps->sctps_max_in_streams;
1029 		sctp->sctp_num_ostr = sctps->sctps_initial_out_streams;
1030 
1031 		sctp->sctp_hb_interval =
1032 		    MSEC_TO_TICK(sctps->sctps_heartbeat_interval);
1033 	}
1034 	sctp->sctp_understands_asconf = B_TRUE;
1035 	sctp->sctp_understands_addip = B_TRUE;
1036 	sctp->sctp_prsctp_aware = B_FALSE;
1037 
1038 	sctp->sctp_connp->conn_ref = 1;
1039 	sctp->sctp_connp->conn_fully_bound = B_FALSE;
1040 
1041 	sctp->sctp_prsctpdrop = 0;
1042 	sctp->sctp_msgcount = 0;
1043 
1044 	return (0);
1045 }
1046 
1047 /*
1048  * Extracts the init tag from an INIT chunk and checks if it matches
1049  * the sctp's verification tag. Returns 0 if it doesn't match, 1 if
1050  * it does.
1051  */
1052 static boolean_t
1053 sctp_icmp_verf(sctp_t *sctp, sctp_hdr_t *sh, mblk_t *mp)
1054 {
1055 	sctp_chunk_hdr_t *sch;
1056 	uint32_t verf, *vp;
1057 
1058 	sch = (sctp_chunk_hdr_t *)(sh + 1);
1059 	vp = (uint32_t *)(sch + 1);
1060 
1061 	/* Need at least the data chunk hdr and the first 4 bytes of INIT */
1062 	if ((unsigned char *)(vp + 1) > mp->b_wptr) {
1063 		return (B_FALSE);
1064 	}
1065 
1066 	bcopy(vp, &verf, sizeof (verf));
1067 
1068 	if (verf == sctp->sctp_lvtag) {
1069 		return (B_TRUE);
1070 	}
1071 	return (B_FALSE);
1072 }
1073 
1074 /*
1075  * sctp_icmp_error is called by sctp_input() to process ICMP error messages
1076  * passed up by IP.  The queue is the default queue.  We need to find a sctp_t
1077  * that corresponds to the returned datagram.  Passes the message back in on
1078  * the correct queue once it has located the connection.
1079  * Assumes that IP has pulled up everything up to and including
1080  * the ICMP header.
1081  */
1082 void
1083 sctp_icmp_error(sctp_t *sctp, mblk_t *mp)
1084 {
1085 	icmph_t *icmph;
1086 	ipha_t	*ipha;
1087 	int	iph_hdr_length;
1088 	sctp_hdr_t *sctph;
1089 	mblk_t *first_mp;
1090 	uint32_t new_mtu;
1091 	in6_addr_t dst;
1092 	sctp_faddr_t *fp;
1093 	sctp_stack_t	*sctps = sctp->sctp_sctps;
1094 
1095 	dprint(1, ("sctp_icmp_error: sctp=%p, mp=%p\n", (void *)sctp,
1096 	    (void *)mp));
1097 
1098 	first_mp = mp;
1099 
1100 	ipha = (ipha_t *)mp->b_rptr;
1101 	if (IPH_HDR_VERSION(ipha) != IPV4_VERSION) {
1102 		ASSERT(IPH_HDR_VERSION(ipha) == IPV6_VERSION);
1103 		sctp_icmp_error_ipv6(sctp, first_mp);
1104 		return;
1105 	}
1106 
1107 	iph_hdr_length = IPH_HDR_LENGTH(ipha);
1108 	icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length];
1109 	ipha = (ipha_t *)&icmph[1];
1110 	iph_hdr_length = IPH_HDR_LENGTH(ipha);
1111 	sctph = (sctp_hdr_t *)((char *)ipha + iph_hdr_length);
1112 	if ((uchar_t *)(sctph + 1) >= mp->b_wptr) {
1113 		/* not enough data for SCTP header */
1114 		freemsg(first_mp);
1115 		return;
1116 	}
1117 
1118 	switch (icmph->icmph_type) {
1119 	case ICMP_DEST_UNREACHABLE:
1120 		switch (icmph->icmph_code) {
1121 		case ICMP_FRAGMENTATION_NEEDED:
1122 			/*
1123 			 * Reduce the MSS based on the new MTU.  This will
1124 			 * eliminate any fragmentation locally.
1125 			 * N.B.  There may well be some funny side-effects on
1126 			 * the local send policy and the remote receive policy.
1127 			 * Pending further research, we provide
1128 			 * sctp_ignore_path_mtu just in case this proves
1129 			 * disastrous somewhere.
1130 			 *
1131 			 * After updating the MSS, retransmit part of the
1132 			 * dropped segment using the new mss by calling
1133 			 * sctp_wput_slow().  Need to adjust all those
1134 			 * params to make sure sctp_wput_slow() work properly.
1135 			 */
1136 			if (sctps->sctps_ignore_path_mtu)
1137 				break;
1138 
1139 			/* find the offending faddr */
1140 			IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, &dst);
1141 			fp = sctp_lookup_faddr(sctp, &dst);
1142 			if (fp == NULL) {
1143 				break;
1144 			}
1145 
1146 			new_mtu = ntohs(icmph->icmph_du_mtu);
1147 
1148 			if (new_mtu - sctp->sctp_hdr_len >= fp->sfa_pmss)
1149 				break;
1150 
1151 			/*
1152 			 * Make sure that sfa_pmss is a multiple of
1153 			 * SCTP_ALIGN.
1154 			 */
1155 			fp->sfa_pmss = (new_mtu - sctp->sctp_hdr_len) &
1156 				~(SCTP_ALIGN - 1);
1157 			fp->pmtu_discovered = 1;
1158 
1159 			break;
1160 		case ICMP_PORT_UNREACHABLE:
1161 		case ICMP_PROTOCOL_UNREACHABLE:
1162 			switch (sctp->sctp_state) {
1163 			case SCTPS_COOKIE_WAIT:
1164 			case SCTPS_COOKIE_ECHOED:
1165 				/* make sure the verification tag matches */
1166 				if (!sctp_icmp_verf(sctp, sctph, mp)) {
1167 					break;
1168 				}
1169 				BUMP_MIB(&sctps->sctps_mib, sctpAborted);
1170 				sctp_assoc_event(sctp, SCTP_CANT_STR_ASSOC, 0,
1171 				    NULL);
1172 				sctp_clean_death(sctp, ECONNREFUSED);
1173 				break;
1174 			}
1175 			break;
1176 		case ICMP_HOST_UNREACHABLE:
1177 		case ICMP_NET_UNREACHABLE:
1178 			/* Record the error in case we finally time out. */
1179 			sctp->sctp_client_errno = (icmph->icmph_code ==
1180 			    ICMP_HOST_UNREACHABLE) ? EHOSTUNREACH : ENETUNREACH;
1181 			break;
1182 		default:
1183 			break;
1184 		}
1185 		break;
1186 	case ICMP_SOURCE_QUENCH: {
1187 		/* Reduce the sending rate as if we got a retransmit timeout */
1188 		break;
1189 	}
1190 	}
1191 	freemsg(first_mp);
1192 }
1193 
1194 /*
1195  * sctp_icmp_error_ipv6() is called by sctp_icmp_error() to process ICMPv6
1196  * error messages passed up by IP.
1197  * Assumes that IP has pulled up all the extension headers as well
1198  * as the ICMPv6 header.
1199  */
1200 static void
1201 sctp_icmp_error_ipv6(sctp_t *sctp, mblk_t *mp)
1202 {
1203 	icmp6_t *icmp6;
1204 	ip6_t	*ip6h;
1205 	uint16_t	iph_hdr_length;
1206 	sctp_hdr_t *sctpha;
1207 	uint8_t	*nexthdrp;
1208 	uint32_t new_mtu;
1209 	sctp_faddr_t *fp;
1210 	sctp_stack_t	*sctps = sctp->sctp_sctps;
1211 
1212 	ip6h = (ip6_t *)mp->b_rptr;
1213 	iph_hdr_length = (ip6h->ip6_nxt != IPPROTO_SCTP) ?
1214 	    ip_hdr_length_v6(mp, ip6h) : IPV6_HDR_LEN;
1215 
1216 	icmp6 = (icmp6_t *)&mp->b_rptr[iph_hdr_length];
1217 	ip6h = (ip6_t *)&icmp6[1];
1218 	if (!ip_hdr_length_nexthdr_v6(mp, ip6h, &iph_hdr_length, &nexthdrp)) {
1219 		freemsg(mp);
1220 		return;
1221 	}
1222 	ASSERT(*nexthdrp == IPPROTO_SCTP);
1223 
1224 	/* XXX need ifindex to find connection */
1225 	sctpha = (sctp_hdr_t *)((char *)ip6h + iph_hdr_length);
1226 	if ((uchar_t *)sctpha >= mp->b_wptr) {
1227 		/* not enough data for SCTP header */
1228 		freemsg(mp);
1229 		return;
1230 	}
1231 	switch (icmp6->icmp6_type) {
1232 	case ICMP6_PACKET_TOO_BIG:
1233 		/*
1234 		 * Reduce the MSS based on the new MTU.  This will
1235 		 * eliminate any fragmentation locally.
1236 		 * N.B.  There may well be some funny side-effects on
1237 		 * the local send policy and the remote receive policy.
1238 		 * Pending further research, we provide
1239 		 * sctp_ignore_path_mtu just in case this proves
1240 		 * disastrous somewhere.
1241 		 *
1242 		 * After updating the MSS, retransmit part of the
1243 		 * dropped segment using the new mss by calling
1244 		 * sctp_wput_slow().  Need to adjust all those
1245 		 * params to make sure sctp_wput_slow() work properly.
1246 		 */
1247 		if (sctps->sctps_ignore_path_mtu)
1248 			break;
1249 
1250 		/* find the offending faddr */
1251 		fp = sctp_lookup_faddr(sctp, &ip6h->ip6_dst);
1252 		if (fp == NULL) {
1253 			break;
1254 		}
1255 
1256 		new_mtu = ntohs(icmp6->icmp6_mtu);
1257 
1258 		if (new_mtu - sctp->sctp_hdr6_len >= fp->sfa_pmss)
1259 			break;
1260 
1261 		/* Make sure that sfa_pmss is a multiple of SCTP_ALIGN. */
1262 		fp->sfa_pmss = (new_mtu - sctp->sctp_hdr6_len) &
1263 			~(SCTP_ALIGN - 1);
1264 		fp->pmtu_discovered = 1;
1265 
1266 		break;
1267 
1268 	case ICMP6_DST_UNREACH:
1269 		switch (icmp6->icmp6_code) {
1270 		case ICMP6_DST_UNREACH_NOPORT:
1271 			/* make sure the verification tag matches */
1272 			if (!sctp_icmp_verf(sctp, sctpha, mp)) {
1273 				break;
1274 			}
1275 			if (sctp->sctp_state == SCTPS_COOKIE_WAIT ||
1276 			    sctp->sctp_state == SCTPS_COOKIE_ECHOED) {
1277 				BUMP_MIB(&sctps->sctps_mib, sctpAborted);
1278 				sctp_assoc_event(sctp, SCTP_CANT_STR_ASSOC, 0,
1279 				    NULL);
1280 				sctp_clean_death(sctp, ECONNREFUSED);
1281 			}
1282 			break;
1283 
1284 		case ICMP6_DST_UNREACH_ADMIN:
1285 		case ICMP6_DST_UNREACH_NOROUTE:
1286 		case ICMP6_DST_UNREACH_NOTNEIGHBOR:
1287 		case ICMP6_DST_UNREACH_ADDR:
1288 			/* Record the error in case we finally time out. */
1289 			sctp->sctp_client_errno = EHOSTUNREACH;
1290 			break;
1291 		default:
1292 			break;
1293 		}
1294 		break;
1295 
1296 	case ICMP6_PARAM_PROB:
1297 		/* If this corresponds to an ICMP_PROTOCOL_UNREACHABLE */
1298 		if (icmp6->icmp6_code == ICMP6_PARAMPROB_NEXTHEADER &&
1299 		    (uchar_t *)ip6h + icmp6->icmp6_pptr ==
1300 		    (uchar_t *)nexthdrp) {
1301 			/* make sure the verification tag matches */
1302 			if (!sctp_icmp_verf(sctp, sctpha, mp)) {
1303 				break;
1304 			}
1305 			if (sctp->sctp_state == SCTPS_COOKIE_WAIT) {
1306 				BUMP_MIB(&sctps->sctps_mib, sctpAborted);
1307 				sctp_assoc_event(sctp, SCTP_CANT_STR_ASSOC, 0,
1308 				    NULL);
1309 				sctp_clean_death(sctp, ECONNREFUSED);
1310 			}
1311 			break;
1312 		}
1313 		break;
1314 
1315 	case ICMP6_TIME_EXCEEDED:
1316 	default:
1317 		break;
1318 	}
1319 	freemsg(mp);
1320 }
1321 
1322 /*
1323  * Called by sockfs to create a new sctp instance.
1324  *
1325  * If parent pointer is passed in, inherit settings from it.
1326  */
1327 sctp_t *
1328 sctp_create(void *sctp_ulpd, sctp_t *parent, int family, int flags,
1329     const sctp_upcalls_t *sctp_upcalls, sctp_sockbuf_limits_t *sbl,
1330     cred_t *credp)
1331 {
1332 	sctp_t		*sctp, *psctp;
1333 	conn_t		*sctp_connp;
1334 	mblk_t		*ack_mp, *hb_mp;
1335 	int		sleep = flags & SCTP_CAN_BLOCK ? KM_SLEEP : KM_NOSLEEP;
1336 	zoneid_t	zoneid;
1337 	sctp_stack_t	*sctps;
1338 
1339 	/* User must supply a credential. */
1340 	if (credp == NULL)
1341 		return (NULL);
1342 
1343 	psctp = (sctp_t *)parent;
1344 	if (psctp != NULL) {
1345 		sctps = psctp->sctp_sctps;
1346 		/* Increase here to have common decrease at end */
1347 		netstack_hold(sctps->sctps_netstack);
1348 	} else {
1349 		netstack_t *ns;
1350 
1351 		ns = netstack_find_by_cred(credp);
1352 		ASSERT(ns != NULL);
1353 		sctps = ns->netstack_sctp;
1354 		ASSERT(sctps != NULL);
1355 
1356 		/*
1357 		 * For exclusive stacks we set the zoneid to zero
1358 		 * to make SCTP operate as if in the global zone.
1359 		 */
1360 		if (sctps->sctps_netstack->netstack_stackid !=
1361 		    GLOBAL_NETSTACKID)
1362 			zoneid = GLOBAL_ZONEID;
1363 		else
1364 			zoneid = crgetzoneid(credp);
1365 
1366 		/*
1367 		 * For stackid zero this is done from strplumb.c, but
1368 		 * non-zero stackids are handled here.
1369 		 */
1370 		if (sctps->sctps_g_q == NULL &&
1371 		    sctps->sctps_netstack->netstack_stackid !=
1372 		    GLOBAL_NETSTACKID) {
1373 			sctp_g_q_setup(sctps);
1374 		}
1375 	}
1376 	if ((sctp_connp = ipcl_conn_create(IPCL_SCTPCONN, sleep,
1377 		    sctps->sctps_netstack)) == NULL) {
1378 		netstack_rele(sctps->sctps_netstack);
1379 		SCTP_KSTAT(sctps, sctp_conn_create);
1380 		return (NULL);
1381 	}
1382 	/*
1383 	 * ipcl_conn_create did a netstack_hold. Undo the hold that was
1384 	 * done at top of sctp_create.
1385 	 */
1386 	netstack_rele(sctps->sctps_netstack);
1387 	sctp = CONN2SCTP(sctp_connp);
1388 	sctp->sctp_sctps = sctps;
1389 
1390 	sctp_connp->conn_ulp_labeled = is_system_labeled();
1391 	if ((ack_mp = sctp_timer_alloc(sctp, sctp_ack_timer)) == NULL ||
1392 	    (hb_mp = sctp_timer_alloc(sctp, sctp_heartbeat_timer)) == NULL) {
1393 		if (ack_mp != NULL)
1394 			freeb(ack_mp);
1395 		netstack_rele(sctp_connp->conn_netstack);
1396 		sctp_connp->conn_netstack = NULL;
1397 		sctp->sctp_sctps = NULL;
1398 		SCTP_G_Q_REFRELE(sctps);
1399 		kmem_cache_free(sctp_conn_cache, sctp_connp);
1400 		return (NULL);
1401 	}
1402 
1403 	sctp->sctp_ack_mp = ack_mp;
1404 	sctp->sctp_heartbeat_mp = hb_mp;
1405 
1406 	switch (family) {
1407 	case AF_INET6:
1408 		sctp_connp->conn_af_isv6 = B_TRUE;
1409 		sctp->sctp_ipversion = IPV6_VERSION;
1410 		sctp->sctp_family = AF_INET6;
1411 		break;
1412 
1413 	case AF_INET:
1414 		sctp_connp->conn_af_isv6 = B_FALSE;
1415 		sctp_connp->conn_pkt_isv6 = B_FALSE;
1416 		sctp->sctp_ipversion = IPV4_VERSION;
1417 		sctp->sctp_family = AF_INET;
1418 		break;
1419 	default:
1420 		ASSERT(0);
1421 		break;
1422 	}
1423 	if (sctp_init_values(sctp, psctp, sleep) != 0) {
1424 		freeb(ack_mp);
1425 		freeb(hb_mp);
1426 		netstack_rele(sctp_connp->conn_netstack);
1427 		sctp_connp->conn_netstack = NULL;
1428 		sctp->sctp_sctps = NULL;
1429 		SCTP_G_Q_REFRELE(sctps);
1430 		kmem_cache_free(sctp_conn_cache, sctp_connp);
1431 		return (NULL);
1432 	}
1433 	sctp->sctp_cansleep = ((flags & SCTP_CAN_BLOCK) == SCTP_CAN_BLOCK);
1434 
1435 	sctp->sctp_mss = sctps->sctps_initial_mtu - ((family == AF_INET6) ?
1436 		sctp->sctp_hdr6_len : sctp->sctp_hdr_len);
1437 
1438 	if (psctp != NULL) {
1439 		RUN_SCTP(psctp);
1440 		/*
1441 		 * Inherit local address list, local port. Parent is either
1442 		 * in SCTPS_BOUND, or SCTPS_LISTEN state.
1443 		 */
1444 		ASSERT((psctp->sctp_state == SCTPS_BOUND) ||
1445 		    (psctp->sctp_state == SCTPS_LISTEN));
1446 		if (sctp_dup_saddrs(psctp, sctp, sleep)) {
1447 			WAKE_SCTP(psctp);
1448 			freeb(ack_mp);
1449 			freeb(hb_mp);
1450 			sctp_headers_free(sctp);
1451 			netstack_rele(sctps->sctps_netstack);
1452 			sctp_connp->conn_netstack = NULL;
1453 			sctp->sctp_sctps = NULL;
1454 			SCTP_G_Q_REFRELE(sctps);
1455 			kmem_cache_free(sctp_conn_cache, sctp_connp);
1456 			return (NULL);
1457 		}
1458 
1459 		/*
1460 		 * If the parent is specified, it'll be immediatelly
1461 		 * followed by sctp_connect(). So don't add this guy to
1462 		 * bind hash.
1463 		 */
1464 		sctp->sctp_lport = psctp->sctp_lport;
1465 		sctp->sctp_state = SCTPS_BOUND;
1466 		sctp->sctp_allzones = psctp->sctp_allzones;
1467 		sctp->sctp_zoneid = psctp->sctp_zoneid;
1468 		WAKE_SCTP(psctp);
1469 	} else {
1470 		sctp->sctp_zoneid = zoneid;
1471 	}
1472 
1473 	sctp_connp->conn_cred = credp;
1474 	crhold(credp);
1475 
1476 	/*
1477 	 * If the caller has the process-wide flag set, then default to MAC
1478 	 * exempt mode.  This allows read-down to unlabeled hosts.
1479 	 */
1480 	if (getpflags(NET_MAC_AWARE, credp) != 0)
1481 		sctp_connp->conn_mac_exempt = B_TRUE;
1482 
1483 	/* Initialize SCTP instance values,  our verf tag must never be 0 */
1484 	(void) random_get_pseudo_bytes((uint8_t *)&sctp->sctp_lvtag,
1485 	    sizeof (sctp->sctp_lvtag));
1486 	if (sctp->sctp_lvtag == 0)
1487 		sctp->sctp_lvtag = (uint32_t)gethrtime();
1488 	ASSERT(sctp->sctp_lvtag != 0);
1489 
1490 	sctp->sctp_ltsn = sctp->sctp_lvtag + 1;
1491 	sctp->sctp_lcsn = sctp->sctp_ltsn;
1492 	sctp->sctp_recovery_tsn = sctp->sctp_lastack_rxd = sctp->sctp_ltsn - 1;
1493 	sctp->sctp_adv_pap = sctp->sctp_lastack_rxd;
1494 
1495 	/* Information required by upper layer */
1496 	if (sctp_ulpd != NULL) {
1497 		sctp->sctp_ulpd = sctp_ulpd;
1498 
1499 		ASSERT(sctp_upcalls != NULL);
1500 		bcopy(sctp_upcalls, &sctp->sctp_upcalls,
1501 		    sizeof (sctp_upcalls_t));
1502 		ASSERT(sbl != NULL);
1503 		/* Fill in the socket buffer limits for sctpsockfs */
1504 		sbl->sbl_txlowat = sctp->sctp_xmit_lowater;
1505 		sbl->sbl_txbuf = sctp->sctp_xmit_hiwater;
1506 		sbl->sbl_rxbuf = sctp->sctp_rwnd;
1507 		sbl->sbl_rxlowat = SCTP_RECV_LOWATER;
1508 	}
1509 	/* If no sctp_ulpd, must be creating the default sctp */
1510 	ASSERT(sctp_ulpd != NULL || sctps->sctps_gsctp == NULL);
1511 
1512 	/* Insert this in the global list. */
1513 	SCTP_LINK(sctp, sctps);
1514 
1515 	return (sctp);
1516 }
1517 
1518 /*
1519  * Make sure we wait until the default queue is setup, yet allow
1520  * sctp_g_q_create() to open a SCTP stream.
1521  * We need to allow sctp_g_q_create() do do an open
1522  * of sctp, hence we compare curhread.
1523  * All others have to wait until the sctps_g_q has been
1524  * setup.
1525  */
1526 void
1527 sctp_g_q_setup(sctp_stack_t *sctps)
1528 {
1529 	mutex_enter(&sctps->sctps_g_q_lock);
1530 	if (sctps->sctps_g_q != NULL) {
1531 		mutex_exit(&sctps->sctps_g_q_lock);
1532 		return;
1533 	}
1534 	if (sctps->sctps_g_q_creator == NULL) {
1535 		/* This thread will set it up */
1536 		sctps->sctps_g_q_creator = curthread;
1537 		mutex_exit(&sctps->sctps_g_q_lock);
1538 		sctp_g_q_create(sctps);
1539 		mutex_enter(&sctps->sctps_g_q_lock);
1540 		ASSERT(sctps->sctps_g_q_creator == curthread);
1541 		sctps->sctps_g_q_creator = NULL;
1542 		cv_signal(&sctps->sctps_g_q_cv);
1543 		ASSERT(sctps->sctps_g_q != NULL);
1544 		mutex_exit(&sctps->sctps_g_q_lock);
1545 		return;
1546 	}
1547 	/* Everybody but the creator has to wait */
1548 	if (sctps->sctps_g_q_creator != curthread) {
1549 		while (sctps->sctps_g_q == NULL)
1550 			cv_wait(&sctps->sctps_g_q_cv, &sctps->sctps_g_q_lock);
1551 	}
1552 	mutex_exit(&sctps->sctps_g_q_lock);
1553 }
1554 
1555 major_t IP_MAJ;
1556 #define	IP	"ip"
1557 
1558 #define	SCTP6DEV		"/devices/pseudo/sctp6@0:sctp6"
1559 
1560 /*
1561  * Create a default sctp queue here instead of in strplumb
1562  */
1563 void
1564 sctp_g_q_create(sctp_stack_t *sctps)
1565 {
1566 	int error;
1567 	ldi_handle_t	lh = NULL;
1568 	ldi_ident_t	li = NULL;
1569 	int		rval;
1570 	cred_t		*cr;
1571 
1572 #ifdef NS_DEBUG
1573 	(void) printf("sctp_g_q_create()for stack %d\n",
1574 	    sctps->sctps_netstack->netstack_stackid);
1575 #endif
1576 
1577 	ASSERT(sctps->sctps_g_q_creator == curthread);
1578 
1579 	error = ldi_ident_from_major(IP_MAJ, &li);
1580 	if (error) {
1581 #ifdef DEBUG
1582 		printf("sctp_g_q_create: lyr ident get failed error %d\n",
1583 		    error);
1584 #endif
1585 		return;
1586 	}
1587 
1588 	cr = zone_get_kcred(netstackid_to_zoneid(
1589 				sctps->sctps_netstack->netstack_stackid));
1590 	ASSERT(cr != NULL);
1591 	/*
1592 	 * We set the sctp default queue to IPv6 because IPv4 falls
1593 	 * back to IPv6 when it can't find a client, but
1594 	 * IPv6 does not fall back to IPv4.
1595 	 */
1596 	error = ldi_open_by_name(SCTP6DEV, FREAD|FWRITE, cr, &lh, li);
1597 	if (error) {
1598 #ifdef DEBUG
1599 		printf("sctp_g_q_create: open of SCTP6DEV failed error %d\n",
1600 		    error);
1601 #endif
1602 		goto out;
1603 	}
1604 
1605 	/*
1606 	 * This ioctl causes the sctp framework to cache a pointer to
1607 	 * this stream, so we don't want to close the stream after
1608 	 * this operation.
1609 	 * Use the kernel credentials that are for the zone we're in.
1610 	 */
1611 	error = ldi_ioctl(lh, SCTP_IOC_DEFAULT_Q,
1612 	    (intptr_t)0, FKIOCTL, cr, &rval);
1613 	if (error) {
1614 #ifdef DEBUG
1615 		printf("sctp_g_q_create: ioctl SCTP_IOC_DEFAULT_Q failed "
1616 		    "error %d\n", error);
1617 #endif
1618 		goto out;
1619 	}
1620 	sctps->sctps_g_q_lh = lh;	/* For sctp_g_q_inactive */
1621 	lh = NULL;
1622 out:
1623 	/* Close layered handles */
1624 	if (li)
1625 		ldi_ident_release(li);
1626 	/* Keep cred around until _inactive needs it */
1627 	sctps->sctps_g_q_cr = cr;
1628 }
1629 
1630 /*
1631  * Remove the sctp_default queue so that new connections will not find it.
1632  * SCTP uses sctp_g_q for all transmission, so all sctp'ts implicitly
1633  * refer to it. Hence have each one have a reference on sctp_g_q_ref!
1634  *
1635  * We decrement the refcnt added in sctp_g_q_create. Once all the
1636  * sctp_t's which use the default go away, sctp_g_q_close will be called
1637  * and close the sctp_g_q. Once sctp_g_q is closed, sctp_close() will drop the
1638  * last reference count on the stack by calling netstack_rele().
1639  */
1640 void
1641 sctp_g_q_destroy(sctp_stack_t *sctps)
1642 {
1643 	if (sctps->sctps_g_q == NULL) {
1644 		return;	/* Nothing to cleanup */
1645 	}
1646 	/*
1647 	 * Keep sctps_g_q and sctps_gsctp until the last reference has
1648 	 * dropped, since the output is always done using those.
1649 	 * Need to decrement twice to take sctp_g_q_create and
1650 	 * the gsctp reference into account so that sctp_g_q_inactive is called
1651 	 * when all but the default queue remains.
1652 	 */
1653 #ifdef NS_DEBUG
1654 	(void) printf("sctp_g_q_destroy: ref %d\n",
1655 	    sctps->sctps_g_q_ref);
1656 #endif
1657 	SCTP_G_Q_REFRELE(sctps);
1658 }
1659 
1660 /*
1661  * Called when last user (could be sctp_g_q_destroy) drops reference count
1662  * using SCTP_G_Q_REFRELE.
1663  * Run by sctp_q_q_inactive using a taskq.
1664  */
1665 static void
1666 sctp_g_q_close(void *arg)
1667 {
1668 	sctp_stack_t *sctps = arg;
1669 	int error;
1670 	ldi_handle_t	lh = NULL;
1671 	ldi_ident_t	li = NULL;
1672 	cred_t		*cr;
1673 
1674 	lh = sctps->sctps_g_q_lh;
1675 	if (lh == NULL)
1676 		return;	/* Nothing to cleanup */
1677 
1678 	error = ldi_ident_from_major(IP_MAJ, &li);
1679 	if (error) {
1680 #ifdef NS_DEBUG
1681 		printf("sctp_g_q_inactive: lyr ident get failed error %d\n",
1682 		    error);
1683 #endif
1684 		return;
1685 	}
1686 
1687 	cr = sctps->sctps_g_q_cr;
1688 	sctps->sctps_g_q_cr = NULL;
1689 	ASSERT(cr != NULL);
1690 
1691 	/*
1692 	 * Make sure we can break the recursion when sctp_close decrements
1693 	 * the reference count causing g_q_inactive to be called again.
1694 	 */
1695 	sctps->sctps_g_q_lh = NULL;
1696 
1697 	/* close the default queue */
1698 	(void) ldi_close(lh, FREAD|FWRITE, cr);
1699 
1700 	/* Close layered handles */
1701 	ldi_ident_release(li);
1702 	crfree(cr);
1703 
1704 	ASSERT(sctps->sctps_g_q != NULL);
1705 	sctps->sctps_g_q = NULL;
1706 	/*
1707 	 * Now free sctps_gsctp.
1708 	 */
1709 	ASSERT(sctps->sctps_gsctp != NULL);
1710 	sctp_closei_local(sctps->sctps_gsctp);
1711 	SCTP_CONDEMNED(sctps->sctps_gsctp);
1712 	SCTP_REFRELE(sctps->sctps_gsctp);
1713 	sctps->sctps_gsctp = NULL;
1714 }
1715 
1716 /*
1717  * Called when last sctp_t drops reference count using SCTP_G_Q_REFRELE.
1718  *
1719  * Have to ensure that the ldi routines are not used by an
1720  * interrupt thread by using a taskq.
1721  */
1722 void
1723 sctp_g_q_inactive(sctp_stack_t *sctps)
1724 {
1725 	if (sctps->sctps_g_q_lh == NULL)
1726 		return;	/* Nothing to cleanup */
1727 
1728 	ASSERT(sctps->sctps_g_q_ref == 0);
1729 	SCTP_G_Q_REFHOLD(sctps); /* Compensate for what g_q_destroy did */
1730 
1731 	if (servicing_interrupt()) {
1732 		(void) taskq_dispatch(sctp_taskq, sctp_g_q_close,
1733 			    (void *) sctps, TQ_SLEEP);
1734 	} else {
1735 		sctp_g_q_close(sctps);
1736 	}
1737 }
1738 
1739 /* Run at module load time */
1740 void
1741 sctp_ddi_g_init(void)
1742 {
1743 	IP_MAJ = ddi_name_to_major(IP);
1744 
1745 	/* Create sctp_t/conn_t cache */
1746 	sctp_conn_cache_init();
1747 
1748 	/* Create the faddr cache */
1749 	sctp_faddr_init();
1750 
1751 	/* Create the sets cache */
1752 	sctp_sets_init();
1753 
1754 	/* Create the PR-SCTP sets cache */
1755 	sctp_ftsn_sets_init();
1756 
1757 	/* Initialize tables used for CRC calculation */
1758 	sctp_crc32_init();
1759 
1760 	sctp_taskq = taskq_create("sctp_taskq", 1, minclsyspri, 1, 1,
1761 	    TASKQ_PREPOPULATE);
1762 
1763 	/*
1764 	 * We want to be informed each time a stack is created or
1765 	 * destroyed in the kernel, so we can maintain the
1766 	 * set of sctp_stack_t's.
1767 	 */
1768 	netstack_register(NS_SCTP, sctp_stack_init, sctp_stack_shutdown,
1769 	    sctp_stack_fini);
1770 }
1771 
1772 static void *
1773 sctp_stack_init(netstackid_t stackid, netstack_t *ns)
1774 {
1775 	sctp_stack_t	*sctps;
1776 
1777 	sctps = kmem_zalloc(sizeof (*sctps), KM_SLEEP);
1778 	sctps->sctps_netstack = ns;
1779 
1780 	/* Initialize locks */
1781 	mutex_init(&sctps->sctps_g_q_lock, NULL, MUTEX_DEFAULT, NULL);
1782 	cv_init(&sctps->sctps_g_q_cv, NULL, CV_DEFAULT, NULL);
1783 	mutex_init(&sctps->sctps_g_lock, NULL, MUTEX_DEFAULT, NULL);
1784 	mutex_init(&sctps->sctps_epriv_port_lock, NULL, MUTEX_DEFAULT, NULL);
1785 	sctps->sctps_g_num_epriv_ports = SCTP_NUM_EPRIV_PORTS;
1786 	sctps->sctps_g_epriv_ports[0] = 2049;
1787 	sctps->sctps_g_epriv_ports[1] = 4045;
1788 
1789 	/* Initialize SCTP hash arrays. */
1790 	sctp_hash_init(sctps);
1791 
1792 	sctps->sctps_pad_mp = allocb(SCTP_ALIGN, BPRI_MED);
1793 	bzero(sctps->sctps_pad_mp->b_rptr, SCTP_ALIGN);
1794 	ASSERT(sctps->sctps_pad_mp);
1795 
1796 	if (!sctp_nd_init(sctps)) {
1797 		sctp_nd_free(sctps);
1798 	}
1799 
1800 	/* Initialize the recvq taskq. */
1801 	sctp_rq_tq_init(sctps);
1802 
1803 	/* saddr init */
1804 	sctp_saddr_init(sctps);
1805 
1806 	/* Global SCTP PCB list. */
1807 	list_create(&sctps->sctps_g_list, sizeof (sctp_t),
1808 	    offsetof(sctp_t, sctp_list));
1809 
1810 	/* Initialize sctp kernel stats. */
1811 	sctps->sctps_mibkp = sctp_kstat_init(stackid);
1812 	sctps->sctps_kstat =
1813 	    sctp_kstat2_init(stackid, &sctps->sctps_statistics);
1814 
1815 	return (sctps);
1816 }
1817 
1818 /*
1819  * Called when the module is about to be unloaded.
1820  */
1821 void
1822 sctp_ddi_g_destroy(void)
1823 {
1824 	/* Destroy sctp_t/conn_t caches */
1825 	sctp_conn_cache_fini();
1826 
1827 	/* Destroy the faddr cache */
1828 	sctp_faddr_fini();
1829 
1830 	/* Destroy the sets cache */
1831 	sctp_sets_fini();
1832 
1833 	/* Destroy the PR-SCTP sets cache */
1834 	sctp_ftsn_sets_fini();
1835 
1836 	netstack_unregister(NS_SCTP);
1837 	taskq_destroy(sctp_taskq);
1838 }
1839 
1840 /*
1841  * Shut down the SCTP stack instance.
1842  */
1843 /* ARGSUSED */
1844 static void
1845 sctp_stack_shutdown(netstackid_t stackid, void *arg)
1846 {
1847 	sctp_stack_t *sctps = (sctp_stack_t *)arg;
1848 
1849 	sctp_g_q_destroy(sctps);
1850 }
1851 
1852 /*
1853  * Free the SCTP stack instance.
1854  */
1855 static void
1856 sctp_stack_fini(netstackid_t stackid, void *arg)
1857 {
1858 	sctp_stack_t *sctps = (sctp_stack_t *)arg;
1859 
1860 	sctp_nd_free(sctps);
1861 
1862 	/* Destroy the recvq taskqs. */
1863 	sctp_rq_tq_fini(sctps);
1864 
1865 	/* Destroy saddr  */
1866 	sctp_saddr_fini(sctps);
1867 
1868 	/* Global SCTP PCB list. */
1869 	list_destroy(&sctps->sctps_g_list);
1870 
1871 	/* Destroy SCTP hash arrays. */
1872 	sctp_hash_destroy(sctps);
1873 
1874 	/* Destroy SCTP kernel stats. */
1875 	sctp_kstat2_fini(stackid, sctps->sctps_kstat);
1876 	sctps->sctps_kstat = NULL;
1877 	bzero(&sctps->sctps_statistics, sizeof (sctps->sctps_statistics));
1878 
1879 	sctp_kstat_fini(stackid, sctps->sctps_mibkp);
1880 	sctps->sctps_mibkp = NULL;
1881 
1882 	freeb(sctps->sctps_pad_mp);
1883 	sctps->sctps_pad_mp = NULL;
1884 
1885 	mutex_destroy(&sctps->sctps_g_lock);
1886 	mutex_destroy(&sctps->sctps_epriv_port_lock);
1887 	mutex_destroy(&sctps->sctps_g_q_lock);
1888 	cv_destroy(&sctps->sctps_g_q_cv);
1889 
1890 	kmem_free(sctps, sizeof (*sctps));
1891 }
1892 
1893 void
1894 sctp_display_all(sctp_stack_t *sctps)
1895 {
1896 	sctp_t *sctp_walker;
1897 
1898 	mutex_enter(&sctps->sctps_g_lock);
1899 	for (sctp_walker = sctps->sctps_gsctp; sctp_walker != NULL;
1900 	    sctp_walker = (sctp_t *)list_next(&sctps->sctps_g_list,
1901 	    sctp_walker)) {
1902 		(void) sctp_display(sctp_walker, NULL);
1903 	}
1904 	mutex_exit(&sctps->sctps_g_lock);
1905 }
1906 
1907 static void
1908 sctp_rq_tq_init(sctp_stack_t *sctps)
1909 {
1910 	sctps->sctps_recvq_tq_list_max_sz = 16;
1911 	sctps->sctps_recvq_tq_list_cur_sz = 1;
1912 	/*
1913 	 * Initialize the recvq_tq_list and create the first recvq taskq.
1914 	 * What to do if it fails?
1915 	 */
1916 	sctps->sctps_recvq_tq_list =
1917 	    kmem_zalloc(sctps->sctps_recvq_tq_list_max_sz * sizeof (taskq_t *),
1918 		KM_SLEEP);
1919 	sctps->sctps_recvq_tq_list[0] = taskq_create("sctp_def_recvq_taskq",
1920 	    MIN(sctp_recvq_tq_thr_max, MAX(sctp_recvq_tq_thr_min, ncpus)),
1921 	    minclsyspri, sctp_recvq_tq_task_min, sctp_recvq_tq_task_max,
1922 	    TASKQ_PREPOPULATE);
1923 	mutex_init(&sctps->sctps_rq_tq_lock, NULL, MUTEX_DEFAULT, NULL);
1924 }
1925 
1926 static void
1927 sctp_rq_tq_fini(sctp_stack_t *sctps)
1928 {
1929 	int i;
1930 
1931 	for (i = 0; i < sctps->sctps_recvq_tq_list_cur_sz; i++) {
1932 		ASSERT(sctps->sctps_recvq_tq_list[i] != NULL);
1933 		taskq_destroy(sctps->sctps_recvq_tq_list[i]);
1934 	}
1935 	kmem_free(sctps->sctps_recvq_tq_list,
1936 	    sctps->sctps_recvq_tq_list_max_sz * sizeof (taskq_t *));
1937 	sctps->sctps_recvq_tq_list = NULL;
1938 }
1939 
1940 /* Add another taskq for a new ill. */
1941 void
1942 sctp_inc_taskq(sctp_stack_t *sctps)
1943 {
1944 	taskq_t *tq;
1945 	char tq_name[TASKQ_NAMELEN];
1946 
1947 	mutex_enter(&sctps->sctps_rq_tq_lock);
1948 	if (sctps->sctps_recvq_tq_list_cur_sz + 1 >
1949 	    sctps->sctps_recvq_tq_list_max_sz) {
1950 		mutex_exit(&sctps->sctps_rq_tq_lock);
1951 		cmn_err(CE_NOTE, "Cannot create more SCTP recvq taskq");
1952 		return;
1953 	}
1954 
1955 	(void) snprintf(tq_name, sizeof (tq_name), "sctp_recvq_taskq_%u",
1956 	    sctps->sctps_recvq_tq_list_cur_sz);
1957 	tq = taskq_create(tq_name,
1958 	    MIN(sctp_recvq_tq_thr_max, MAX(sctp_recvq_tq_thr_min, ncpus)),
1959 	    minclsyspri, sctp_recvq_tq_task_min, sctp_recvq_tq_task_max,
1960 	    TASKQ_PREPOPULATE);
1961 	if (tq == NULL) {
1962 		mutex_exit(&sctps->sctps_rq_tq_lock);
1963 		cmn_err(CE_NOTE, "SCTP recvq taskq creation failed");
1964 		return;
1965 	}
1966 	ASSERT(sctps->sctps_recvq_tq_list[
1967 		    sctps->sctps_recvq_tq_list_cur_sz] == NULL);
1968 	sctps->sctps_recvq_tq_list[sctps->sctps_recvq_tq_list_cur_sz] = tq;
1969 	atomic_add_32(&sctps->sctps_recvq_tq_list_cur_sz, 1);
1970 	mutex_exit(&sctps->sctps_rq_tq_lock);
1971 }
1972 
1973 #ifdef DEBUG
1974 uint32_t sendq_loop_cnt = 0;
1975 uint32_t sendq_collision = 0;
1976 uint32_t sendq_empty = 0;
1977 #endif
1978 
1979 void
1980 sctp_add_sendq(sctp_t *sctp, mblk_t *mp)
1981 {
1982 	mutex_enter(&sctp->sctp_sendq_lock);
1983 	if (sctp->sctp_sendq == NULL) {
1984 		sctp->sctp_sendq = mp;
1985 		sctp->sctp_sendq_tail = mp;
1986 	} else {
1987 		sctp->sctp_sendq_tail->b_next = mp;
1988 		sctp->sctp_sendq_tail = mp;
1989 	}
1990 	mutex_exit(&sctp->sctp_sendq_lock);
1991 }
1992 
1993 void
1994 sctp_process_sendq(sctp_t *sctp)
1995 {
1996 	mblk_t *mp;
1997 #ifdef DEBUG
1998 	uint32_t loop_cnt = 0;
1999 #endif
2000 
2001 	mutex_enter(&sctp->sctp_sendq_lock);
2002 	if (sctp->sctp_sendq == NULL || sctp->sctp_sendq_sending) {
2003 #ifdef DEBUG
2004 		if (sctp->sctp_sendq == NULL)
2005 			sendq_empty++;
2006 		else
2007 			sendq_collision++;
2008 #endif
2009 		mutex_exit(&sctp->sctp_sendq_lock);
2010 		return;
2011 	}
2012 	sctp->sctp_sendq_sending = B_TRUE;
2013 
2014 	/*
2015 	 * Note that while we are in this loop, other thread can put
2016 	 * new packets in the receive queue.  We may be looping for
2017 	 * quite a while.  This is OK even for an interrupt thread.
2018 	 * The reason is that SCTP should only able to send a limited
2019 	 * number of packets out in a burst.  So the number of times
2020 	 * we go through this loop should not be many.
2021 	 */
2022 	while ((mp = sctp->sctp_sendq) != NULL) {
2023 		sctp->sctp_sendq = mp->b_next;
2024 		ASSERT(sctp->sctp_connp->conn_ref > 0);
2025 		mutex_exit(&sctp->sctp_sendq_lock);
2026 		mp->b_next = NULL;
2027 		CONN_INC_REF(sctp->sctp_connp);
2028 		mp->b_flag |= MSGHASREF;
2029 		/* If we don't have sctp_current, default to IPv4 */
2030 		IP_PUT(mp, sctp->sctp_connp, sctp->sctp_current == NULL ?
2031 		    B_TRUE : sctp->sctp_current->isv4);
2032 		BUMP_LOCAL(sctp->sctp_opkts);
2033 #ifdef DEBUG
2034 		loop_cnt++;
2035 #endif
2036 		mutex_enter(&sctp->sctp_sendq_lock);
2037 	}
2038 
2039 	sctp->sctp_sendq_tail = NULL;
2040 	sctp->sctp_sendq_sending = B_FALSE;
2041 #ifdef DEBUG
2042 	if (loop_cnt > sendq_loop_cnt)
2043 		sendq_loop_cnt = loop_cnt;
2044 #endif
2045 	mutex_exit(&sctp->sctp_sendq_lock);
2046 }
2047 
2048 #ifdef DEBUG
2049 uint32_t recvq_loop_cnt = 0;
2050 uint32_t recvq_call = 0;
2051 #endif
2052 
2053 /*
2054  * Find the next recvq_tq to use.  This routine will go thru all the
2055  * taskqs until it can dispatch a job for the sctp.  If this fails,
2056  * it will create a new taskq and try it.
2057  */
2058 static boolean_t
2059 sctp_find_next_tq(sctp_t *sctp)
2060 {
2061 	int next_tq, try;
2062 	taskq_t *tq;
2063 	sctp_stack_t	*sctps = sctp->sctp_sctps;
2064 
2065 	/*
2066 	 * Note that since we don't hold a lock on sctp_rq_tq_lock for
2067 	 * performance reason, recvq_ta_list_cur_sz can be changed during
2068 	 * this loop.  The problem this will create is that the loop may
2069 	 * not have tried all the recvq_tq.  This should be OK.
2070 	 */
2071 	next_tq = atomic_add_32_nv(&sctps->sctps_recvq_tq_list_cur, 1) %
2072 	    sctps->sctps_recvq_tq_list_cur_sz;
2073 	for (try = 0; try < sctps->sctps_recvq_tq_list_cur_sz; try++) {
2074 		tq = sctps->sctps_recvq_tq_list[next_tq];
2075 		if (taskq_dispatch(tq, sctp_process_recvq, sctp,
2076 		    TQ_NOSLEEP) != NULL) {
2077 			sctp->sctp_recvq_tq = tq;
2078 			return (B_TRUE);
2079 		}
2080 		next_tq = (next_tq + 1) % sctps->sctps_recvq_tq_list_cur_sz;
2081 	}
2082 
2083 	/*
2084 	 * Create one more taskq and try it.  Note that sctp_inc_taskq()
2085 	 * may not have created another taskq if the number of recvq
2086 	 * taskqs is at the maximum.  We are probably in a pretty bad
2087 	 * shape if this actually happens...
2088 	 */
2089 	sctp_inc_taskq(sctps);
2090 	tq = sctps->sctps_recvq_tq_list[sctps->sctps_recvq_tq_list_cur_sz - 1];
2091 	if (taskq_dispatch(tq, sctp_process_recvq, sctp, TQ_NOSLEEP) != NULL) {
2092 		sctp->sctp_recvq_tq = tq;
2093 		return (B_TRUE);
2094 	}
2095 	SCTP_KSTAT(sctps, sctp_find_next_tq);
2096 	return (B_FALSE);
2097 }
2098 
2099 /*
2100  * To add a message to the recvq.  Note that the sctp_timer_fire()
2101  * routine also uses this function to add the timer message to the
2102  * receive queue for later processing.  And it should be the only
2103  * caller of sctp_add_recvq() which sets the try_harder argument
2104  * to B_TRUE.
2105  *
2106  * If the try_harder argument is B_TRUE, this routine sctp_find_next_tq()
2107  * will try very hard to dispatch the task.  Refer to the comment
2108  * for that routine on how it does that.
2109  */
2110 boolean_t
2111 sctp_add_recvq(sctp_t *sctp, mblk_t *mp, boolean_t caller_hold_lock)
2112 {
2113 	if (!caller_hold_lock)
2114 		mutex_enter(&sctp->sctp_recvq_lock);
2115 
2116 	/* If the taskq dispatch has not been scheduled, do it now. */
2117 	if (sctp->sctp_recvq_tq == NULL) {
2118 		ASSERT(sctp->sctp_recvq == NULL);
2119 		if (!sctp_find_next_tq(sctp)) {
2120 			if (!caller_hold_lock)
2121 				mutex_exit(&sctp->sctp_recvq_lock);
2122 			return (B_FALSE);
2123 		}
2124 		/* Make sure the sctp_t will not go away. */
2125 		SCTP_REFHOLD(sctp);
2126 	}
2127 
2128 	if (sctp->sctp_recvq == NULL) {
2129 		sctp->sctp_recvq = mp;
2130 		sctp->sctp_recvq_tail = mp;
2131 	} else {
2132 		sctp->sctp_recvq_tail->b_next = mp;
2133 		sctp->sctp_recvq_tail = mp;
2134 	}
2135 
2136 	if (!caller_hold_lock)
2137 		mutex_exit(&sctp->sctp_recvq_lock);
2138 	return (B_TRUE);
2139 }
2140 
2141 static void
2142 sctp_process_recvq(void *arg)
2143 {
2144 	sctp_t		*sctp = (sctp_t *)arg;
2145 	mblk_t		*mp;
2146 	mblk_t		*ipsec_mp;
2147 #ifdef DEBUG
2148 	uint32_t	loop_cnt = 0;
2149 #endif
2150 
2151 #ifdef	_BIG_ENDIAN
2152 #define	IPVER(ip6h)	((((uint32_t *)ip6h)[0] >> 28) & 0x7)
2153 #else
2154 #define	IPVER(ip6h)	((((uint32_t *)ip6h)[0] >> 4) & 0x7)
2155 #endif
2156 
2157 	RUN_SCTP(sctp);
2158 	mutex_enter(&sctp->sctp_recvq_lock);
2159 
2160 #ifdef DEBUG
2161 	recvq_call++;
2162 #endif
2163 	/*
2164 	 * Note that while we are in this loop, other thread can put
2165 	 * new packets in the receive queue.  We may be looping for
2166 	 * quite a while.
2167 	 */
2168 	while ((mp = sctp->sctp_recvq) != NULL) {
2169 		sctp->sctp_recvq = mp->b_next;
2170 		mutex_exit(&sctp->sctp_recvq_lock);
2171 		mp->b_next = NULL;
2172 #ifdef DEBUG
2173 		loop_cnt++;
2174 #endif
2175 		ipsec_mp = mp->b_prev;
2176 		mp->b_prev = NULL;
2177 		sctp_input_data(sctp, mp, ipsec_mp);
2178 
2179 		mutex_enter(&sctp->sctp_recvq_lock);
2180 	}
2181 
2182 	sctp->sctp_recvq_tail = NULL;
2183 	sctp->sctp_recvq_tq = NULL;
2184 
2185 	mutex_exit(&sctp->sctp_recvq_lock);
2186 
2187 	WAKE_SCTP(sctp);
2188 
2189 	/* We may have sent something when processing the receive queue. */
2190 	sctp_process_sendq(sctp);
2191 #ifdef DEBUG
2192 	if (loop_cnt > recvq_loop_cnt)
2193 		recvq_loop_cnt = loop_cnt;
2194 #endif
2195 	/* Now it can go away. */
2196 	SCTP_REFRELE(sctp);
2197 }
2198 
2199 /* ARGSUSED */
2200 static int
2201 sctp_conn_cache_constructor(void *buf, void *cdrarg, int kmflags)
2202 {
2203 	conn_t	*sctp_connp = (conn_t *)buf;
2204 	sctp_t	*sctp = (sctp_t *)&sctp_connp[1];
2205 
2206 	bzero(buf, (char *)&sctp[1] - (char *)buf);
2207 
2208 	sctp->sctp_connp = sctp_connp;
2209 	mutex_init(&sctp->sctp_reflock, NULL, MUTEX_DEFAULT, NULL);
2210 	mutex_init(&sctp->sctp_lock, NULL, MUTEX_DEFAULT, NULL);
2211 	mutex_init(&sctp->sctp_recvq_lock, NULL, MUTEX_DEFAULT, NULL);
2212 	cv_init(&sctp->sctp_cv, NULL, CV_DEFAULT, NULL);
2213 	mutex_init(&sctp->sctp_sendq_lock, NULL, MUTEX_DEFAULT, NULL);
2214 
2215 	sctp_connp->conn_rq = sctp_connp->conn_wq = NULL;
2216 	sctp_connp->conn_multicast_loop = IP_DEFAULT_MULTICAST_LOOP;
2217 	sctp_connp->conn_ulp = IPPROTO_SCTP;
2218 	mutex_init(&sctp_connp->conn_lock, NULL, MUTEX_DEFAULT, NULL);
2219 	cv_init(&sctp_connp->conn_cv, NULL, CV_DEFAULT, NULL);
2220 
2221 	return (0);
2222 }
2223 
2224 /* ARGSUSED */
2225 static void
2226 sctp_conn_cache_destructor(void *buf, void *cdrarg)
2227 {
2228 	conn_t	*sctp_connp = (conn_t *)buf;
2229 	sctp_t	*sctp = (sctp_t *)&sctp_connp[1];
2230 
2231 	ASSERT(!MUTEX_HELD(&sctp->sctp_lock));
2232 	ASSERT(!MUTEX_HELD(&sctp->sctp_reflock));
2233 	ASSERT(!MUTEX_HELD(&sctp->sctp_recvq_lock));
2234 	ASSERT(!MUTEX_HELD(&sctp->sctp_sendq_lock));
2235 	ASSERT(!MUTEX_HELD(&sctp->sctp_connp->conn_lock));
2236 
2237 	ASSERT(sctp->sctp_conn_hash_next == NULL);
2238 	ASSERT(sctp->sctp_conn_hash_prev == NULL);
2239 	ASSERT(sctp->sctp_listen_hash_next == NULL);
2240 	ASSERT(sctp->sctp_listen_hash_prev == NULL);
2241 	ASSERT(sctp->sctp_listen_tfp == NULL);
2242 	ASSERT(sctp->sctp_conn_tfp == NULL);
2243 
2244 	ASSERT(sctp->sctp_faddrs == NULL);
2245 	ASSERT(sctp->sctp_nsaddrs == 0);
2246 
2247 	ASSERT(sctp->sctp_ulpd == NULL);
2248 
2249 	ASSERT(sctp->sctp_lastfaddr == NULL);
2250 	ASSERT(sctp->sctp_primary == NULL);
2251 	ASSERT(sctp->sctp_current == NULL);
2252 	ASSERT(sctp->sctp_lastdata == NULL);
2253 
2254 	ASSERT(sctp->sctp_xmit_head == NULL);
2255 	ASSERT(sctp->sctp_xmit_tail == NULL);
2256 	ASSERT(sctp->sctp_xmit_unsent == NULL);
2257 	ASSERT(sctp->sctp_xmit_unsent_tail == NULL);
2258 
2259 	ASSERT(sctp->sctp_ostrcntrs == NULL);
2260 
2261 	ASSERT(sctp->sctp_sack_info == NULL);
2262 	ASSERT(sctp->sctp_ack_mp == NULL);
2263 	ASSERT(sctp->sctp_instr == NULL);
2264 
2265 	ASSERT(sctp->sctp_iphc == NULL);
2266 	ASSERT(sctp->sctp_iphc6 == NULL);
2267 	ASSERT(sctp->sctp_ipha == NULL);
2268 	ASSERT(sctp->sctp_ip6h == NULL);
2269 	ASSERT(sctp->sctp_sctph == NULL);
2270 	ASSERT(sctp->sctp_sctph6 == NULL);
2271 
2272 	ASSERT(sctp->sctp_cookie_mp == NULL);
2273 
2274 	ASSERT(sctp->sctp_refcnt == 0);
2275 	ASSERT(sctp->sctp_timer_mp == NULL);
2276 	ASSERT(sctp->sctp_connp->conn_ref == 0);
2277 	ASSERT(sctp->sctp_heartbeat_mp == NULL);
2278 	ASSERT(sctp->sctp_ptpbhn == NULL && sctp->sctp_bind_hash == NULL);
2279 
2280 	ASSERT(sctp->sctp_shutdown_faddr == NULL);
2281 
2282 	ASSERT(sctp->sctp_cxmit_list == NULL);
2283 
2284 	ASSERT(sctp->sctp_recvq == NULL);
2285 	ASSERT(sctp->sctp_recvq_tail == NULL);
2286 	ASSERT(sctp->sctp_recvq_tq == NULL);
2287 
2288 	ASSERT(sctp->sctp_sendq == NULL);
2289 	ASSERT(sctp->sctp_sendq_tail == NULL);
2290 	ASSERT(sctp->sctp_sendq_sending == B_FALSE);
2291 
2292 	ASSERT(sctp->sctp_ipp_hopopts == NULL);
2293 	ASSERT(sctp->sctp_ipp_rtdstopts == NULL);
2294 	ASSERT(sctp->sctp_ipp_rthdr == NULL);
2295 	ASSERT(sctp->sctp_ipp_dstopts == NULL);
2296 	ASSERT(sctp->sctp_ipp_pathmtu == NULL);
2297 
2298 	mutex_destroy(&sctp->sctp_reflock);
2299 	mutex_destroy(&sctp->sctp_lock);
2300 	mutex_destroy(&sctp->sctp_recvq_lock);
2301 	cv_destroy(&sctp->sctp_cv);
2302 	mutex_destroy(&sctp->sctp_sendq_lock);
2303 
2304 	mutex_destroy(&sctp_connp->conn_lock);
2305 	cv_destroy(&sctp_connp->conn_cv);
2306 }
2307 
2308 static void
2309 sctp_conn_cache_init()
2310 {
2311 	sctp_conn_cache = kmem_cache_create("sctp_conn_cache",
2312 	    sizeof (sctp_t) + sizeof (conn_t), 0, sctp_conn_cache_constructor,
2313 	    sctp_conn_cache_destructor, NULL, NULL, NULL, 0);
2314 }
2315 
2316 static void
2317 sctp_conn_cache_fini()
2318 {
2319 	kmem_cache_destroy(sctp_conn_cache);
2320 }
2321