xref: /illumos-gate/usr/src/uts/common/inet/sctp/sctp.c (revision 15deec58)
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 	/* Sanity check, don't do the same thing twice.  */
499 	if (connp->conn_state_flags & CONN_CLOSING) {
500 		ASSERT(sctp->sctp_state == SCTPS_IDLE);
501 		return;
502 	}
503 
504 	/* Stop and free the timers */
505 	sctp_free_faddr_timers(sctp);
506 	if ((mp = sctp->sctp_heartbeat_mp) != NULL) {
507 		sctp_timer_free(mp);
508 		sctp->sctp_heartbeat_mp = NULL;
509 	}
510 	if ((mp = sctp->sctp_ack_mp) != NULL) {
511 		sctp_timer_free(mp);
512 		sctp->sctp_ack_mp = NULL;
513 	}
514 
515 	/* Set the CONN_CLOSING flag so that IP will not cache IRE again. */
516 	mutex_enter(&connp->conn_lock);
517 	connp->conn_state_flags |= CONN_CLOSING;
518 	ire = connp->conn_ire_cache;
519 	connp->conn_ire_cache = NULL;
520 	mutex_exit(&connp->conn_lock);
521 	if (ire != NULL)
522 		IRE_REFRELE_NOTR(ire);
523 
524 	/* Remove from all hashes. */
525 	sctp_bind_hash_remove(sctp);
526 	sctp_conn_hash_remove(sctp);
527 	sctp_listen_hash_remove(sctp);
528 	sctp->sctp_state = SCTPS_IDLE;
529 
530 	/*
531 	 * Clean up the recvq as much as possible.  All those packets
532 	 * will be silently dropped as this sctp_t is now in idle state.
533 	 */
534 	mutex_enter(&sctp->sctp_recvq_lock);
535 	while ((mp = sctp->sctp_recvq) != NULL) {
536 		mblk_t *ipsec_mp;
537 
538 		sctp->sctp_recvq = mp->b_next;
539 		mp->b_next = NULL;
540 		if ((ipsec_mp = mp->b_prev) != NULL) {
541 			freeb(ipsec_mp);
542 			mp->b_prev = NULL;
543 		}
544 		freemsg(mp);
545 	}
546 	mutex_exit(&sctp->sctp_recvq_lock);
547 }
548 
549 /*
550  * Free memory associated with the sctp/ip header template.
551  */
552 static void
553 sctp_headers_free(sctp_t *sctp)
554 {
555 	if (sctp->sctp_iphc != NULL) {
556 		kmem_free(sctp->sctp_iphc, sctp->sctp_iphc_len);
557 		sctp->sctp_iphc = NULL;
558 		sctp->sctp_ipha = NULL;
559 		sctp->sctp_hdr_len = 0;
560 		sctp->sctp_ip_hdr_len = 0;
561 		sctp->sctp_iphc_len = 0;
562 		sctp->sctp_sctph = NULL;
563 		sctp->sctp_hdr_len = 0;
564 	}
565 	if (sctp->sctp_iphc6 != NULL) {
566 		kmem_free(sctp->sctp_iphc6, sctp->sctp_iphc6_len);
567 		sctp->sctp_iphc6 = NULL;
568 		sctp->sctp_ip6h = NULL;
569 		sctp->sctp_hdr6_len = 0;
570 		sctp->sctp_ip_hdr6_len = 0;
571 		sctp->sctp_iphc6_len = 0;
572 		sctp->sctp_sctph6 = NULL;
573 		sctp->sctp_hdr6_len = 0;
574 	}
575 }
576 
577 static void
578 sctp_free_xmit_data(sctp_t *sctp)
579 {
580 	mblk_t	*ump = NULL;
581 	mblk_t	*nump;
582 	mblk_t	*mp;
583 	mblk_t	*nmp;
584 
585 	sctp->sctp_xmit_unacked = NULL;
586 	ump = sctp->sctp_xmit_head;
587 	sctp->sctp_xmit_tail = sctp->sctp_xmit_head = NULL;
588 free_unsent:
589 	for (; ump != NULL; ump = nump) {
590 		for (mp = ump->b_cont; mp != NULL; mp = nmp) {
591 			nmp = mp->b_next;
592 			mp->b_next = NULL;
593 			mp->b_prev = NULL;
594 			freemsg(mp);
595 		}
596 		ASSERT(DB_REF(ump) == 1);
597 		nump = ump->b_next;
598 		ump->b_next = NULL;
599 		ump->b_prev = NULL;
600 		ump->b_cont = NULL;
601 		freeb(ump);
602 	}
603 	if ((ump = sctp->sctp_xmit_unsent) == NULL) {
604 		ASSERT(sctp->sctp_xmit_unsent_tail == NULL);
605 		return;
606 	}
607 	sctp->sctp_xmit_unsent = sctp->sctp_xmit_unsent_tail = NULL;
608 	goto free_unsent;
609 }
610 
611 /*
612  * Cleanup all the messages in the stream queue and the reassembly lists.
613  * If 'free' is true, then delete the streams as well.
614  */
615 void
616 sctp_instream_cleanup(sctp_t *sctp, boolean_t free)
617 {
618 	int	i;
619 	mblk_t	*mp;
620 	mblk_t	*mp1;
621 
622 	if (sctp->sctp_instr != NULL) {
623 		/* walk thru and flush out anything remaining in the Q */
624 		for (i = 0; i < sctp->sctp_num_istr; i++) {
625 			mp = sctp->sctp_instr[i].istr_msgs;
626 			while (mp != NULL) {
627 				mp1 = mp->b_next;
628 				mp->b_next = mp->b_prev = NULL;
629 				freemsg(mp);
630 				mp = mp1;
631 			}
632 			sctp->sctp_instr[i].istr_msgs = NULL;
633 			sctp->sctp_instr[i].istr_nmsgs = 0;
634 			sctp_free_reass((sctp->sctp_instr) + i);
635 			sctp->sctp_instr[i].nextseq = 0;
636 		}
637 		if (free) {
638 			kmem_free(sctp->sctp_instr,
639 			    sizeof (*sctp->sctp_instr) * sctp->sctp_num_istr);
640 			sctp->sctp_instr = NULL;
641 			sctp->sctp_num_istr = 0;
642 		}
643 	}
644 	/* un-ordered fragments */
645 	if (sctp->sctp_uo_frags != NULL) {
646 		for (mp = sctp->sctp_uo_frags; mp != NULL; mp = mp1) {
647 			mp1 = mp->b_next;
648 			mp->b_next = mp->b_prev = NULL;
649 			freemsg(mp);
650 		}
651 	}
652 }
653 
654 /*
655  * Last reference to the sctp_t is gone. Free all memory associated with it.
656  * Called from SCTP_REFRELE. Called inline in sctp_close()
657  */
658 void
659 sctp_free(conn_t *connp)
660 {
661 	sctp_t *sctp = CONN2SCTP(connp);
662 	int		cnt;
663 	sctp_stack_t	*sctps = sctp->sctp_sctps;
664 	netstack_t	*ns;
665 
666 	ASSERT(sctps != NULL);
667 	/* Unlink it from the global list */
668 	SCTP_UNLINK(sctp, sctps);
669 
670 	ASSERT(connp->conn_ref == 0);
671 	ASSERT(connp->conn_ulp == IPPROTO_SCTP);
672 	ASSERT(!MUTEX_HELD(&sctp->sctp_reflock));
673 	ASSERT(sctp->sctp_refcnt == 0);
674 
675 	ASSERT(sctp->sctp_ptpbhn == NULL && sctp->sctp_bind_hash == NULL);
676 	ASSERT(sctp->sctp_conn_hash_next == NULL &&
677 	    sctp->sctp_conn_hash_prev == NULL);
678 
679 
680 	/* Free up all the resources. */
681 
682 	/* blow away sctp stream management */
683 	if (sctp->sctp_ostrcntrs != NULL) {
684 		kmem_free(sctp->sctp_ostrcntrs,
685 		    sizeof (uint16_t) * sctp->sctp_num_ostr);
686 		sctp->sctp_ostrcntrs = NULL;
687 	}
688 	sctp_instream_cleanup(sctp, B_TRUE);
689 
690 	/* Remove all data transfer resources. */
691 	sctp->sctp_istr_nmsgs = 0;
692 	sctp->sctp_rxqueued = 0;
693 	sctp_free_xmit_data(sctp);
694 	sctp->sctp_unacked = 0;
695 	sctp->sctp_unsent = 0;
696 	if (sctp->sctp_cxmit_list != NULL)
697 		sctp_asconf_free_cxmit(sctp, NULL);
698 
699 	sctp->sctp_lastdata = NULL;
700 
701 	/* Clear out default xmit settings */
702 	sctp->sctp_def_stream = 0;
703 	sctp->sctp_def_flags = 0;
704 	sctp->sctp_def_ppid = 0;
705 	sctp->sctp_def_context = 0;
706 	sctp->sctp_def_timetolive = 0;
707 
708 	if (sctp->sctp_sack_info != NULL) {
709 		sctp_free_set(sctp->sctp_sack_info);
710 		sctp->sctp_sack_info = NULL;
711 	}
712 	sctp->sctp_sack_gaps = 0;
713 
714 	if (sctp->sctp_cookie_mp != NULL) {
715 		freemsg(sctp->sctp_cookie_mp);
716 		sctp->sctp_cookie_mp = NULL;
717 	}
718 
719 	/* Remove all the address resources. */
720 	sctp_zap_addrs(sctp);
721 	for (cnt = 0; cnt < SCTP_IPIF_HASH; cnt++) {
722 		ASSERT(sctp->sctp_saddrs[cnt].ipif_count == 0);
723 		list_destroy(&sctp->sctp_saddrs[cnt].sctp_ipif_list);
724 	}
725 
726 	ip6_pkt_free(&sctp->sctp_sticky_ipp);
727 
728 	if (sctp->sctp_hopopts != NULL) {
729 		mi_free(sctp->sctp_hopopts);
730 		sctp->sctp_hopopts = NULL;
731 		sctp->sctp_hopoptslen = 0;
732 	}
733 	ASSERT(sctp->sctp_hopoptslen == 0);
734 	if (sctp->sctp_dstopts != NULL) {
735 		mi_free(sctp->sctp_dstopts);
736 		sctp->sctp_dstopts = NULL;
737 		sctp->sctp_dstoptslen = 0;
738 	}
739 	ASSERT(sctp->sctp_dstoptslen == 0);
740 	if (sctp->sctp_rtdstopts != NULL) {
741 		mi_free(sctp->sctp_rtdstopts);
742 		sctp->sctp_rtdstopts = NULL;
743 		sctp->sctp_rtdstoptslen = 0;
744 	}
745 	ASSERT(sctp->sctp_rtdstoptslen == 0);
746 	if (sctp->sctp_rthdr != NULL) {
747 		mi_free(sctp->sctp_rthdr);
748 		sctp->sctp_rthdr = NULL;
749 		sctp->sctp_rthdrlen = 0;
750 	}
751 	ASSERT(sctp->sctp_rthdrlen == 0);
752 	sctp_headers_free(sctp);
753 
754 	sctp->sctp_shutdown_faddr = NULL;
755 
756 	/* Clear all the bitfields. */
757 	bzero(&sctp->sctp_bits, sizeof (sctp->sctp_bits));
758 
759 	/* It is time to update the global statistics. */
760 	UPDATE_MIB(&sctps->sctps_mib, sctpOutSCTPPkts, sctp->sctp_opkts);
761 	UPDATE_MIB(&sctps->sctps_mib, sctpOutCtrlChunks, sctp->sctp_obchunks);
762 	UPDATE_MIB(&sctps->sctps_mib, sctpOutOrderChunks, sctp->sctp_odchunks);
763 	UPDATE_MIB(&sctps->sctps_mib,
764 	    sctpOutUnorderChunks, sctp->sctp_oudchunks);
765 	UPDATE_MIB(&sctps->sctps_mib, sctpRetransChunks, sctp->sctp_rxtchunks);
766 	UPDATE_MIB(&sctps->sctps_mib, sctpInSCTPPkts, sctp->sctp_ipkts);
767 	UPDATE_MIB(&sctps->sctps_mib, sctpInCtrlChunks, sctp->sctp_ibchunks);
768 	UPDATE_MIB(&sctps->sctps_mib, sctpInOrderChunks, sctp->sctp_idchunks);
769 	UPDATE_MIB(&sctps->sctps_mib,
770 	    sctpInUnorderChunks, sctp->sctp_iudchunks);
771 	UPDATE_MIB(&sctps->sctps_mib, sctpFragUsrMsgs, sctp->sctp_fragdmsgs);
772 	UPDATE_MIB(&sctps->sctps_mib, sctpReasmUsrMsgs, sctp->sctp_reassmsgs);
773 	sctp->sctp_opkts = 0;
774 	sctp->sctp_obchunks = 0;
775 	sctp->sctp_odchunks = 0;
776 	sctp->sctp_oudchunks = 0;
777 	sctp->sctp_rxtchunks = 0;
778 	sctp->sctp_ipkts = 0;
779 	sctp->sctp_ibchunks = 0;
780 	sctp->sctp_idchunks = 0;
781 	sctp->sctp_iudchunks = 0;
782 	sctp->sctp_fragdmsgs = 0;
783 	sctp->sctp_reassmsgs = 0;
784 
785 	sctp->sctp_autoclose = 0;
786 	sctp->sctp_tx_adaption_code = 0;
787 
788 	sctp->sctp_v6label_len = 0;
789 	sctp->sctp_v4label_len = 0;
790 
791 	/* Clean up conn_t stuff */
792 	connp->conn_policy_cached = B_FALSE;
793 	if (connp->conn_latch != NULL) {
794 		IPLATCH_REFRELE(connp->conn_latch, connp->conn_netstack);
795 		connp->conn_latch = NULL;
796 	}
797 	if (connp->conn_policy != NULL) {
798 		IPPH_REFRELE(connp->conn_policy, connp->conn_netstack);
799 		connp->conn_policy = NULL;
800 	}
801 	if (connp->conn_ipsec_opt_mp != NULL) {
802 		freemsg(connp->conn_ipsec_opt_mp);
803 		connp->conn_ipsec_opt_mp = NULL;
804 	}
805 	if (connp->conn_cred != NULL) {
806 		crfree(connp->conn_cred);
807 		connp->conn_cred = NULL;
808 	}
809 
810 	/* Every sctp_t holds one reference on the default queue */
811 	sctp->sctp_sctps = NULL;
812 	SCTP_G_Q_REFRELE(sctps);
813 
814 	ns = connp->conn_netstack;
815 	connp->conn_netstack = NULL;
816 	netstack_rele(ns);
817 	kmem_cache_free(sctp_conn_cache, connp);
818 }
819 
820 /* Diagnostic routine used to return a string associated with the sctp state. */
821 char *
822 sctp_display(sctp_t *sctp, char *sup_buf)
823 {
824 	char	*buf;
825 	char	buf1[30];
826 	static char	priv_buf[INET6_ADDRSTRLEN * 2 + 80];
827 	char	*cp;
828 
829 	if (sctp == NULL)
830 		return ("NULL_SCTP");
831 
832 	buf = (sup_buf != NULL) ? sup_buf : priv_buf;
833 
834 	switch (sctp->sctp_state) {
835 	case SCTPS_IDLE:
836 		cp = "SCTP_IDLE";
837 		break;
838 	case SCTPS_BOUND:
839 		cp = "SCTP_BOUND";
840 		break;
841 	case SCTPS_LISTEN:
842 		cp = "SCTP_LISTEN";
843 		break;
844 	case SCTPS_COOKIE_WAIT:
845 		cp = "SCTP_COOKIE_WAIT";
846 		break;
847 	case SCTPS_COOKIE_ECHOED:
848 		cp = "SCTP_COOKIE_ECHOED";
849 		break;
850 	case SCTPS_ESTABLISHED:
851 		cp = "SCTP_ESTABLISHED";
852 		break;
853 	case SCTPS_SHUTDOWN_PENDING:
854 		cp = "SCTP_SHUTDOWN_PENDING";
855 		break;
856 	case SCTPS_SHUTDOWN_SENT:
857 		cp = "SCTPS_SHUTDOWN_SENT";
858 		break;
859 	case SCTPS_SHUTDOWN_RECEIVED:
860 		cp = "SCTPS_SHUTDOWN_RECEIVED";
861 		break;
862 	case SCTPS_SHUTDOWN_ACK_SENT:
863 		cp = "SCTPS_SHUTDOWN_ACK_SENT";
864 		break;
865 	default:
866 		(void) mi_sprintf(buf1, "SCTPUnkState(%d)", sctp->sctp_state);
867 		cp = buf1;
868 		break;
869 	}
870 	(void) mi_sprintf(buf, "[%u, %u] %s",
871 	    ntohs(sctp->sctp_lport), ntohs(sctp->sctp_fport), cp);
872 
873 	return (buf);
874 }
875 
876 /*
877  * Initialize protocol control block. If a parent exists, inherit
878  * all values set through setsockopt().
879  */
880 static int
881 sctp_init_values(sctp_t *sctp, sctp_t *psctp, int sleep)
882 {
883 	int	err;
884 	int	cnt;
885 	sctp_stack_t	*sctps = sctp->sctp_sctps;
886 	conn_t 	*connp, *pconnp;
887 
888 	ASSERT((sctp->sctp_family == AF_INET &&
889 	    sctp->sctp_ipversion == IPV4_VERSION) ||
890 	    (sctp->sctp_family == AF_INET6 &&
891 	    (sctp->sctp_ipversion == IPV4_VERSION ||
892 	    sctp->sctp_ipversion == IPV6_VERSION)));
893 
894 	sctp->sctp_nsaddrs = 0;
895 	for (cnt = 0; cnt < SCTP_IPIF_HASH; cnt++) {
896 		sctp->sctp_saddrs[cnt].ipif_count = 0;
897 		list_create(&sctp->sctp_saddrs[cnt].sctp_ipif_list,
898 		    sizeof (sctp_saddr_ipif_t), offsetof(sctp_saddr_ipif_t,
899 		    saddr_ipif));
900 	}
901 	sctp->sctp_ports = 0;
902 	sctp->sctp_running = B_FALSE;
903 	sctp->sctp_state = SCTPS_IDLE;
904 
905 	sctp->sctp_refcnt = 1;
906 
907 	sctp->sctp_strikes = 0;
908 
909 	sctp->sctp_last_mtu_probe = lbolt64;
910 	sctp->sctp_mtu_probe_intvl = sctps->sctps_mtu_probe_interval;
911 
912 	sctp->sctp_sack_gaps = 0;
913 	sctp->sctp_sack_toggle = 2;
914 
915 	if (psctp != NULL) {
916 		/*
917 		 * Inherit from parent
918 		 */
919 		sctp->sctp_iphc = kmem_zalloc(psctp->sctp_iphc_len,
920 		    KM_NOSLEEP);
921 		if (sctp->sctp_iphc == NULL)
922 			return (ENOMEM);
923 		sctp->sctp_iphc_len = psctp->sctp_iphc_len;
924 		sctp->sctp_hdr_len = psctp->sctp_hdr_len;
925 
926 		sctp->sctp_iphc6 = kmem_zalloc(psctp->sctp_iphc6_len,
927 		    KM_NOSLEEP);
928 		if (sctp->sctp_iphc6 == NULL) {
929 			sctp->sctp_iphc6_len = 0;
930 			return (ENOMEM);
931 		}
932 		sctp->sctp_iphc6_len = psctp->sctp_iphc6_len;
933 		sctp->sctp_hdr6_len = psctp->sctp_hdr6_len;
934 
935 		sctp->sctp_ip_hdr_len = psctp->sctp_ip_hdr_len;
936 		sctp->sctp_ip_hdr6_len = psctp->sctp_ip_hdr6_len;
937 
938 		/*
939 		 * Copy the IP+SCTP header templates from listener
940 		 */
941 		bcopy(psctp->sctp_iphc, sctp->sctp_iphc,
942 		    psctp->sctp_hdr_len);
943 		sctp->sctp_ipha = (ipha_t *)sctp->sctp_iphc;
944 		sctp->sctp_sctph = (sctp_hdr_t *)(sctp->sctp_iphc +
945 		    sctp->sctp_ip_hdr_len);
946 
947 		bcopy(psctp->sctp_iphc6, sctp->sctp_iphc6,
948 		    psctp->sctp_hdr6_len);
949 		if (((ip6i_t *)(sctp->sctp_iphc6))->ip6i_nxt == IPPROTO_RAW) {
950 			sctp->sctp_ip6h = (ip6_t *)(sctp->sctp_iphc6 +
951 			    sizeof (ip6i_t));
952 		} else {
953 			sctp->sctp_ip6h = (ip6_t *)sctp->sctp_iphc6;
954 		}
955 		sctp->sctp_sctph6 = (sctp_hdr_t *)(sctp->sctp_iphc6 +
956 		    sctp->sctp_ip_hdr6_len);
957 
958 		sctp->sctp_cookie_lifetime = psctp->sctp_cookie_lifetime;
959 		sctp->sctp_xmit_lowater = psctp->sctp_xmit_lowater;
960 		sctp->sctp_xmit_hiwater = psctp->sctp_xmit_hiwater;
961 		sctp->sctp_cwnd_max = psctp->sctp_cwnd_max;
962 		sctp->sctp_rwnd = psctp->sctp_rwnd;
963 		sctp->sctp_irwnd = psctp->sctp_rwnd;
964 		sctp->sctp_pd_point = psctp->sctp_pd_point;
965 		sctp->sctp_rto_max = psctp->sctp_rto_max;
966 		sctp->sctp_init_rto_max = psctp->sctp_init_rto_max;
967 		sctp->sctp_rto_min = psctp->sctp_rto_min;
968 		sctp->sctp_rto_initial = psctp->sctp_rto_initial;
969 		sctp->sctp_pa_max_rxt = psctp->sctp_pa_max_rxt;
970 		sctp->sctp_pp_max_rxt = psctp->sctp_pp_max_rxt;
971 		sctp->sctp_max_init_rxt = psctp->sctp_max_init_rxt;
972 
973 		sctp->sctp_def_stream = psctp->sctp_def_stream;
974 		sctp->sctp_def_flags = psctp->sctp_def_flags;
975 		sctp->sctp_def_ppid = psctp->sctp_def_ppid;
976 		sctp->sctp_def_context = psctp->sctp_def_context;
977 		sctp->sctp_def_timetolive = psctp->sctp_def_timetolive;
978 
979 		sctp->sctp_num_istr = psctp->sctp_num_istr;
980 		sctp->sctp_num_ostr = psctp->sctp_num_ostr;
981 
982 		sctp->sctp_hb_interval = psctp->sctp_hb_interval;
983 		sctp->sctp_autoclose = psctp->sctp_autoclose;
984 		sctp->sctp_tx_adaption_code = psctp->sctp_tx_adaption_code;
985 
986 		/* xxx should be a better way to copy these flags xxx */
987 		sctp->sctp_debug = psctp->sctp_debug;
988 		sctp->sctp_bound_to_all = psctp->sctp_bound_to_all;
989 		sctp->sctp_cansleep = psctp->sctp_cansleep;
990 		sctp->sctp_send_adaption = psctp->sctp_send_adaption;
991 		sctp->sctp_ndelay = psctp->sctp_ndelay;
992 		sctp->sctp_events = psctp->sctp_events;
993 		sctp->sctp_ipv6_recvancillary = psctp->sctp_ipv6_recvancillary;
994 
995 		/* Copy IP-layer options */
996 		connp = sctp->sctp_connp;
997 		pconnp = psctp->sctp_connp;
998 
999 		connp->conn_broadcast = pconnp->conn_broadcast;
1000 		connp->conn_loopback = pconnp->conn_loopback;
1001 		connp->conn_dontroute = pconnp->conn_dontroute;
1002 		connp->conn_reuseaddr = pconnp->conn_reuseaddr;
1003 
1004 	} else {
1005 		/*
1006 		 * Initialize the header template
1007 		 */
1008 		if ((err = sctp_header_init_ipv4(sctp, sleep)) != 0) {
1009 			return (err);
1010 		}
1011 		if ((err = sctp_header_init_ipv6(sctp, sleep)) != 0) {
1012 			return (err);
1013 		}
1014 
1015 		/*
1016 		 * Set to system defaults
1017 		 */
1018 		sctp->sctp_cookie_lifetime =
1019 		    MSEC_TO_TICK(sctps->sctps_cookie_life);
1020 		sctp->sctp_xmit_lowater = sctps->sctps_xmit_lowat;
1021 		sctp->sctp_xmit_hiwater = sctps->sctps_xmit_hiwat;
1022 		sctp->sctp_cwnd_max = sctps->sctps_cwnd_max_;
1023 		sctp->sctp_rwnd = sctps->sctps_recv_hiwat;
1024 		sctp->sctp_irwnd = sctp->sctp_rwnd;
1025 		sctp->sctp_pd_point = sctp->sctp_rwnd;
1026 		sctp->sctp_rto_max = MSEC_TO_TICK(sctps->sctps_rto_maxg);
1027 		sctp->sctp_init_rto_max = sctp->sctp_rto_max;
1028 		sctp->sctp_rto_min = MSEC_TO_TICK(sctps->sctps_rto_ming);
1029 		sctp->sctp_rto_initial = MSEC_TO_TICK(
1030 		    sctps->sctps_rto_initialg);
1031 		sctp->sctp_pa_max_rxt = sctps->sctps_pa_max_retr;
1032 		sctp->sctp_pp_max_rxt = sctps->sctps_pp_max_retr;
1033 		sctp->sctp_max_init_rxt = sctps->sctps_max_init_retr;
1034 
1035 		sctp->sctp_num_istr = sctps->sctps_max_in_streams;
1036 		sctp->sctp_num_ostr = sctps->sctps_initial_out_streams;
1037 
1038 		sctp->sctp_hb_interval =
1039 		    MSEC_TO_TICK(sctps->sctps_heartbeat_interval);
1040 	}
1041 	sctp->sctp_understands_asconf = B_TRUE;
1042 	sctp->sctp_understands_addip = B_TRUE;
1043 	sctp->sctp_prsctp_aware = B_FALSE;
1044 
1045 	sctp->sctp_connp->conn_ref = 1;
1046 	sctp->sctp_connp->conn_fully_bound = B_FALSE;
1047 
1048 	sctp->sctp_prsctpdrop = 0;
1049 	sctp->sctp_msgcount = 0;
1050 
1051 	return (0);
1052 }
1053 
1054 /*
1055  * Extracts the init tag from an INIT chunk and checks if it matches
1056  * the sctp's verification tag. Returns 0 if it doesn't match, 1 if
1057  * it does.
1058  */
1059 static boolean_t
1060 sctp_icmp_verf(sctp_t *sctp, sctp_hdr_t *sh, mblk_t *mp)
1061 {
1062 	sctp_chunk_hdr_t *sch;
1063 	uint32_t verf, *vp;
1064 
1065 	sch = (sctp_chunk_hdr_t *)(sh + 1);
1066 	vp = (uint32_t *)(sch + 1);
1067 
1068 	/* Need at least the data chunk hdr and the first 4 bytes of INIT */
1069 	if ((unsigned char *)(vp + 1) > mp->b_wptr) {
1070 		return (B_FALSE);
1071 	}
1072 
1073 	bcopy(vp, &verf, sizeof (verf));
1074 
1075 	if (verf == sctp->sctp_lvtag) {
1076 		return (B_TRUE);
1077 	}
1078 	return (B_FALSE);
1079 }
1080 
1081 /*
1082  * sctp_icmp_error is called by sctp_input() to process ICMP error messages
1083  * passed up by IP.  The queue is the default queue.  We need to find a sctp_t
1084  * that corresponds to the returned datagram.  Passes the message back in on
1085  * the correct queue once it has located the connection.
1086  * Assumes that IP has pulled up everything up to and including
1087  * the ICMP header.
1088  */
1089 void
1090 sctp_icmp_error(sctp_t *sctp, mblk_t *mp)
1091 {
1092 	icmph_t *icmph;
1093 	ipha_t	*ipha;
1094 	int	iph_hdr_length;
1095 	sctp_hdr_t *sctph;
1096 	mblk_t *first_mp;
1097 	uint32_t new_mtu;
1098 	in6_addr_t dst;
1099 	sctp_faddr_t *fp;
1100 	sctp_stack_t	*sctps = sctp->sctp_sctps;
1101 
1102 	dprint(1, ("sctp_icmp_error: sctp=%p, mp=%p\n", (void *)sctp,
1103 	    (void *)mp));
1104 
1105 	first_mp = mp;
1106 
1107 	ipha = (ipha_t *)mp->b_rptr;
1108 	if (IPH_HDR_VERSION(ipha) != IPV4_VERSION) {
1109 		ASSERT(IPH_HDR_VERSION(ipha) == IPV6_VERSION);
1110 		sctp_icmp_error_ipv6(sctp, first_mp);
1111 		return;
1112 	}
1113 
1114 	iph_hdr_length = IPH_HDR_LENGTH(ipha);
1115 	icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length];
1116 	ipha = (ipha_t *)&icmph[1];
1117 	iph_hdr_length = IPH_HDR_LENGTH(ipha);
1118 	sctph = (sctp_hdr_t *)((char *)ipha + iph_hdr_length);
1119 	if ((uchar_t *)(sctph + 1) >= mp->b_wptr) {
1120 		/* not enough data for SCTP header */
1121 		freemsg(first_mp);
1122 		return;
1123 	}
1124 
1125 	switch (icmph->icmph_type) {
1126 	case ICMP_DEST_UNREACHABLE:
1127 		switch (icmph->icmph_code) {
1128 		case ICMP_FRAGMENTATION_NEEDED:
1129 			/*
1130 			 * Reduce the MSS based on the new MTU.  This will
1131 			 * eliminate any fragmentation locally.
1132 			 * N.B.  There may well be some funny side-effects on
1133 			 * the local send policy and the remote receive policy.
1134 			 * Pending further research, we provide
1135 			 * sctp_ignore_path_mtu just in case this proves
1136 			 * disastrous somewhere.
1137 			 *
1138 			 * After updating the MSS, retransmit part of the
1139 			 * dropped segment using the new mss by calling
1140 			 * sctp_wput_slow().  Need to adjust all those
1141 			 * params to make sure sctp_wput_slow() work properly.
1142 			 */
1143 			if (sctps->sctps_ignore_path_mtu)
1144 				break;
1145 
1146 			/* find the offending faddr */
1147 			IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, &dst);
1148 			fp = sctp_lookup_faddr(sctp, &dst);
1149 			if (fp == NULL) {
1150 				break;
1151 			}
1152 
1153 			new_mtu = ntohs(icmph->icmph_du_mtu);
1154 
1155 			if (new_mtu - sctp->sctp_hdr_len >= fp->sfa_pmss)
1156 				break;
1157 
1158 			/*
1159 			 * Make sure that sfa_pmss is a multiple of
1160 			 * SCTP_ALIGN.
1161 			 */
1162 			fp->sfa_pmss = (new_mtu - sctp->sctp_hdr_len) &
1163 			    ~(SCTP_ALIGN - 1);
1164 			fp->pmtu_discovered = 1;
1165 
1166 			break;
1167 		case ICMP_PORT_UNREACHABLE:
1168 		case ICMP_PROTOCOL_UNREACHABLE:
1169 			switch (sctp->sctp_state) {
1170 			case SCTPS_COOKIE_WAIT:
1171 			case SCTPS_COOKIE_ECHOED:
1172 				/* make sure the verification tag matches */
1173 				if (!sctp_icmp_verf(sctp, sctph, mp)) {
1174 					break;
1175 				}
1176 				BUMP_MIB(&sctps->sctps_mib, sctpAborted);
1177 				sctp_assoc_event(sctp, SCTP_CANT_STR_ASSOC, 0,
1178 				    NULL);
1179 				sctp_clean_death(sctp, ECONNREFUSED);
1180 				break;
1181 			}
1182 			break;
1183 		case ICMP_HOST_UNREACHABLE:
1184 		case ICMP_NET_UNREACHABLE:
1185 			/* Record the error in case we finally time out. */
1186 			sctp->sctp_client_errno = (icmph->icmph_code ==
1187 			    ICMP_HOST_UNREACHABLE) ? EHOSTUNREACH : ENETUNREACH;
1188 			break;
1189 		default:
1190 			break;
1191 		}
1192 		break;
1193 	case ICMP_SOURCE_QUENCH: {
1194 		/* Reduce the sending rate as if we got a retransmit timeout */
1195 		break;
1196 	}
1197 	}
1198 	freemsg(first_mp);
1199 }
1200 
1201 /*
1202  * sctp_icmp_error_ipv6() is called by sctp_icmp_error() to process ICMPv6
1203  * error messages passed up by IP.
1204  * Assumes that IP has pulled up all the extension headers as well
1205  * as the ICMPv6 header.
1206  */
1207 static void
1208 sctp_icmp_error_ipv6(sctp_t *sctp, mblk_t *mp)
1209 {
1210 	icmp6_t *icmp6;
1211 	ip6_t	*ip6h;
1212 	uint16_t	iph_hdr_length;
1213 	sctp_hdr_t *sctpha;
1214 	uint8_t	*nexthdrp;
1215 	uint32_t new_mtu;
1216 	sctp_faddr_t *fp;
1217 	sctp_stack_t	*sctps = sctp->sctp_sctps;
1218 
1219 	ip6h = (ip6_t *)mp->b_rptr;
1220 	iph_hdr_length = (ip6h->ip6_nxt != IPPROTO_SCTP) ?
1221 	    ip_hdr_length_v6(mp, ip6h) : IPV6_HDR_LEN;
1222 
1223 	icmp6 = (icmp6_t *)&mp->b_rptr[iph_hdr_length];
1224 	ip6h = (ip6_t *)&icmp6[1];
1225 	if (!ip_hdr_length_nexthdr_v6(mp, ip6h, &iph_hdr_length, &nexthdrp)) {
1226 		freemsg(mp);
1227 		return;
1228 	}
1229 	ASSERT(*nexthdrp == IPPROTO_SCTP);
1230 
1231 	/* XXX need ifindex to find connection */
1232 	sctpha = (sctp_hdr_t *)((char *)ip6h + iph_hdr_length);
1233 	if ((uchar_t *)sctpha >= mp->b_wptr) {
1234 		/* not enough data for SCTP header */
1235 		freemsg(mp);
1236 		return;
1237 	}
1238 	switch (icmp6->icmp6_type) {
1239 	case ICMP6_PACKET_TOO_BIG:
1240 		/*
1241 		 * Reduce the MSS based on the new MTU.  This will
1242 		 * eliminate any fragmentation locally.
1243 		 * N.B.  There may well be some funny side-effects on
1244 		 * the local send policy and the remote receive policy.
1245 		 * Pending further research, we provide
1246 		 * sctp_ignore_path_mtu just in case this proves
1247 		 * disastrous somewhere.
1248 		 *
1249 		 * After updating the MSS, retransmit part of the
1250 		 * dropped segment using the new mss by calling
1251 		 * sctp_wput_slow().  Need to adjust all those
1252 		 * params to make sure sctp_wput_slow() work properly.
1253 		 */
1254 		if (sctps->sctps_ignore_path_mtu)
1255 			break;
1256 
1257 		/* find the offending faddr */
1258 		fp = sctp_lookup_faddr(sctp, &ip6h->ip6_dst);
1259 		if (fp == NULL) {
1260 			break;
1261 		}
1262 
1263 		new_mtu = ntohs(icmp6->icmp6_mtu);
1264 
1265 		if (new_mtu - sctp->sctp_hdr6_len >= fp->sfa_pmss)
1266 			break;
1267 
1268 		/* Make sure that sfa_pmss is a multiple of SCTP_ALIGN. */
1269 		fp->sfa_pmss = (new_mtu - sctp->sctp_hdr6_len) &
1270 		    ~(SCTP_ALIGN - 1);
1271 		fp->pmtu_discovered = 1;
1272 
1273 		break;
1274 
1275 	case ICMP6_DST_UNREACH:
1276 		switch (icmp6->icmp6_code) {
1277 		case ICMP6_DST_UNREACH_NOPORT:
1278 			/* make sure the verification tag matches */
1279 			if (!sctp_icmp_verf(sctp, sctpha, mp)) {
1280 				break;
1281 			}
1282 			if (sctp->sctp_state == SCTPS_COOKIE_WAIT ||
1283 			    sctp->sctp_state == SCTPS_COOKIE_ECHOED) {
1284 				BUMP_MIB(&sctps->sctps_mib, sctpAborted);
1285 				sctp_assoc_event(sctp, SCTP_CANT_STR_ASSOC, 0,
1286 				    NULL);
1287 				sctp_clean_death(sctp, ECONNREFUSED);
1288 			}
1289 			break;
1290 
1291 		case ICMP6_DST_UNREACH_ADMIN:
1292 		case ICMP6_DST_UNREACH_NOROUTE:
1293 		case ICMP6_DST_UNREACH_NOTNEIGHBOR:
1294 		case ICMP6_DST_UNREACH_ADDR:
1295 			/* Record the error in case we finally time out. */
1296 			sctp->sctp_client_errno = EHOSTUNREACH;
1297 			break;
1298 		default:
1299 			break;
1300 		}
1301 		break;
1302 
1303 	case ICMP6_PARAM_PROB:
1304 		/* If this corresponds to an ICMP_PROTOCOL_UNREACHABLE */
1305 		if (icmp6->icmp6_code == ICMP6_PARAMPROB_NEXTHEADER &&
1306 		    (uchar_t *)ip6h + icmp6->icmp6_pptr ==
1307 		    (uchar_t *)nexthdrp) {
1308 			/* make sure the verification tag matches */
1309 			if (!sctp_icmp_verf(sctp, sctpha, mp)) {
1310 				break;
1311 			}
1312 			if (sctp->sctp_state == SCTPS_COOKIE_WAIT) {
1313 				BUMP_MIB(&sctps->sctps_mib, sctpAborted);
1314 				sctp_assoc_event(sctp, SCTP_CANT_STR_ASSOC, 0,
1315 				    NULL);
1316 				sctp_clean_death(sctp, ECONNREFUSED);
1317 			}
1318 			break;
1319 		}
1320 		break;
1321 
1322 	case ICMP6_TIME_EXCEEDED:
1323 	default:
1324 		break;
1325 	}
1326 	freemsg(mp);
1327 }
1328 
1329 /*
1330  * Called by sockfs to create a new sctp instance.
1331  *
1332  * If parent pointer is passed in, inherit settings from it.
1333  */
1334 sctp_t *
1335 sctp_create(void *sctp_ulpd, sctp_t *parent, int family, int flags,
1336     const sctp_upcalls_t *sctp_upcalls, sctp_sockbuf_limits_t *sbl,
1337     cred_t *credp)
1338 {
1339 	sctp_t		*sctp, *psctp;
1340 	conn_t		*sctp_connp;
1341 	mblk_t		*ack_mp, *hb_mp;
1342 	int		sleep = flags & SCTP_CAN_BLOCK ? KM_SLEEP : KM_NOSLEEP;
1343 	zoneid_t	zoneid;
1344 	sctp_stack_t	*sctps;
1345 
1346 	/* User must supply a credential. */
1347 	if (credp == NULL)
1348 		return (NULL);
1349 
1350 	psctp = (sctp_t *)parent;
1351 	if (psctp != NULL) {
1352 		sctps = psctp->sctp_sctps;
1353 		/* Increase here to have common decrease at end */
1354 		netstack_hold(sctps->sctps_netstack);
1355 	} else {
1356 		netstack_t *ns;
1357 
1358 		ns = netstack_find_by_cred(credp);
1359 		ASSERT(ns != NULL);
1360 		sctps = ns->netstack_sctp;
1361 		ASSERT(sctps != NULL);
1362 
1363 		/*
1364 		 * For exclusive stacks we set the zoneid to zero
1365 		 * to make SCTP operate as if in the global zone.
1366 		 */
1367 		if (sctps->sctps_netstack->netstack_stackid !=
1368 		    GLOBAL_NETSTACKID)
1369 			zoneid = GLOBAL_ZONEID;
1370 		else
1371 			zoneid = crgetzoneid(credp);
1372 
1373 		/*
1374 		 * For stackid zero this is done from strplumb.c, but
1375 		 * non-zero stackids are handled here.
1376 		 */
1377 		if (sctps->sctps_g_q == NULL &&
1378 		    sctps->sctps_netstack->netstack_stackid !=
1379 		    GLOBAL_NETSTACKID) {
1380 			sctp_g_q_setup(sctps);
1381 		}
1382 	}
1383 	if ((sctp_connp = ipcl_conn_create(IPCL_SCTPCONN, sleep,
1384 	    sctps->sctps_netstack)) == NULL) {
1385 		netstack_rele(sctps->sctps_netstack);
1386 		SCTP_KSTAT(sctps, sctp_conn_create);
1387 		return (NULL);
1388 	}
1389 	/*
1390 	 * ipcl_conn_create did a netstack_hold. Undo the hold that was
1391 	 * done at top of sctp_create.
1392 	 */
1393 	netstack_rele(sctps->sctps_netstack);
1394 	sctp = CONN2SCTP(sctp_connp);
1395 	sctp->sctp_sctps = sctps;
1396 
1397 	sctp_connp->conn_ulp_labeled = is_system_labeled();
1398 	if ((ack_mp = sctp_timer_alloc(sctp, sctp_ack_timer)) == NULL ||
1399 	    (hb_mp = sctp_timer_alloc(sctp, sctp_heartbeat_timer)) == NULL) {
1400 		if (ack_mp != NULL)
1401 			freeb(ack_mp);
1402 		netstack_rele(sctp_connp->conn_netstack);
1403 		sctp_connp->conn_netstack = NULL;
1404 		sctp->sctp_sctps = NULL;
1405 		SCTP_G_Q_REFRELE(sctps);
1406 		kmem_cache_free(sctp_conn_cache, sctp_connp);
1407 		return (NULL);
1408 	}
1409 
1410 	sctp->sctp_ack_mp = ack_mp;
1411 	sctp->sctp_heartbeat_mp = hb_mp;
1412 
1413 	switch (family) {
1414 	case AF_INET6:
1415 		sctp_connp->conn_af_isv6 = B_TRUE;
1416 		sctp->sctp_ipversion = IPV6_VERSION;
1417 		sctp->sctp_family = AF_INET6;
1418 		break;
1419 
1420 	case AF_INET:
1421 		sctp_connp->conn_af_isv6 = B_FALSE;
1422 		sctp_connp->conn_pkt_isv6 = B_FALSE;
1423 		sctp->sctp_ipversion = IPV4_VERSION;
1424 		sctp->sctp_family = AF_INET;
1425 		break;
1426 	default:
1427 		ASSERT(0);
1428 		break;
1429 	}
1430 	if (sctp_init_values(sctp, psctp, sleep) != 0) {
1431 		freeb(ack_mp);
1432 		freeb(hb_mp);
1433 		netstack_rele(sctp_connp->conn_netstack);
1434 		sctp_connp->conn_netstack = NULL;
1435 		sctp->sctp_sctps = NULL;
1436 		SCTP_G_Q_REFRELE(sctps);
1437 		kmem_cache_free(sctp_conn_cache, sctp_connp);
1438 		return (NULL);
1439 	}
1440 	sctp->sctp_cansleep = ((flags & SCTP_CAN_BLOCK) == SCTP_CAN_BLOCK);
1441 
1442 	sctp->sctp_mss = sctps->sctps_initial_mtu - ((family == AF_INET6) ?
1443 	    sctp->sctp_hdr6_len : sctp->sctp_hdr_len);
1444 
1445 	if (psctp != NULL) {
1446 		RUN_SCTP(psctp);
1447 		/*
1448 		 * Inherit local address list, local port. Parent is either
1449 		 * in SCTPS_BOUND, or SCTPS_LISTEN state.
1450 		 */
1451 		ASSERT((psctp->sctp_state == SCTPS_BOUND) ||
1452 		    (psctp->sctp_state == SCTPS_LISTEN));
1453 		if (sctp_dup_saddrs(psctp, sctp, sleep)) {
1454 			WAKE_SCTP(psctp);
1455 			freeb(ack_mp);
1456 			freeb(hb_mp);
1457 			sctp_headers_free(sctp);
1458 			netstack_rele(sctps->sctps_netstack);
1459 			sctp_connp->conn_netstack = NULL;
1460 			sctp->sctp_sctps = NULL;
1461 			SCTP_G_Q_REFRELE(sctps);
1462 			kmem_cache_free(sctp_conn_cache, sctp_connp);
1463 			return (NULL);
1464 		}
1465 
1466 		/*
1467 		 * If the parent is specified, it'll be immediatelly
1468 		 * followed by sctp_connect(). So don't add this guy to
1469 		 * bind hash.
1470 		 */
1471 		sctp->sctp_lport = psctp->sctp_lport;
1472 		sctp->sctp_state = SCTPS_BOUND;
1473 		sctp->sctp_allzones = psctp->sctp_allzones;
1474 		sctp->sctp_zoneid = psctp->sctp_zoneid;
1475 		WAKE_SCTP(psctp);
1476 	} else {
1477 		sctp->sctp_zoneid = zoneid;
1478 	}
1479 
1480 	sctp_connp->conn_cred = credp;
1481 	crhold(credp);
1482 
1483 	/*
1484 	 * If the caller has the process-wide flag set, then default to MAC
1485 	 * exempt mode.  This allows read-down to unlabeled hosts.
1486 	 */
1487 	if (getpflags(NET_MAC_AWARE, credp) != 0)
1488 		sctp_connp->conn_mac_exempt = B_TRUE;
1489 
1490 	/* Initialize SCTP instance values,  our verf tag must never be 0 */
1491 	(void) random_get_pseudo_bytes((uint8_t *)&sctp->sctp_lvtag,
1492 	    sizeof (sctp->sctp_lvtag));
1493 	if (sctp->sctp_lvtag == 0)
1494 		sctp->sctp_lvtag = (uint32_t)gethrtime();
1495 	ASSERT(sctp->sctp_lvtag != 0);
1496 
1497 	sctp->sctp_ltsn = sctp->sctp_lvtag + 1;
1498 	sctp->sctp_lcsn = sctp->sctp_ltsn;
1499 	sctp->sctp_recovery_tsn = sctp->sctp_lastack_rxd = sctp->sctp_ltsn - 1;
1500 	sctp->sctp_adv_pap = sctp->sctp_lastack_rxd;
1501 
1502 	/* Information required by upper layer */
1503 	if (sctp_ulpd != NULL) {
1504 		sctp->sctp_ulpd = sctp_ulpd;
1505 
1506 		ASSERT(sctp_upcalls != NULL);
1507 		bcopy(sctp_upcalls, &sctp->sctp_upcalls,
1508 		    sizeof (sctp_upcalls_t));
1509 		ASSERT(sbl != NULL);
1510 		/* Fill in the socket buffer limits for sctpsockfs */
1511 		sbl->sbl_txlowat = sctp->sctp_xmit_lowater;
1512 		sbl->sbl_txbuf = sctp->sctp_xmit_hiwater;
1513 		sbl->sbl_rxbuf = sctp->sctp_rwnd;
1514 		sbl->sbl_rxlowat = SCTP_RECV_LOWATER;
1515 	}
1516 	/* If no sctp_ulpd, must be creating the default sctp */
1517 	ASSERT(sctp_ulpd != NULL || sctps->sctps_gsctp == NULL);
1518 
1519 	/* Insert this in the global list. */
1520 	SCTP_LINK(sctp, sctps);
1521 
1522 	return (sctp);
1523 }
1524 
1525 /*
1526  * Make sure we wait until the default queue is setup, yet allow
1527  * sctp_g_q_create() to open a SCTP stream.
1528  * We need to allow sctp_g_q_create() do do an open
1529  * of sctp, hence we compare curhread.
1530  * All others have to wait until the sctps_g_q has been
1531  * setup.
1532  */
1533 void
1534 sctp_g_q_setup(sctp_stack_t *sctps)
1535 {
1536 	mutex_enter(&sctps->sctps_g_q_lock);
1537 	if (sctps->sctps_g_q != NULL) {
1538 		mutex_exit(&sctps->sctps_g_q_lock);
1539 		return;
1540 	}
1541 	if (sctps->sctps_g_q_creator == NULL) {
1542 		/* This thread will set it up */
1543 		sctps->sctps_g_q_creator = curthread;
1544 		mutex_exit(&sctps->sctps_g_q_lock);
1545 		sctp_g_q_create(sctps);
1546 		mutex_enter(&sctps->sctps_g_q_lock);
1547 		ASSERT(sctps->sctps_g_q_creator == curthread);
1548 		sctps->sctps_g_q_creator = NULL;
1549 		cv_signal(&sctps->sctps_g_q_cv);
1550 		ASSERT(sctps->sctps_g_q != NULL);
1551 		mutex_exit(&sctps->sctps_g_q_lock);
1552 		return;
1553 	}
1554 	/* Everybody but the creator has to wait */
1555 	if (sctps->sctps_g_q_creator != curthread) {
1556 		while (sctps->sctps_g_q == NULL)
1557 			cv_wait(&sctps->sctps_g_q_cv, &sctps->sctps_g_q_lock);
1558 	}
1559 	mutex_exit(&sctps->sctps_g_q_lock);
1560 }
1561 
1562 major_t IP_MAJ;
1563 #define	IP	"ip"
1564 
1565 #define	SCTP6DEV		"/devices/pseudo/sctp6@0:sctp6"
1566 
1567 /*
1568  * Create a default sctp queue here instead of in strplumb
1569  */
1570 void
1571 sctp_g_q_create(sctp_stack_t *sctps)
1572 {
1573 	int error;
1574 	ldi_handle_t	lh = NULL;
1575 	ldi_ident_t	li = NULL;
1576 	int		rval;
1577 	cred_t		*cr;
1578 
1579 #ifdef NS_DEBUG
1580 	(void) printf("sctp_g_q_create()for stack %d\n",
1581 	    sctps->sctps_netstack->netstack_stackid);
1582 #endif
1583 
1584 	ASSERT(sctps->sctps_g_q_creator == curthread);
1585 
1586 	error = ldi_ident_from_major(IP_MAJ, &li);
1587 	if (error) {
1588 #ifdef DEBUG
1589 		printf("sctp_g_q_create: lyr ident get failed error %d\n",
1590 		    error);
1591 #endif
1592 		return;
1593 	}
1594 
1595 	cr = zone_get_kcred(netstackid_to_zoneid(
1596 	    sctps->sctps_netstack->netstack_stackid));
1597 	ASSERT(cr != NULL);
1598 	/*
1599 	 * We set the sctp default queue to IPv6 because IPv4 falls
1600 	 * back to IPv6 when it can't find a client, but
1601 	 * IPv6 does not fall back to IPv4.
1602 	 */
1603 	error = ldi_open_by_name(SCTP6DEV, FREAD|FWRITE, cr, &lh, li);
1604 	if (error) {
1605 #ifdef DEBUG
1606 		printf("sctp_g_q_create: open of SCTP6DEV failed error %d\n",
1607 		    error);
1608 #endif
1609 		goto out;
1610 	}
1611 
1612 	/*
1613 	 * This ioctl causes the sctp framework to cache a pointer to
1614 	 * this stream, so we don't want to close the stream after
1615 	 * this operation.
1616 	 * Use the kernel credentials that are for the zone we're in.
1617 	 */
1618 	error = ldi_ioctl(lh, SCTP_IOC_DEFAULT_Q,
1619 	    (intptr_t)0, FKIOCTL, cr, &rval);
1620 	if (error) {
1621 #ifdef DEBUG
1622 		printf("sctp_g_q_create: ioctl SCTP_IOC_DEFAULT_Q failed "
1623 		    "error %d\n", error);
1624 #endif
1625 		goto out;
1626 	}
1627 	sctps->sctps_g_q_lh = lh;	/* For sctp_g_q_inactive */
1628 	lh = NULL;
1629 out:
1630 	/* Close layered handles */
1631 	if (li)
1632 		ldi_ident_release(li);
1633 	/* Keep cred around until _inactive needs it */
1634 	sctps->sctps_g_q_cr = cr;
1635 }
1636 
1637 /*
1638  * Remove the sctp_default queue so that new connections will not find it.
1639  * SCTP uses sctp_g_q for all transmission, so all sctp'ts implicitly
1640  * refer to it. Hence have each one have a reference on sctp_g_q_ref!
1641  *
1642  * We decrement the refcnt added in sctp_g_q_create. Once all the
1643  * sctp_t's which use the default go away, sctp_g_q_close will be called
1644  * and close the sctp_g_q. Once sctp_g_q is closed, sctp_close() will drop the
1645  * last reference count on the stack by calling netstack_rele().
1646  */
1647 void
1648 sctp_g_q_destroy(sctp_stack_t *sctps)
1649 {
1650 	if (sctps->sctps_g_q == NULL) {
1651 		return;	/* Nothing to cleanup */
1652 	}
1653 	/*
1654 	 * Keep sctps_g_q and sctps_gsctp until the last reference has
1655 	 * dropped, since the output is always done using those.
1656 	 * Need to decrement twice to take sctp_g_q_create and
1657 	 * the gsctp reference into account so that sctp_g_q_inactive is called
1658 	 * when all but the default queue remains.
1659 	 */
1660 #ifdef NS_DEBUG
1661 	(void) printf("sctp_g_q_destroy: ref %d\n",
1662 	    sctps->sctps_g_q_ref);
1663 #endif
1664 	SCTP_G_Q_REFRELE(sctps);
1665 }
1666 
1667 /*
1668  * Called when last user (could be sctp_g_q_destroy) drops reference count
1669  * using SCTP_G_Q_REFRELE.
1670  * Run by sctp_q_q_inactive using a taskq.
1671  */
1672 static void
1673 sctp_g_q_close(void *arg)
1674 {
1675 	sctp_stack_t *sctps = arg;
1676 	int error;
1677 	ldi_handle_t	lh = NULL;
1678 	ldi_ident_t	li = NULL;
1679 	cred_t		*cr;
1680 
1681 	lh = sctps->sctps_g_q_lh;
1682 	if (lh == NULL)
1683 		return;	/* Nothing to cleanup */
1684 
1685 	error = ldi_ident_from_major(IP_MAJ, &li);
1686 	if (error) {
1687 #ifdef NS_DEBUG
1688 		printf("sctp_g_q_inactive: lyr ident get failed error %d\n",
1689 		    error);
1690 #endif
1691 		return;
1692 	}
1693 
1694 	cr = sctps->sctps_g_q_cr;
1695 	sctps->sctps_g_q_cr = NULL;
1696 	ASSERT(cr != NULL);
1697 
1698 	/*
1699 	 * Make sure we can break the recursion when sctp_close decrements
1700 	 * the reference count causing g_q_inactive to be called again.
1701 	 */
1702 	sctps->sctps_g_q_lh = NULL;
1703 
1704 	/* close the default queue */
1705 	(void) ldi_close(lh, FREAD|FWRITE, cr);
1706 
1707 	/* Close layered handles */
1708 	ldi_ident_release(li);
1709 	crfree(cr);
1710 
1711 	ASSERT(sctps->sctps_g_q != NULL);
1712 	sctps->sctps_g_q = NULL;
1713 	/*
1714 	 * Now free sctps_gsctp.
1715 	 */
1716 	ASSERT(sctps->sctps_gsctp != NULL);
1717 	sctp_closei_local(sctps->sctps_gsctp);
1718 	SCTP_CONDEMNED(sctps->sctps_gsctp);
1719 	SCTP_REFRELE(sctps->sctps_gsctp);
1720 	sctps->sctps_gsctp = NULL;
1721 }
1722 
1723 /*
1724  * Called when last sctp_t drops reference count using SCTP_G_Q_REFRELE.
1725  *
1726  * Have to ensure that the ldi routines are not used by an
1727  * interrupt thread by using a taskq.
1728  */
1729 void
1730 sctp_g_q_inactive(sctp_stack_t *sctps)
1731 {
1732 	if (sctps->sctps_g_q_lh == NULL)
1733 		return;	/* Nothing to cleanup */
1734 
1735 	ASSERT(sctps->sctps_g_q_ref == 0);
1736 	SCTP_G_Q_REFHOLD(sctps); /* Compensate for what g_q_destroy did */
1737 
1738 	if (servicing_interrupt()) {
1739 		(void) taskq_dispatch(sctp_taskq, sctp_g_q_close,
1740 		    (void *) sctps, TQ_SLEEP);
1741 	} else {
1742 		sctp_g_q_close(sctps);
1743 	}
1744 }
1745 
1746 /* Run at module load time */
1747 void
1748 sctp_ddi_g_init(void)
1749 {
1750 	IP_MAJ = ddi_name_to_major(IP);
1751 
1752 	/* Create sctp_t/conn_t cache */
1753 	sctp_conn_cache_init();
1754 
1755 	/* Create the faddr cache */
1756 	sctp_faddr_init();
1757 
1758 	/* Create the sets cache */
1759 	sctp_sets_init();
1760 
1761 	/* Create the PR-SCTP sets cache */
1762 	sctp_ftsn_sets_init();
1763 
1764 	/* Initialize tables used for CRC calculation */
1765 	sctp_crc32_init();
1766 
1767 	sctp_taskq = taskq_create("sctp_taskq", 1, minclsyspri, 1, 1,
1768 	    TASKQ_PREPOPULATE);
1769 
1770 	/*
1771 	 * We want to be informed each time a stack is created or
1772 	 * destroyed in the kernel, so we can maintain the
1773 	 * set of sctp_stack_t's.
1774 	 */
1775 	netstack_register(NS_SCTP, sctp_stack_init, sctp_stack_shutdown,
1776 	    sctp_stack_fini);
1777 }
1778 
1779 static void *
1780 sctp_stack_init(netstackid_t stackid, netstack_t *ns)
1781 {
1782 	sctp_stack_t	*sctps;
1783 
1784 	sctps = kmem_zalloc(sizeof (*sctps), KM_SLEEP);
1785 	sctps->sctps_netstack = ns;
1786 
1787 	/* Initialize locks */
1788 	mutex_init(&sctps->sctps_g_q_lock, NULL, MUTEX_DEFAULT, NULL);
1789 	cv_init(&sctps->sctps_g_q_cv, NULL, CV_DEFAULT, NULL);
1790 	mutex_init(&sctps->sctps_g_lock, NULL, MUTEX_DEFAULT, NULL);
1791 	mutex_init(&sctps->sctps_epriv_port_lock, NULL, MUTEX_DEFAULT, NULL);
1792 	sctps->sctps_g_num_epriv_ports = SCTP_NUM_EPRIV_PORTS;
1793 	sctps->sctps_g_epriv_ports[0] = 2049;
1794 	sctps->sctps_g_epriv_ports[1] = 4045;
1795 
1796 	/* Initialize SCTP hash arrays. */
1797 	sctp_hash_init(sctps);
1798 
1799 	sctps->sctps_pad_mp = allocb(SCTP_ALIGN, BPRI_MED);
1800 	bzero(sctps->sctps_pad_mp->b_rptr, SCTP_ALIGN);
1801 	ASSERT(sctps->sctps_pad_mp);
1802 
1803 	if (!sctp_nd_init(sctps)) {
1804 		sctp_nd_free(sctps);
1805 	}
1806 
1807 	/* Initialize the recvq taskq. */
1808 	sctp_rq_tq_init(sctps);
1809 
1810 	/* saddr init */
1811 	sctp_saddr_init(sctps);
1812 
1813 	/* Global SCTP PCB list. */
1814 	list_create(&sctps->sctps_g_list, sizeof (sctp_t),
1815 	    offsetof(sctp_t, sctp_list));
1816 
1817 	/* Initialize sctp kernel stats. */
1818 	sctps->sctps_mibkp = sctp_kstat_init(stackid);
1819 	sctps->sctps_kstat =
1820 	    sctp_kstat2_init(stackid, &sctps->sctps_statistics);
1821 
1822 	return (sctps);
1823 }
1824 
1825 /*
1826  * Called when the module is about to be unloaded.
1827  */
1828 void
1829 sctp_ddi_g_destroy(void)
1830 {
1831 	/* Destroy sctp_t/conn_t caches */
1832 	sctp_conn_cache_fini();
1833 
1834 	/* Destroy the faddr cache */
1835 	sctp_faddr_fini();
1836 
1837 	/* Destroy the sets cache */
1838 	sctp_sets_fini();
1839 
1840 	/* Destroy the PR-SCTP sets cache */
1841 	sctp_ftsn_sets_fini();
1842 
1843 	netstack_unregister(NS_SCTP);
1844 	taskq_destroy(sctp_taskq);
1845 }
1846 
1847 /*
1848  * Shut down the SCTP stack instance.
1849  */
1850 /* ARGSUSED */
1851 static void
1852 sctp_stack_shutdown(netstackid_t stackid, void *arg)
1853 {
1854 	sctp_stack_t *sctps = (sctp_stack_t *)arg;
1855 
1856 	sctp_g_q_destroy(sctps);
1857 }
1858 
1859 /*
1860  * Free the SCTP stack instance.
1861  */
1862 static void
1863 sctp_stack_fini(netstackid_t stackid, void *arg)
1864 {
1865 	sctp_stack_t *sctps = (sctp_stack_t *)arg;
1866 
1867 	sctp_nd_free(sctps);
1868 
1869 	/* Destroy the recvq taskqs. */
1870 	sctp_rq_tq_fini(sctps);
1871 
1872 	/* Destroy saddr  */
1873 	sctp_saddr_fini(sctps);
1874 
1875 	/* Global SCTP PCB list. */
1876 	list_destroy(&sctps->sctps_g_list);
1877 
1878 	/* Destroy SCTP hash arrays. */
1879 	sctp_hash_destroy(sctps);
1880 
1881 	/* Destroy SCTP kernel stats. */
1882 	sctp_kstat2_fini(stackid, sctps->sctps_kstat);
1883 	sctps->sctps_kstat = NULL;
1884 	bzero(&sctps->sctps_statistics, sizeof (sctps->sctps_statistics));
1885 
1886 	sctp_kstat_fini(stackid, sctps->sctps_mibkp);
1887 	sctps->sctps_mibkp = NULL;
1888 
1889 	freeb(sctps->sctps_pad_mp);
1890 	sctps->sctps_pad_mp = NULL;
1891 
1892 	mutex_destroy(&sctps->sctps_g_lock);
1893 	mutex_destroy(&sctps->sctps_epriv_port_lock);
1894 	mutex_destroy(&sctps->sctps_g_q_lock);
1895 	cv_destroy(&sctps->sctps_g_q_cv);
1896 
1897 	kmem_free(sctps, sizeof (*sctps));
1898 }
1899 
1900 void
1901 sctp_display_all(sctp_stack_t *sctps)
1902 {
1903 	sctp_t *sctp_walker;
1904 
1905 	mutex_enter(&sctps->sctps_g_lock);
1906 	for (sctp_walker = sctps->sctps_gsctp; sctp_walker != NULL;
1907 	    sctp_walker = (sctp_t *)list_next(&sctps->sctps_g_list,
1908 	    sctp_walker)) {
1909 		(void) sctp_display(sctp_walker, NULL);
1910 	}
1911 	mutex_exit(&sctps->sctps_g_lock);
1912 }
1913 
1914 static void
1915 sctp_rq_tq_init(sctp_stack_t *sctps)
1916 {
1917 	sctps->sctps_recvq_tq_list_max_sz = 16;
1918 	sctps->sctps_recvq_tq_list_cur_sz = 1;
1919 	/*
1920 	 * Initialize the recvq_tq_list and create the first recvq taskq.
1921 	 * What to do if it fails?
1922 	 */
1923 	sctps->sctps_recvq_tq_list =
1924 	    kmem_zalloc(sctps->sctps_recvq_tq_list_max_sz * sizeof (taskq_t *),
1925 	    KM_SLEEP);
1926 	sctps->sctps_recvq_tq_list[0] = taskq_create("sctp_def_recvq_taskq",
1927 	    MIN(sctp_recvq_tq_thr_max, MAX(sctp_recvq_tq_thr_min, ncpus)),
1928 	    minclsyspri, sctp_recvq_tq_task_min, sctp_recvq_tq_task_max,
1929 	    TASKQ_PREPOPULATE);
1930 	mutex_init(&sctps->sctps_rq_tq_lock, NULL, MUTEX_DEFAULT, NULL);
1931 }
1932 
1933 static void
1934 sctp_rq_tq_fini(sctp_stack_t *sctps)
1935 {
1936 	int i;
1937 
1938 	for (i = 0; i < sctps->sctps_recvq_tq_list_cur_sz; i++) {
1939 		ASSERT(sctps->sctps_recvq_tq_list[i] != NULL);
1940 		taskq_destroy(sctps->sctps_recvq_tq_list[i]);
1941 	}
1942 	kmem_free(sctps->sctps_recvq_tq_list,
1943 	    sctps->sctps_recvq_tq_list_max_sz * sizeof (taskq_t *));
1944 	sctps->sctps_recvq_tq_list = NULL;
1945 }
1946 
1947 /* Add another taskq for a new ill. */
1948 void
1949 sctp_inc_taskq(sctp_stack_t *sctps)
1950 {
1951 	taskq_t *tq;
1952 	char tq_name[TASKQ_NAMELEN];
1953 
1954 	mutex_enter(&sctps->sctps_rq_tq_lock);
1955 	if (sctps->sctps_recvq_tq_list_cur_sz + 1 >
1956 	    sctps->sctps_recvq_tq_list_max_sz) {
1957 		mutex_exit(&sctps->sctps_rq_tq_lock);
1958 		cmn_err(CE_NOTE, "Cannot create more SCTP recvq taskq");
1959 		return;
1960 	}
1961 
1962 	(void) snprintf(tq_name, sizeof (tq_name), "sctp_recvq_taskq_%u",
1963 	    sctps->sctps_recvq_tq_list_cur_sz);
1964 	tq = taskq_create(tq_name,
1965 	    MIN(sctp_recvq_tq_thr_max, MAX(sctp_recvq_tq_thr_min, ncpus)),
1966 	    minclsyspri, sctp_recvq_tq_task_min, sctp_recvq_tq_task_max,
1967 	    TASKQ_PREPOPULATE);
1968 	if (tq == NULL) {
1969 		mutex_exit(&sctps->sctps_rq_tq_lock);
1970 		cmn_err(CE_NOTE, "SCTP recvq taskq creation failed");
1971 		return;
1972 	}
1973 	ASSERT(sctps->sctps_recvq_tq_list[
1974 	    sctps->sctps_recvq_tq_list_cur_sz] == NULL);
1975 	sctps->sctps_recvq_tq_list[sctps->sctps_recvq_tq_list_cur_sz] = tq;
1976 	atomic_add_32(&sctps->sctps_recvq_tq_list_cur_sz, 1);
1977 	mutex_exit(&sctps->sctps_rq_tq_lock);
1978 }
1979 
1980 #ifdef DEBUG
1981 uint32_t sendq_loop_cnt = 0;
1982 uint32_t sendq_collision = 0;
1983 uint32_t sendq_empty = 0;
1984 #endif
1985 
1986 void
1987 sctp_add_sendq(sctp_t *sctp, mblk_t *mp)
1988 {
1989 	mutex_enter(&sctp->sctp_sendq_lock);
1990 	if (sctp->sctp_sendq == NULL) {
1991 		sctp->sctp_sendq = mp;
1992 		sctp->sctp_sendq_tail = mp;
1993 	} else {
1994 		sctp->sctp_sendq_tail->b_next = mp;
1995 		sctp->sctp_sendq_tail = mp;
1996 	}
1997 	mutex_exit(&sctp->sctp_sendq_lock);
1998 }
1999 
2000 void
2001 sctp_process_sendq(sctp_t *sctp)
2002 {
2003 	mblk_t *mp;
2004 #ifdef DEBUG
2005 	uint32_t loop_cnt = 0;
2006 #endif
2007 
2008 	mutex_enter(&sctp->sctp_sendq_lock);
2009 	if (sctp->sctp_sendq == NULL || sctp->sctp_sendq_sending) {
2010 #ifdef DEBUG
2011 		if (sctp->sctp_sendq == NULL)
2012 			sendq_empty++;
2013 		else
2014 			sendq_collision++;
2015 #endif
2016 		mutex_exit(&sctp->sctp_sendq_lock);
2017 		return;
2018 	}
2019 	sctp->sctp_sendq_sending = B_TRUE;
2020 
2021 	/*
2022 	 * Note that while we are in this loop, other thread can put
2023 	 * new packets in the receive queue.  We may be looping for
2024 	 * quite a while.  This is OK even for an interrupt thread.
2025 	 * The reason is that SCTP should only able to send a limited
2026 	 * number of packets out in a burst.  So the number of times
2027 	 * we go through this loop should not be many.
2028 	 */
2029 	while ((mp = sctp->sctp_sendq) != NULL) {
2030 		sctp->sctp_sendq = mp->b_next;
2031 		ASSERT(sctp->sctp_connp->conn_ref > 0);
2032 		mutex_exit(&sctp->sctp_sendq_lock);
2033 		mp->b_next = NULL;
2034 		CONN_INC_REF(sctp->sctp_connp);
2035 		mp->b_flag |= MSGHASREF;
2036 		/* If we don't have sctp_current, default to IPv4 */
2037 		IP_PUT(mp, sctp->sctp_connp, sctp->sctp_current == NULL ?
2038 		    B_TRUE : sctp->sctp_current->isv4);
2039 		BUMP_LOCAL(sctp->sctp_opkts);
2040 #ifdef DEBUG
2041 		loop_cnt++;
2042 #endif
2043 		mutex_enter(&sctp->sctp_sendq_lock);
2044 	}
2045 
2046 	sctp->sctp_sendq_tail = NULL;
2047 	sctp->sctp_sendq_sending = B_FALSE;
2048 #ifdef DEBUG
2049 	if (loop_cnt > sendq_loop_cnt)
2050 		sendq_loop_cnt = loop_cnt;
2051 #endif
2052 	mutex_exit(&sctp->sctp_sendq_lock);
2053 }
2054 
2055 #ifdef DEBUG
2056 uint32_t recvq_loop_cnt = 0;
2057 uint32_t recvq_call = 0;
2058 #endif
2059 
2060 /*
2061  * Find the next recvq_tq to use.  This routine will go thru all the
2062  * taskqs until it can dispatch a job for the sctp.  If this fails,
2063  * it will create a new taskq and try it.
2064  */
2065 static boolean_t
2066 sctp_find_next_tq(sctp_t *sctp)
2067 {
2068 	int next_tq, try;
2069 	taskq_t *tq;
2070 	sctp_stack_t	*sctps = sctp->sctp_sctps;
2071 
2072 	/*
2073 	 * Note that since we don't hold a lock on sctp_rq_tq_lock for
2074 	 * performance reason, recvq_ta_list_cur_sz can be changed during
2075 	 * this loop.  The problem this will create is that the loop may
2076 	 * not have tried all the recvq_tq.  This should be OK.
2077 	 */
2078 	next_tq = atomic_add_32_nv(&sctps->sctps_recvq_tq_list_cur, 1) %
2079 	    sctps->sctps_recvq_tq_list_cur_sz;
2080 	for (try = 0; try < sctps->sctps_recvq_tq_list_cur_sz; try++) {
2081 		tq = sctps->sctps_recvq_tq_list[next_tq];
2082 		if (taskq_dispatch(tq, sctp_process_recvq, sctp,
2083 		    TQ_NOSLEEP) != NULL) {
2084 			sctp->sctp_recvq_tq = tq;
2085 			return (B_TRUE);
2086 		}
2087 		next_tq = (next_tq + 1) % sctps->sctps_recvq_tq_list_cur_sz;
2088 	}
2089 
2090 	/*
2091 	 * Create one more taskq and try it.  Note that sctp_inc_taskq()
2092 	 * may not have created another taskq if the number of recvq
2093 	 * taskqs is at the maximum.  We are probably in a pretty bad
2094 	 * shape if this actually happens...
2095 	 */
2096 	sctp_inc_taskq(sctps);
2097 	tq = sctps->sctps_recvq_tq_list[sctps->sctps_recvq_tq_list_cur_sz - 1];
2098 	if (taskq_dispatch(tq, sctp_process_recvq, sctp, TQ_NOSLEEP) != NULL) {
2099 		sctp->sctp_recvq_tq = tq;
2100 		return (B_TRUE);
2101 	}
2102 	SCTP_KSTAT(sctps, sctp_find_next_tq);
2103 	return (B_FALSE);
2104 }
2105 
2106 /*
2107  * To add a message to the recvq.  Note that the sctp_timer_fire()
2108  * routine also uses this function to add the timer message to the
2109  * receive queue for later processing.  And it should be the only
2110  * caller of sctp_add_recvq() which sets the try_harder argument
2111  * to B_TRUE.
2112  *
2113  * If the try_harder argument is B_TRUE, this routine sctp_find_next_tq()
2114  * will try very hard to dispatch the task.  Refer to the comment
2115  * for that routine on how it does that.
2116  */
2117 boolean_t
2118 sctp_add_recvq(sctp_t *sctp, mblk_t *mp, boolean_t caller_hold_lock)
2119 {
2120 	if (!caller_hold_lock)
2121 		mutex_enter(&sctp->sctp_recvq_lock);
2122 
2123 	/* If the taskq dispatch has not been scheduled, do it now. */
2124 	if (sctp->sctp_recvq_tq == NULL) {
2125 		ASSERT(sctp->sctp_recvq == NULL);
2126 		if (!sctp_find_next_tq(sctp)) {
2127 			if (!caller_hold_lock)
2128 				mutex_exit(&sctp->sctp_recvq_lock);
2129 			return (B_FALSE);
2130 		}
2131 		/* Make sure the sctp_t will not go away. */
2132 		SCTP_REFHOLD(sctp);
2133 	}
2134 
2135 	if (sctp->sctp_recvq == NULL) {
2136 		sctp->sctp_recvq = mp;
2137 		sctp->sctp_recvq_tail = mp;
2138 	} else {
2139 		sctp->sctp_recvq_tail->b_next = mp;
2140 		sctp->sctp_recvq_tail = mp;
2141 	}
2142 
2143 	if (!caller_hold_lock)
2144 		mutex_exit(&sctp->sctp_recvq_lock);
2145 	return (B_TRUE);
2146 }
2147 
2148 static void
2149 sctp_process_recvq(void *arg)
2150 {
2151 	sctp_t		*sctp = (sctp_t *)arg;
2152 	mblk_t		*mp;
2153 	mblk_t		*ipsec_mp;
2154 #ifdef DEBUG
2155 	uint32_t	loop_cnt = 0;
2156 #endif
2157 
2158 #ifdef	_BIG_ENDIAN
2159 #define	IPVER(ip6h)	((((uint32_t *)ip6h)[0] >> 28) & 0x7)
2160 #else
2161 #define	IPVER(ip6h)	((((uint32_t *)ip6h)[0] >> 4) & 0x7)
2162 #endif
2163 
2164 	RUN_SCTP(sctp);
2165 	mutex_enter(&sctp->sctp_recvq_lock);
2166 
2167 #ifdef DEBUG
2168 	recvq_call++;
2169 #endif
2170 	/*
2171 	 * Note that while we are in this loop, other thread can put
2172 	 * new packets in the receive queue.  We may be looping for
2173 	 * quite a while.
2174 	 */
2175 	while ((mp = sctp->sctp_recvq) != NULL) {
2176 		sctp->sctp_recvq = mp->b_next;
2177 		mutex_exit(&sctp->sctp_recvq_lock);
2178 		mp->b_next = NULL;
2179 #ifdef DEBUG
2180 		loop_cnt++;
2181 #endif
2182 		ipsec_mp = mp->b_prev;
2183 		mp->b_prev = NULL;
2184 		sctp_input_data(sctp, mp, ipsec_mp);
2185 
2186 		mutex_enter(&sctp->sctp_recvq_lock);
2187 	}
2188 
2189 	sctp->sctp_recvq_tail = NULL;
2190 	sctp->sctp_recvq_tq = NULL;
2191 
2192 	mutex_exit(&sctp->sctp_recvq_lock);
2193 
2194 	WAKE_SCTP(sctp);
2195 
2196 	/* We may have sent something when processing the receive queue. */
2197 	sctp_process_sendq(sctp);
2198 #ifdef DEBUG
2199 	if (loop_cnt > recvq_loop_cnt)
2200 		recvq_loop_cnt = loop_cnt;
2201 #endif
2202 	/* Now it can go away. */
2203 	SCTP_REFRELE(sctp);
2204 }
2205 
2206 /* ARGSUSED */
2207 static int
2208 sctp_conn_cache_constructor(void *buf, void *cdrarg, int kmflags)
2209 {
2210 	conn_t	*sctp_connp = (conn_t *)buf;
2211 	sctp_t	*sctp = (sctp_t *)&sctp_connp[1];
2212 
2213 	bzero(buf, (char *)&sctp[1] - (char *)buf);
2214 
2215 	sctp->sctp_connp = sctp_connp;
2216 	mutex_init(&sctp->sctp_reflock, NULL, MUTEX_DEFAULT, NULL);
2217 	mutex_init(&sctp->sctp_lock, NULL, MUTEX_DEFAULT, NULL);
2218 	mutex_init(&sctp->sctp_recvq_lock, NULL, MUTEX_DEFAULT, NULL);
2219 	cv_init(&sctp->sctp_cv, NULL, CV_DEFAULT, NULL);
2220 	mutex_init(&sctp->sctp_sendq_lock, NULL, MUTEX_DEFAULT, NULL);
2221 
2222 	sctp_connp->conn_rq = sctp_connp->conn_wq = NULL;
2223 	sctp_connp->conn_multicast_loop = IP_DEFAULT_MULTICAST_LOOP;
2224 	sctp_connp->conn_ulp = IPPROTO_SCTP;
2225 	mutex_init(&sctp_connp->conn_lock, NULL, MUTEX_DEFAULT, NULL);
2226 	cv_init(&sctp_connp->conn_cv, NULL, CV_DEFAULT, NULL);
2227 
2228 	return (0);
2229 }
2230 
2231 /* ARGSUSED */
2232 static void
2233 sctp_conn_cache_destructor(void *buf, void *cdrarg)
2234 {
2235 	conn_t	*sctp_connp = (conn_t *)buf;
2236 	sctp_t	*sctp = (sctp_t *)&sctp_connp[1];
2237 
2238 	ASSERT(!MUTEX_HELD(&sctp->sctp_lock));
2239 	ASSERT(!MUTEX_HELD(&sctp->sctp_reflock));
2240 	ASSERT(!MUTEX_HELD(&sctp->sctp_recvq_lock));
2241 	ASSERT(!MUTEX_HELD(&sctp->sctp_sendq_lock));
2242 	ASSERT(!MUTEX_HELD(&sctp->sctp_connp->conn_lock));
2243 
2244 	ASSERT(sctp->sctp_conn_hash_next == NULL);
2245 	ASSERT(sctp->sctp_conn_hash_prev == NULL);
2246 	ASSERT(sctp->sctp_listen_hash_next == NULL);
2247 	ASSERT(sctp->sctp_listen_hash_prev == NULL);
2248 	ASSERT(sctp->sctp_listen_tfp == NULL);
2249 	ASSERT(sctp->sctp_conn_tfp == NULL);
2250 
2251 	ASSERT(sctp->sctp_faddrs == NULL);
2252 	ASSERT(sctp->sctp_nsaddrs == 0);
2253 
2254 	ASSERT(sctp->sctp_ulpd == NULL);
2255 
2256 	ASSERT(sctp->sctp_lastfaddr == NULL);
2257 	ASSERT(sctp->sctp_primary == NULL);
2258 	ASSERT(sctp->sctp_current == NULL);
2259 	ASSERT(sctp->sctp_lastdata == NULL);
2260 
2261 	ASSERT(sctp->sctp_xmit_head == NULL);
2262 	ASSERT(sctp->sctp_xmit_tail == NULL);
2263 	ASSERT(sctp->sctp_xmit_unsent == NULL);
2264 	ASSERT(sctp->sctp_xmit_unsent_tail == NULL);
2265 
2266 	ASSERT(sctp->sctp_ostrcntrs == NULL);
2267 
2268 	ASSERT(sctp->sctp_sack_info == NULL);
2269 	ASSERT(sctp->sctp_ack_mp == NULL);
2270 	ASSERT(sctp->sctp_instr == NULL);
2271 
2272 	ASSERT(sctp->sctp_iphc == NULL);
2273 	ASSERT(sctp->sctp_iphc6 == NULL);
2274 	ASSERT(sctp->sctp_ipha == NULL);
2275 	ASSERT(sctp->sctp_ip6h == NULL);
2276 	ASSERT(sctp->sctp_sctph == NULL);
2277 	ASSERT(sctp->sctp_sctph6 == NULL);
2278 
2279 	ASSERT(sctp->sctp_cookie_mp == NULL);
2280 
2281 	ASSERT(sctp->sctp_refcnt == 0);
2282 	ASSERT(sctp->sctp_timer_mp == NULL);
2283 	ASSERT(sctp->sctp_connp->conn_ref == 0);
2284 	ASSERT(sctp->sctp_heartbeat_mp == NULL);
2285 	ASSERT(sctp->sctp_ptpbhn == NULL && sctp->sctp_bind_hash == NULL);
2286 
2287 	ASSERT(sctp->sctp_shutdown_faddr == NULL);
2288 
2289 	ASSERT(sctp->sctp_cxmit_list == NULL);
2290 
2291 	ASSERT(sctp->sctp_recvq == NULL);
2292 	ASSERT(sctp->sctp_recvq_tail == NULL);
2293 	ASSERT(sctp->sctp_recvq_tq == NULL);
2294 
2295 	ASSERT(sctp->sctp_sendq == NULL);
2296 	ASSERT(sctp->sctp_sendq_tail == NULL);
2297 	ASSERT(sctp->sctp_sendq_sending == B_FALSE);
2298 
2299 	ASSERT(sctp->sctp_ipp_hopopts == NULL);
2300 	ASSERT(sctp->sctp_ipp_rtdstopts == NULL);
2301 	ASSERT(sctp->sctp_ipp_rthdr == NULL);
2302 	ASSERT(sctp->sctp_ipp_dstopts == NULL);
2303 	ASSERT(sctp->sctp_ipp_pathmtu == NULL);
2304 
2305 	mutex_destroy(&sctp->sctp_reflock);
2306 	mutex_destroy(&sctp->sctp_lock);
2307 	mutex_destroy(&sctp->sctp_recvq_lock);
2308 	cv_destroy(&sctp->sctp_cv);
2309 	mutex_destroy(&sctp->sctp_sendq_lock);
2310 
2311 	mutex_destroy(&sctp_connp->conn_lock);
2312 	cv_destroy(&sctp_connp->conn_cv);
2313 }
2314 
2315 static void
2316 sctp_conn_cache_init()
2317 {
2318 	sctp_conn_cache = kmem_cache_create("sctp_conn_cache",
2319 	    sizeof (sctp_t) + sizeof (conn_t), 0, sctp_conn_cache_constructor,
2320 	    sctp_conn_cache_destructor, NULL, NULL, NULL, 0);
2321 }
2322 
2323 static void
2324 sctp_conn_cache_fini()
2325 {
2326 	kmem_cache_destroy(sctp_conn_cache);
2327 }
2328