xref: /illumos-gate/usr/src/uts/common/inet/sctp/sctp.c (revision 150d2c5288c645a1c1a7d2bee61199a3729406c7)
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 
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_rto_max = MSEC_TO_TICK(sctps->sctps_rto_maxg);
1019 		sctp->sctp_init_rto_max = sctp->sctp_rto_max;
1020 		sctp->sctp_rto_min = MSEC_TO_TICK(sctps->sctps_rto_ming);
1021 		sctp->sctp_rto_initial = MSEC_TO_TICK(
1022 		    sctps->sctps_rto_initialg);
1023 		sctp->sctp_pa_max_rxt = sctps->sctps_pa_max_retr;
1024 		sctp->sctp_pp_max_rxt = sctps->sctps_pp_max_retr;
1025 		sctp->sctp_max_init_rxt = sctps->sctps_max_init_retr;
1026 
1027 		sctp->sctp_num_istr = sctps->sctps_max_in_streams;
1028 		sctp->sctp_num_ostr = sctps->sctps_initial_out_streams;
1029 
1030 		sctp->sctp_hb_interval =
1031 		    MSEC_TO_TICK(sctps->sctps_heartbeat_interval);
1032 	}
1033 	sctp->sctp_understands_asconf = B_TRUE;
1034 	sctp->sctp_understands_addip = B_TRUE;
1035 	sctp->sctp_prsctp_aware = B_FALSE;
1036 
1037 	sctp->sctp_connp->conn_ref = 1;
1038 	sctp->sctp_connp->conn_fully_bound = B_FALSE;
1039 
1040 	sctp->sctp_prsctpdrop = 0;
1041 	sctp->sctp_msgcount = 0;
1042 
1043 	return (0);
1044 }
1045 
1046 /*
1047  * Extracts the init tag from an INIT chunk and checks if it matches
1048  * the sctp's verification tag. Returns 0 if it doesn't match, 1 if
1049  * it does.
1050  */
1051 static boolean_t
1052 sctp_icmp_verf(sctp_t *sctp, sctp_hdr_t *sh, mblk_t *mp)
1053 {
1054 	sctp_chunk_hdr_t *sch;
1055 	uint32_t verf, *vp;
1056 
1057 	sch = (sctp_chunk_hdr_t *)(sh + 1);
1058 	vp = (uint32_t *)(sch + 1);
1059 
1060 	/* Need at least the data chunk hdr and the first 4 bytes of INIT */
1061 	if ((unsigned char *)(vp + 1) > mp->b_wptr) {
1062 		return (B_FALSE);
1063 	}
1064 
1065 	bcopy(vp, &verf, sizeof (verf));
1066 
1067 	if (verf == sctp->sctp_lvtag) {
1068 		return (B_TRUE);
1069 	}
1070 	return (B_FALSE);
1071 }
1072 
1073 /*
1074  * sctp_icmp_error is called by sctp_input() to process ICMP error messages
1075  * passed up by IP.  The queue is the default queue.  We need to find a sctp_t
1076  * that corresponds to the returned datagram.  Passes the message back in on
1077  * the correct queue once it has located the connection.
1078  * Assumes that IP has pulled up everything up to and including
1079  * the ICMP header.
1080  */
1081 void
1082 sctp_icmp_error(sctp_t *sctp, mblk_t *mp)
1083 {
1084 	icmph_t *icmph;
1085 	ipha_t	*ipha;
1086 	int	iph_hdr_length;
1087 	sctp_hdr_t *sctph;
1088 	mblk_t *first_mp;
1089 	uint32_t new_mtu;
1090 	in6_addr_t dst;
1091 	sctp_faddr_t *fp;
1092 	sctp_stack_t	*sctps = sctp->sctp_sctps;
1093 
1094 	dprint(1, ("sctp_icmp_error: sctp=%p, mp=%p\n", (void *)sctp,
1095 	    (void *)mp));
1096 
1097 	first_mp = mp;
1098 
1099 	ipha = (ipha_t *)mp->b_rptr;
1100 	if (IPH_HDR_VERSION(ipha) != IPV4_VERSION) {
1101 		ASSERT(IPH_HDR_VERSION(ipha) == IPV6_VERSION);
1102 		sctp_icmp_error_ipv6(sctp, first_mp);
1103 		return;
1104 	}
1105 
1106 	iph_hdr_length = IPH_HDR_LENGTH(ipha);
1107 	icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length];
1108 	ipha = (ipha_t *)&icmph[1];
1109 	iph_hdr_length = IPH_HDR_LENGTH(ipha);
1110 	sctph = (sctp_hdr_t *)((char *)ipha + iph_hdr_length);
1111 	if ((uchar_t *)(sctph + 1) >= mp->b_wptr) {
1112 		/* not enough data for SCTP header */
1113 		freemsg(first_mp);
1114 		return;
1115 	}
1116 
1117 	switch (icmph->icmph_type) {
1118 	case ICMP_DEST_UNREACHABLE:
1119 		switch (icmph->icmph_code) {
1120 		case ICMP_FRAGMENTATION_NEEDED:
1121 			/*
1122 			 * Reduce the MSS based on the new MTU.  This will
1123 			 * eliminate any fragmentation locally.
1124 			 * N.B.  There may well be some funny side-effects on
1125 			 * the local send policy and the remote receive policy.
1126 			 * Pending further research, we provide
1127 			 * sctp_ignore_path_mtu just in case this proves
1128 			 * disastrous somewhere.
1129 			 *
1130 			 * After updating the MSS, retransmit part of the
1131 			 * dropped segment using the new mss by calling
1132 			 * sctp_wput_slow().  Need to adjust all those
1133 			 * params to make sure sctp_wput_slow() work properly.
1134 			 */
1135 			if (sctps->sctps_ignore_path_mtu)
1136 				break;
1137 
1138 			/* find the offending faddr */
1139 			IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, &dst);
1140 			fp = sctp_lookup_faddr(sctp, &dst);
1141 			if (fp == NULL) {
1142 				break;
1143 			}
1144 
1145 			new_mtu = ntohs(icmph->icmph_du_mtu);
1146 
1147 			if (new_mtu - sctp->sctp_hdr_len >= fp->sfa_pmss)
1148 				break;
1149 
1150 			/*
1151 			 * Make sure that sfa_pmss is a multiple of
1152 			 * SCTP_ALIGN.
1153 			 */
1154 			fp->sfa_pmss = (new_mtu - sctp->sctp_hdr_len) &
1155 				~(SCTP_ALIGN - 1);
1156 			fp->pmtu_discovered = 1;
1157 
1158 			break;
1159 		case ICMP_PORT_UNREACHABLE:
1160 		case ICMP_PROTOCOL_UNREACHABLE:
1161 			switch (sctp->sctp_state) {
1162 			case SCTPS_COOKIE_WAIT:
1163 			case SCTPS_COOKIE_ECHOED:
1164 				/* make sure the verification tag matches */
1165 				if (!sctp_icmp_verf(sctp, sctph, mp)) {
1166 					break;
1167 				}
1168 				BUMP_MIB(&sctps->sctps_mib, sctpAborted);
1169 				sctp_assoc_event(sctp, SCTP_CANT_STR_ASSOC, 0,
1170 				    NULL);
1171 				sctp_clean_death(sctp, ECONNREFUSED);
1172 				break;
1173 			}
1174 			break;
1175 		case ICMP_HOST_UNREACHABLE:
1176 		case ICMP_NET_UNREACHABLE:
1177 			/* Record the error in case we finally time out. */
1178 			sctp->sctp_client_errno = (icmph->icmph_code ==
1179 			    ICMP_HOST_UNREACHABLE) ? EHOSTUNREACH : ENETUNREACH;
1180 			break;
1181 		default:
1182 			break;
1183 		}
1184 		break;
1185 	case ICMP_SOURCE_QUENCH: {
1186 		/* Reduce the sending rate as if we got a retransmit timeout */
1187 		break;
1188 	}
1189 	}
1190 	freemsg(first_mp);
1191 }
1192 
1193 /*
1194  * sctp_icmp_error_ipv6() is called by sctp_icmp_error() to process ICMPv6
1195  * error messages passed up by IP.
1196  * Assumes that IP has pulled up all the extension headers as well
1197  * as the ICMPv6 header.
1198  */
1199 static void
1200 sctp_icmp_error_ipv6(sctp_t *sctp, mblk_t *mp)
1201 {
1202 	icmp6_t *icmp6;
1203 	ip6_t	*ip6h;
1204 	uint16_t	iph_hdr_length;
1205 	sctp_hdr_t *sctpha;
1206 	uint8_t	*nexthdrp;
1207 	uint32_t new_mtu;
1208 	sctp_faddr_t *fp;
1209 	sctp_stack_t	*sctps = sctp->sctp_sctps;
1210 
1211 	ip6h = (ip6_t *)mp->b_rptr;
1212 	iph_hdr_length = (ip6h->ip6_nxt != IPPROTO_SCTP) ?
1213 	    ip_hdr_length_v6(mp, ip6h) : IPV6_HDR_LEN;
1214 
1215 	icmp6 = (icmp6_t *)&mp->b_rptr[iph_hdr_length];
1216 	ip6h = (ip6_t *)&icmp6[1];
1217 	if (!ip_hdr_length_nexthdr_v6(mp, ip6h, &iph_hdr_length, &nexthdrp)) {
1218 		freemsg(mp);
1219 		return;
1220 	}
1221 	ASSERT(*nexthdrp == IPPROTO_SCTP);
1222 
1223 	/* XXX need ifindex to find connection */
1224 	sctpha = (sctp_hdr_t *)((char *)ip6h + iph_hdr_length);
1225 	if ((uchar_t *)sctpha >= mp->b_wptr) {
1226 		/* not enough data for SCTP header */
1227 		freemsg(mp);
1228 		return;
1229 	}
1230 	switch (icmp6->icmp6_type) {
1231 	case ICMP6_PACKET_TOO_BIG:
1232 		/*
1233 		 * Reduce the MSS based on the new MTU.  This will
1234 		 * eliminate any fragmentation locally.
1235 		 * N.B.  There may well be some funny side-effects on
1236 		 * the local send policy and the remote receive policy.
1237 		 * Pending further research, we provide
1238 		 * sctp_ignore_path_mtu just in case this proves
1239 		 * disastrous somewhere.
1240 		 *
1241 		 * After updating the MSS, retransmit part of the
1242 		 * dropped segment using the new mss by calling
1243 		 * sctp_wput_slow().  Need to adjust all those
1244 		 * params to make sure sctp_wput_slow() work properly.
1245 		 */
1246 		if (sctps->sctps_ignore_path_mtu)
1247 			break;
1248 
1249 		/* find the offending faddr */
1250 		fp = sctp_lookup_faddr(sctp, &ip6h->ip6_dst);
1251 		if (fp == NULL) {
1252 			break;
1253 		}
1254 
1255 		new_mtu = ntohs(icmp6->icmp6_mtu);
1256 
1257 		if (new_mtu - sctp->sctp_hdr6_len >= fp->sfa_pmss)
1258 			break;
1259 
1260 		/* Make sure that sfa_pmss is a multiple of SCTP_ALIGN. */
1261 		fp->sfa_pmss = (new_mtu - sctp->sctp_hdr6_len) &
1262 			~(SCTP_ALIGN - 1);
1263 		fp->pmtu_discovered = 1;
1264 
1265 		break;
1266 
1267 	case ICMP6_DST_UNREACH:
1268 		switch (icmp6->icmp6_code) {
1269 		case ICMP6_DST_UNREACH_NOPORT:
1270 			/* make sure the verification tag matches */
1271 			if (!sctp_icmp_verf(sctp, sctpha, mp)) {
1272 				break;
1273 			}
1274 			if (sctp->sctp_state == SCTPS_COOKIE_WAIT ||
1275 			    sctp->sctp_state == SCTPS_COOKIE_ECHOED) {
1276 				BUMP_MIB(&sctps->sctps_mib, sctpAborted);
1277 				sctp_assoc_event(sctp, SCTP_CANT_STR_ASSOC, 0,
1278 				    NULL);
1279 				sctp_clean_death(sctp, ECONNREFUSED);
1280 			}
1281 			break;
1282 
1283 		case ICMP6_DST_UNREACH_ADMIN:
1284 		case ICMP6_DST_UNREACH_NOROUTE:
1285 		case ICMP6_DST_UNREACH_NOTNEIGHBOR:
1286 		case ICMP6_DST_UNREACH_ADDR:
1287 			/* Record the error in case we finally time out. */
1288 			sctp->sctp_client_errno = EHOSTUNREACH;
1289 			break;
1290 		default:
1291 			break;
1292 		}
1293 		break;
1294 
1295 	case ICMP6_PARAM_PROB:
1296 		/* If this corresponds to an ICMP_PROTOCOL_UNREACHABLE */
1297 		if (icmp6->icmp6_code == ICMP6_PARAMPROB_NEXTHEADER &&
1298 		    (uchar_t *)ip6h + icmp6->icmp6_pptr ==
1299 		    (uchar_t *)nexthdrp) {
1300 			/* make sure the verification tag matches */
1301 			if (!sctp_icmp_verf(sctp, sctpha, mp)) {
1302 				break;
1303 			}
1304 			if (sctp->sctp_state == SCTPS_COOKIE_WAIT) {
1305 				BUMP_MIB(&sctps->sctps_mib, sctpAborted);
1306 				sctp_assoc_event(sctp, SCTP_CANT_STR_ASSOC, 0,
1307 				    NULL);
1308 				sctp_clean_death(sctp, ECONNREFUSED);
1309 			}
1310 			break;
1311 		}
1312 		break;
1313 
1314 	case ICMP6_TIME_EXCEEDED:
1315 	default:
1316 		break;
1317 	}
1318 	freemsg(mp);
1319 }
1320 
1321 /*
1322  * Called by sockfs to create a new sctp instance.
1323  *
1324  * If parent pointer is passed in, inherit settings from it.
1325  */
1326 sctp_t *
1327 sctp_create(void *sctp_ulpd, sctp_t *parent, int family, int flags,
1328     const sctp_upcalls_t *sctp_upcalls, sctp_sockbuf_limits_t *sbl,
1329     cred_t *credp)
1330 {
1331 	sctp_t		*sctp, *psctp;
1332 	conn_t		*sctp_connp;
1333 	mblk_t		*ack_mp, *hb_mp;
1334 	int		sleep = flags & SCTP_CAN_BLOCK ? KM_SLEEP : KM_NOSLEEP;
1335 	zoneid_t	zoneid;
1336 	sctp_stack_t	*sctps;
1337 
1338 	/* User must supply a credential. */
1339 	if (credp == NULL)
1340 		return (NULL);
1341 
1342 	psctp = (sctp_t *)parent;
1343 	if (psctp != NULL) {
1344 		sctps = psctp->sctp_sctps;
1345 		/* Increase here to have common decrease at end */
1346 		netstack_hold(sctps->sctps_netstack);
1347 	} else {
1348 		netstack_t *ns;
1349 
1350 		ns = netstack_find_by_cred(credp);
1351 		ASSERT(ns != NULL);
1352 		sctps = ns->netstack_sctp;
1353 		ASSERT(sctps != NULL);
1354 
1355 		/*
1356 		 * For exclusive stacks we set the zoneid to zero
1357 		 * to make SCTP operate as if in the global zone.
1358 		 */
1359 		if (sctps->sctps_netstack->netstack_stackid !=
1360 		    GLOBAL_NETSTACKID)
1361 			zoneid = GLOBAL_ZONEID;
1362 		else
1363 			zoneid = crgetzoneid(credp);
1364 
1365 		/*
1366 		 * For stackid zero this is done from strplumb.c, but
1367 		 * non-zero stackids are handled here.
1368 		 */
1369 		if (sctps->sctps_g_q == NULL &&
1370 		    sctps->sctps_netstack->netstack_stackid !=
1371 		    GLOBAL_NETSTACKID) {
1372 			sctp_g_q_setup(sctps);
1373 		}
1374 	}
1375 	if ((sctp_connp = ipcl_conn_create(IPCL_SCTPCONN, sleep,
1376 		    sctps->sctps_netstack)) == NULL) {
1377 		netstack_rele(sctps->sctps_netstack);
1378 		SCTP_KSTAT(sctps, sctp_conn_create);
1379 		return (NULL);
1380 	}
1381 	/*
1382 	 * ipcl_conn_create did a netstack_hold. Undo the hold that was
1383 	 * done at top of sctp_create.
1384 	 */
1385 	netstack_rele(sctps->sctps_netstack);
1386 	sctp = CONN2SCTP(sctp_connp);
1387 	sctp->sctp_sctps = sctps;
1388 
1389 	sctp_connp->conn_ulp_labeled = is_system_labeled();
1390 	if ((ack_mp = sctp_timer_alloc(sctp, sctp_ack_timer)) == NULL ||
1391 	    (hb_mp = sctp_timer_alloc(sctp, sctp_heartbeat_timer)) == NULL) {
1392 		if (ack_mp != NULL)
1393 			freeb(ack_mp);
1394 		netstack_rele(sctp_connp->conn_netstack);
1395 		sctp_connp->conn_netstack = NULL;
1396 		sctp->sctp_sctps = NULL;
1397 		SCTP_G_Q_REFRELE(sctps);
1398 		kmem_cache_free(sctp_conn_cache, sctp_connp);
1399 		return (NULL);
1400 	}
1401 
1402 	sctp->sctp_ack_mp = ack_mp;
1403 	sctp->sctp_heartbeat_mp = hb_mp;
1404 
1405 	switch (family) {
1406 	case AF_INET6:
1407 		sctp_connp->conn_af_isv6 = B_TRUE;
1408 		sctp->sctp_ipversion = IPV6_VERSION;
1409 		sctp->sctp_family = AF_INET6;
1410 		break;
1411 
1412 	case AF_INET:
1413 		sctp_connp->conn_af_isv6 = B_FALSE;
1414 		sctp_connp->conn_pkt_isv6 = B_FALSE;
1415 		sctp->sctp_ipversion = IPV4_VERSION;
1416 		sctp->sctp_family = AF_INET;
1417 		break;
1418 	default:
1419 		ASSERT(0);
1420 		break;
1421 	}
1422 	if (sctp_init_values(sctp, psctp, sleep) != 0) {
1423 		freeb(ack_mp);
1424 		freeb(hb_mp);
1425 		netstack_rele(sctp_connp->conn_netstack);
1426 		sctp_connp->conn_netstack = NULL;
1427 		sctp->sctp_sctps = NULL;
1428 		SCTP_G_Q_REFRELE(sctps);
1429 		kmem_cache_free(sctp_conn_cache, sctp_connp);
1430 		return (NULL);
1431 	}
1432 	sctp->sctp_cansleep = ((flags & SCTP_CAN_BLOCK) == SCTP_CAN_BLOCK);
1433 
1434 	sctp->sctp_mss = sctps->sctps_initial_mtu - ((family == AF_INET6) ?
1435 		sctp->sctp_hdr6_len : sctp->sctp_hdr_len);
1436 
1437 	if (psctp != NULL) {
1438 		RUN_SCTP(psctp);
1439 		/*
1440 		 * Inherit local address list, local port. Parent is either
1441 		 * in SCTPS_BOUND, or SCTPS_LISTEN state.
1442 		 */
1443 		ASSERT((psctp->sctp_state == SCTPS_BOUND) ||
1444 		    (psctp->sctp_state == SCTPS_LISTEN));
1445 		if (sctp_dup_saddrs(psctp, sctp, sleep)) {
1446 			WAKE_SCTP(psctp);
1447 			freeb(ack_mp);
1448 			freeb(hb_mp);
1449 			sctp_headers_free(sctp);
1450 			netstack_rele(sctps->sctps_netstack);
1451 			sctp_connp->conn_netstack = NULL;
1452 			sctp->sctp_sctps = NULL;
1453 			SCTP_G_Q_REFRELE(sctps);
1454 			kmem_cache_free(sctp_conn_cache, sctp_connp);
1455 			return (NULL);
1456 		}
1457 
1458 		/*
1459 		 * If the parent is specified, it'll be immediatelly
1460 		 * followed by sctp_connect(). So don't add this guy to
1461 		 * bind hash.
1462 		 */
1463 		sctp->sctp_lport = psctp->sctp_lport;
1464 		sctp->sctp_state = SCTPS_BOUND;
1465 		sctp->sctp_allzones = psctp->sctp_allzones;
1466 		sctp->sctp_zoneid = psctp->sctp_zoneid;
1467 		WAKE_SCTP(psctp);
1468 	} else {
1469 		sctp->sctp_zoneid = zoneid;
1470 	}
1471 
1472 	sctp_connp->conn_cred = credp;
1473 	crhold(credp);
1474 
1475 	/*
1476 	 * If the caller has the process-wide flag set, then default to MAC
1477 	 * exempt mode.  This allows read-down to unlabeled hosts.
1478 	 */
1479 	if (getpflags(NET_MAC_AWARE, credp) != 0)
1480 		sctp_connp->conn_mac_exempt = B_TRUE;
1481 
1482 	/* Initialize SCTP instance values,  our verf tag must never be 0 */
1483 	(void) random_get_pseudo_bytes((uint8_t *)&sctp->sctp_lvtag,
1484 	    sizeof (sctp->sctp_lvtag));
1485 	if (sctp->sctp_lvtag == 0)
1486 		sctp->sctp_lvtag = (uint32_t)gethrtime();
1487 	ASSERT(sctp->sctp_lvtag != 0);
1488 
1489 	sctp->sctp_ltsn = sctp->sctp_lvtag + 1;
1490 	sctp->sctp_lcsn = sctp->sctp_ltsn;
1491 	sctp->sctp_recovery_tsn = sctp->sctp_lastack_rxd = sctp->sctp_ltsn - 1;
1492 	sctp->sctp_adv_pap = sctp->sctp_lastack_rxd;
1493 
1494 	/* Information required by upper layer */
1495 	if (sctp_ulpd != NULL) {
1496 		sctp->sctp_ulpd = sctp_ulpd;
1497 
1498 		ASSERT(sctp_upcalls != NULL);
1499 		bcopy(sctp_upcalls, &sctp->sctp_upcalls,
1500 		    sizeof (sctp_upcalls_t));
1501 		ASSERT(sbl != NULL);
1502 		/* Fill in the socket buffer limits for sctpsockfs */
1503 		sbl->sbl_txlowat = sctp->sctp_xmit_lowater;
1504 		sbl->sbl_txbuf = sctp->sctp_xmit_hiwater;
1505 		sbl->sbl_rxbuf = sctp->sctp_rwnd;
1506 		sbl->sbl_rxlowat = SCTP_RECV_LOWATER;
1507 	}
1508 	/* If no sctp_ulpd, must be creating the default sctp */
1509 	ASSERT(sctp_ulpd != NULL || sctps->sctps_gsctp == NULL);
1510 
1511 	/* Insert this in the global list. */
1512 	SCTP_LINK(sctp, sctps);
1513 
1514 	return (sctp);
1515 }
1516 
1517 /*
1518  * Make sure we wait until the default queue is setup, yet allow
1519  * sctp_g_q_create() to open a SCTP stream.
1520  * We need to allow sctp_g_q_create() do do an open
1521  * of sctp, hence we compare curhread.
1522  * All others have to wait until the sctps_g_q has been
1523  * setup.
1524  */
1525 void
1526 sctp_g_q_setup(sctp_stack_t *sctps)
1527 {
1528 	mutex_enter(&sctps->sctps_g_q_lock);
1529 	if (sctps->sctps_g_q != NULL) {
1530 		mutex_exit(&sctps->sctps_g_q_lock);
1531 		return;
1532 	}
1533 	if (sctps->sctps_g_q_creator == NULL) {
1534 		/* This thread will set it up */
1535 		sctps->sctps_g_q_creator = curthread;
1536 		mutex_exit(&sctps->sctps_g_q_lock);
1537 		sctp_g_q_create(sctps);
1538 		mutex_enter(&sctps->sctps_g_q_lock);
1539 		ASSERT(sctps->sctps_g_q_creator == curthread);
1540 		sctps->sctps_g_q_creator = NULL;
1541 		cv_signal(&sctps->sctps_g_q_cv);
1542 		ASSERT(sctps->sctps_g_q != NULL);
1543 		mutex_exit(&sctps->sctps_g_q_lock);
1544 		return;
1545 	}
1546 	/* Everybody but the creator has to wait */
1547 	if (sctps->sctps_g_q_creator != curthread) {
1548 		while (sctps->sctps_g_q == NULL)
1549 			cv_wait(&sctps->sctps_g_q_cv, &sctps->sctps_g_q_lock);
1550 	}
1551 	mutex_exit(&sctps->sctps_g_q_lock);
1552 }
1553 
1554 major_t IP_MAJ;
1555 #define	IP	"ip"
1556 
1557 #define	SCTP6DEV		"/devices/pseudo/sctp6@0:sctp6"
1558 
1559 /*
1560  * Create a default sctp queue here instead of in strplumb
1561  */
1562 void
1563 sctp_g_q_create(sctp_stack_t *sctps)
1564 {
1565 	int error;
1566 	ldi_handle_t	lh = NULL;
1567 	ldi_ident_t	li = NULL;
1568 	int		rval;
1569 	cred_t		*cr;
1570 
1571 #ifdef NS_DEBUG
1572 	(void) printf("sctp_g_q_create()for stack %d\n",
1573 	    sctps->sctps_netstack->netstack_stackid);
1574 #endif
1575 
1576 	ASSERT(sctps->sctps_g_q_creator == curthread);
1577 
1578 	error = ldi_ident_from_major(IP_MAJ, &li);
1579 	if (error) {
1580 #ifdef DEBUG
1581 		printf("sctp_g_q_create: lyr ident get failed error %d\n",
1582 		    error);
1583 #endif
1584 		return;
1585 	}
1586 
1587 	cr = zone_get_kcred(netstackid_to_zoneid(
1588 				sctps->sctps_netstack->netstack_stackid));
1589 	ASSERT(cr != NULL);
1590 	/*
1591 	 * We set the sctp default queue to IPv6 because IPv4 falls
1592 	 * back to IPv6 when it can't find a client, but
1593 	 * IPv6 does not fall back to IPv4.
1594 	 */
1595 	error = ldi_open_by_name(SCTP6DEV, FREAD|FWRITE, cr, &lh, li);
1596 	if (error) {
1597 #ifdef DEBUG
1598 		printf("sctp_g_q_create: open of SCTP6DEV failed error %d\n",
1599 		    error);
1600 #endif
1601 		goto out;
1602 	}
1603 
1604 	/*
1605 	 * This ioctl causes the sctp framework to cache a pointer to
1606 	 * this stream, so we don't want to close the stream after
1607 	 * this operation.
1608 	 * Use the kernel credentials that are for the zone we're in.
1609 	 */
1610 	error = ldi_ioctl(lh, SCTP_IOC_DEFAULT_Q,
1611 	    (intptr_t)0, FKIOCTL, cr, &rval);
1612 	if (error) {
1613 #ifdef DEBUG
1614 		printf("sctp_g_q_create: ioctl SCTP_IOC_DEFAULT_Q failed "
1615 		    "error %d\n", error);
1616 #endif
1617 		goto out;
1618 	}
1619 	sctps->sctps_g_q_lh = lh;	/* For sctp_g_q_inactive */
1620 	lh = NULL;
1621 out:
1622 	/* Close layered handles */
1623 	if (li)
1624 		ldi_ident_release(li);
1625 	/* Keep cred around until _inactive needs it */
1626 	sctps->sctps_g_q_cr = cr;
1627 }
1628 
1629 /*
1630  * Remove the sctp_default queue so that new connections will not find it.
1631  * SCTP uses sctp_g_q for all transmission, so all sctp'ts implicitly
1632  * refer to it. Hence have each one have a reference on sctp_g_q_ref!
1633  *
1634  * We decrement the refcnt added in sctp_g_q_create. Once all the
1635  * sctp_t's which use the default go away, sctp_g_q_close will be called
1636  * and close the sctp_g_q. Once sctp_g_q is closed, sctp_close() will drop the
1637  * last reference count on the stack by calling netstack_rele().
1638  */
1639 void
1640 sctp_g_q_destroy(sctp_stack_t *sctps)
1641 {
1642 	if (sctps->sctps_g_q == NULL) {
1643 		return;	/* Nothing to cleanup */
1644 	}
1645 	/*
1646 	 * Keep sctps_g_q and sctps_gsctp until the last reference has
1647 	 * dropped, since the output is always done using those.
1648 	 * Need to decrement twice to take sctp_g_q_create and
1649 	 * the gsctp reference into account so that sctp_g_q_inactive is called
1650 	 * when all but the default queue remains.
1651 	 */
1652 #ifdef NS_DEBUG
1653 	(void) printf("sctp_g_q_destroy: ref %d\n",
1654 	    sctps->sctps_g_q_ref);
1655 #endif
1656 	SCTP_G_Q_REFRELE(sctps);
1657 }
1658 
1659 /*
1660  * Called when last user (could be sctp_g_q_destroy) drops reference count
1661  * using SCTP_G_Q_REFRELE.
1662  * Run by sctp_q_q_inactive using a taskq.
1663  */
1664 static void
1665 sctp_g_q_close(void *arg)
1666 {
1667 	sctp_stack_t *sctps = arg;
1668 	int error;
1669 	ldi_handle_t	lh = NULL;
1670 	ldi_ident_t	li = NULL;
1671 	cred_t		*cr;
1672 
1673 	lh = sctps->sctps_g_q_lh;
1674 	if (lh == NULL)
1675 		return;	/* Nothing to cleanup */
1676 
1677 	error = ldi_ident_from_major(IP_MAJ, &li);
1678 	if (error) {
1679 #ifdef NS_DEBUG
1680 		printf("sctp_g_q_inactive: lyr ident get failed error %d\n",
1681 		    error);
1682 #endif
1683 		return;
1684 	}
1685 
1686 	cr = sctps->sctps_g_q_cr;
1687 	sctps->sctps_g_q_cr = NULL;
1688 	ASSERT(cr != NULL);
1689 
1690 	/*
1691 	 * Make sure we can break the recursion when sctp_close decrements
1692 	 * the reference count causing g_q_inactive to be called again.
1693 	 */
1694 	sctps->sctps_g_q_lh = NULL;
1695 
1696 	/* close the default queue */
1697 	(void) ldi_close(lh, FREAD|FWRITE, cr);
1698 
1699 	/* Close layered handles */
1700 	ldi_ident_release(li);
1701 	crfree(cr);
1702 
1703 	ASSERT(sctps->sctps_g_q != NULL);
1704 	sctps->sctps_g_q = NULL;
1705 	/*
1706 	 * Now free sctps_gsctp.
1707 	 */
1708 	ASSERT(sctps->sctps_gsctp != NULL);
1709 	sctp_closei_local(sctps->sctps_gsctp);
1710 	SCTP_CONDEMNED(sctps->sctps_gsctp);
1711 	SCTP_REFRELE(sctps->sctps_gsctp);
1712 	sctps->sctps_gsctp = NULL;
1713 }
1714 
1715 /*
1716  * Called when last sctp_t drops reference count using SCTP_G_Q_REFRELE.
1717  *
1718  * Have to ensure that the ldi routines are not used by an
1719  * interrupt thread by using a taskq.
1720  */
1721 void
1722 sctp_g_q_inactive(sctp_stack_t *sctps)
1723 {
1724 	if (sctps->sctps_g_q_lh == NULL)
1725 		return;	/* Nothing to cleanup */
1726 
1727 	ASSERT(sctps->sctps_g_q_ref == 0);
1728 	SCTP_G_Q_REFHOLD(sctps); /* Compensate for what g_q_destroy did */
1729 
1730 	if (servicing_interrupt()) {
1731 		(void) taskq_dispatch(sctp_taskq, sctp_g_q_close,
1732 			    (void *) sctps, TQ_SLEEP);
1733 	} else {
1734 		sctp_g_q_close(sctps);
1735 	}
1736 }
1737 
1738 /* Run at module load time */
1739 void
1740 sctp_ddi_g_init(void)
1741 {
1742 	IP_MAJ = ddi_name_to_major(IP);
1743 
1744 	/* Create sctp_t/conn_t cache */
1745 	sctp_conn_cache_init();
1746 
1747 	/* Create the faddr cache */
1748 	sctp_faddr_init();
1749 
1750 	/* Create the sets cache */
1751 	sctp_sets_init();
1752 
1753 	/* Create the PR-SCTP sets cache */
1754 	sctp_ftsn_sets_init();
1755 
1756 	/* Initialize tables used for CRC calculation */
1757 	sctp_crc32_init();
1758 
1759 	sctp_taskq = taskq_create("sctp_taskq", 1, minclsyspri, 1, 1,
1760 	    TASKQ_PREPOPULATE);
1761 
1762 	/*
1763 	 * We want to be informed each time a stack is created or
1764 	 * destroyed in the kernel, so we can maintain the
1765 	 * set of sctp_stack_t's.
1766 	 */
1767 	netstack_register(NS_SCTP, sctp_stack_init, sctp_stack_shutdown,
1768 	    sctp_stack_fini);
1769 }
1770 
1771 static void *
1772 sctp_stack_init(netstackid_t stackid, netstack_t *ns)
1773 {
1774 	sctp_stack_t	*sctps;
1775 
1776 	sctps = kmem_zalloc(sizeof (*sctps), KM_SLEEP);
1777 	sctps->sctps_netstack = ns;
1778 
1779 	/* Initialize locks */
1780 	mutex_init(&sctps->sctps_g_q_lock, NULL, MUTEX_DEFAULT, NULL);
1781 	cv_init(&sctps->sctps_g_q_cv, NULL, CV_DEFAULT, NULL);
1782 	mutex_init(&sctps->sctps_g_lock, NULL, MUTEX_DEFAULT, NULL);
1783 	mutex_init(&sctps->sctps_epriv_port_lock, NULL, MUTEX_DEFAULT, NULL);
1784 	sctps->sctps_g_num_epriv_ports = SCTP_NUM_EPRIV_PORTS;
1785 	sctps->sctps_g_epriv_ports[0] = 2049;
1786 	sctps->sctps_g_epriv_ports[1] = 4045;
1787 
1788 	/* Initialize SCTP hash arrays. */
1789 	sctp_hash_init(sctps);
1790 
1791 	sctps->sctps_pad_mp = allocb(SCTP_ALIGN, BPRI_MED);
1792 	bzero(sctps->sctps_pad_mp->b_rptr, SCTP_ALIGN);
1793 	ASSERT(sctps->sctps_pad_mp);
1794 
1795 	if (!sctp_nd_init(sctps)) {
1796 		sctp_nd_free(sctps);
1797 	}
1798 
1799 	/* Initialize the recvq taskq. */
1800 	sctp_rq_tq_init(sctps);
1801 
1802 	/* saddr init */
1803 	sctp_saddr_init(sctps);
1804 
1805 	/* Global SCTP PCB list. */
1806 	list_create(&sctps->sctps_g_list, sizeof (sctp_t),
1807 	    offsetof(sctp_t, sctp_list));
1808 
1809 	/* Initialize sctp kernel stats. */
1810 	sctps->sctps_mibkp = sctp_kstat_init(stackid);
1811 	sctps->sctps_kstat =
1812 	    sctp_kstat2_init(stackid, &sctps->sctps_statistics);
1813 
1814 	return (sctps);
1815 }
1816 
1817 /*
1818  * Called when the module is about to be unloaded.
1819  */
1820 void
1821 sctp_ddi_g_destroy(void)
1822 {
1823 	/* Destroy sctp_t/conn_t caches */
1824 	sctp_conn_cache_fini();
1825 
1826 	/* Destroy the faddr cache */
1827 	sctp_faddr_fini();
1828 
1829 	/* Destroy the sets cache */
1830 	sctp_sets_fini();
1831 
1832 	/* Destroy the PR-SCTP sets cache */
1833 	sctp_ftsn_sets_fini();
1834 
1835 	netstack_unregister(NS_SCTP);
1836 	taskq_destroy(sctp_taskq);
1837 }
1838 
1839 /*
1840  * Shut down the SCTP stack instance.
1841  */
1842 /* ARGSUSED */
1843 static void
1844 sctp_stack_shutdown(netstackid_t stackid, void *arg)
1845 {
1846 	sctp_stack_t *sctps = (sctp_stack_t *)arg;
1847 
1848 	sctp_g_q_destroy(sctps);
1849 }
1850 
1851 /*
1852  * Free the SCTP stack instance.
1853  */
1854 static void
1855 sctp_stack_fini(netstackid_t stackid, void *arg)
1856 {
1857 	sctp_stack_t *sctps = (sctp_stack_t *)arg;
1858 
1859 	sctp_nd_free(sctps);
1860 
1861 	/* Destroy the recvq taskqs. */
1862 	sctp_rq_tq_fini(sctps);
1863 
1864 	/* Destroy saddr  */
1865 	sctp_saddr_fini(sctps);
1866 
1867 	/* Global SCTP PCB list. */
1868 	list_destroy(&sctps->sctps_g_list);
1869 
1870 	/* Destroy SCTP hash arrays. */
1871 	sctp_hash_destroy(sctps);
1872 
1873 	/* Destroy SCTP kernel stats. */
1874 	sctp_kstat2_fini(stackid, sctps->sctps_kstat);
1875 	sctps->sctps_kstat = NULL;
1876 	bzero(&sctps->sctps_statistics, sizeof (sctps->sctps_statistics));
1877 
1878 	sctp_kstat_fini(stackid, sctps->sctps_mibkp);
1879 	sctps->sctps_mibkp = NULL;
1880 
1881 	freeb(sctps->sctps_pad_mp);
1882 	sctps->sctps_pad_mp = NULL;
1883 
1884 	mutex_destroy(&sctps->sctps_g_lock);
1885 	mutex_destroy(&sctps->sctps_epriv_port_lock);
1886 	mutex_destroy(&sctps->sctps_g_q_lock);
1887 	cv_destroy(&sctps->sctps_g_q_cv);
1888 
1889 	kmem_free(sctps, sizeof (*sctps));
1890 }
1891 
1892 void
1893 sctp_display_all(sctp_stack_t *sctps)
1894 {
1895 	sctp_t *sctp_walker;
1896 
1897 	mutex_enter(&sctps->sctps_g_lock);
1898 	for (sctp_walker = sctps->sctps_gsctp; sctp_walker != NULL;
1899 	    sctp_walker = (sctp_t *)list_next(&sctps->sctps_g_list,
1900 	    sctp_walker)) {
1901 		(void) sctp_display(sctp_walker, NULL);
1902 	}
1903 	mutex_exit(&sctps->sctps_g_lock);
1904 }
1905 
1906 static void
1907 sctp_rq_tq_init(sctp_stack_t *sctps)
1908 {
1909 	sctps->sctps_recvq_tq_list_max_sz = 16;
1910 	sctps->sctps_recvq_tq_list_cur_sz = 1;
1911 	/*
1912 	 * Initialize the recvq_tq_list and create the first recvq taskq.
1913 	 * What to do if it fails?
1914 	 */
1915 	sctps->sctps_recvq_tq_list =
1916 	    kmem_zalloc(sctps->sctps_recvq_tq_list_max_sz * sizeof (taskq_t *),
1917 		KM_SLEEP);
1918 	sctps->sctps_recvq_tq_list[0] = taskq_create("sctp_def_recvq_taskq",
1919 	    MIN(sctp_recvq_tq_thr_max, MAX(sctp_recvq_tq_thr_min, ncpus)),
1920 	    minclsyspri, sctp_recvq_tq_task_min, sctp_recvq_tq_task_max,
1921 	    TASKQ_PREPOPULATE);
1922 	mutex_init(&sctps->sctps_rq_tq_lock, NULL, MUTEX_DEFAULT, NULL);
1923 }
1924 
1925 static void
1926 sctp_rq_tq_fini(sctp_stack_t *sctps)
1927 {
1928 	int i;
1929 
1930 	for (i = 0; i < sctps->sctps_recvq_tq_list_cur_sz; i++) {
1931 		ASSERT(sctps->sctps_recvq_tq_list[i] != NULL);
1932 		taskq_destroy(sctps->sctps_recvq_tq_list[i]);
1933 	}
1934 	kmem_free(sctps->sctps_recvq_tq_list,
1935 	    sctps->sctps_recvq_tq_list_max_sz * sizeof (taskq_t *));
1936 	sctps->sctps_recvq_tq_list = NULL;
1937 }
1938 
1939 /* Add another taskq for a new ill. */
1940 void
1941 sctp_inc_taskq(sctp_stack_t *sctps)
1942 {
1943 	taskq_t *tq;
1944 	char tq_name[TASKQ_NAMELEN];
1945 
1946 	mutex_enter(&sctps->sctps_rq_tq_lock);
1947 	if (sctps->sctps_recvq_tq_list_cur_sz + 1 >
1948 	    sctps->sctps_recvq_tq_list_max_sz) {
1949 		mutex_exit(&sctps->sctps_rq_tq_lock);
1950 		cmn_err(CE_NOTE, "Cannot create more SCTP recvq taskq");
1951 		return;
1952 	}
1953 
1954 	(void) snprintf(tq_name, sizeof (tq_name), "sctp_recvq_taskq_%u",
1955 	    sctps->sctps_recvq_tq_list_cur_sz);
1956 	tq = taskq_create(tq_name,
1957 	    MIN(sctp_recvq_tq_thr_max, MAX(sctp_recvq_tq_thr_min, ncpus)),
1958 	    minclsyspri, sctp_recvq_tq_task_min, sctp_recvq_tq_task_max,
1959 	    TASKQ_PREPOPULATE);
1960 	if (tq == NULL) {
1961 		mutex_exit(&sctps->sctps_rq_tq_lock);
1962 		cmn_err(CE_NOTE, "SCTP recvq taskq creation failed");
1963 		return;
1964 	}
1965 	ASSERT(sctps->sctps_recvq_tq_list[
1966 		    sctps->sctps_recvq_tq_list_cur_sz] == NULL);
1967 	sctps->sctps_recvq_tq_list[sctps->sctps_recvq_tq_list_cur_sz] = tq;
1968 	atomic_add_32(&sctps->sctps_recvq_tq_list_cur_sz, 1);
1969 	mutex_exit(&sctps->sctps_rq_tq_lock);
1970 }
1971 
1972 #ifdef DEBUG
1973 uint32_t sendq_loop_cnt = 0;
1974 uint32_t sendq_collision = 0;
1975 uint32_t sendq_empty = 0;
1976 #endif
1977 
1978 void
1979 sctp_add_sendq(sctp_t *sctp, mblk_t *mp)
1980 {
1981 	mutex_enter(&sctp->sctp_sendq_lock);
1982 	if (sctp->sctp_sendq == NULL) {
1983 		sctp->sctp_sendq = mp;
1984 		sctp->sctp_sendq_tail = mp;
1985 	} else {
1986 		sctp->sctp_sendq_tail->b_next = mp;
1987 		sctp->sctp_sendq_tail = mp;
1988 	}
1989 	mutex_exit(&sctp->sctp_sendq_lock);
1990 }
1991 
1992 void
1993 sctp_process_sendq(sctp_t *sctp)
1994 {
1995 	mblk_t *mp;
1996 #ifdef DEBUG
1997 	uint32_t loop_cnt = 0;
1998 #endif
1999 
2000 	mutex_enter(&sctp->sctp_sendq_lock);
2001 	if (sctp->sctp_sendq == NULL || sctp->sctp_sendq_sending) {
2002 #ifdef DEBUG
2003 		if (sctp->sctp_sendq == NULL)
2004 			sendq_empty++;
2005 		else
2006 			sendq_collision++;
2007 #endif
2008 		mutex_exit(&sctp->sctp_sendq_lock);
2009 		return;
2010 	}
2011 	sctp->sctp_sendq_sending = B_TRUE;
2012 
2013 	/*
2014 	 * Note that while we are in this loop, other thread can put
2015 	 * new packets in the receive queue.  We may be looping for
2016 	 * quite a while.  This is OK even for an interrupt thread.
2017 	 * The reason is that SCTP should only able to send a limited
2018 	 * number of packets out in a burst.  So the number of times
2019 	 * we go through this loop should not be many.
2020 	 */
2021 	while ((mp = sctp->sctp_sendq) != NULL) {
2022 		sctp->sctp_sendq = mp->b_next;
2023 		ASSERT(sctp->sctp_connp->conn_ref > 0);
2024 		mutex_exit(&sctp->sctp_sendq_lock);
2025 		mp->b_next = NULL;
2026 		CONN_INC_REF(sctp->sctp_connp);
2027 		mp->b_flag |= MSGHASREF;
2028 		/* If we don't have sctp_current, default to IPv4 */
2029 		IP_PUT(mp, sctp->sctp_connp, sctp->sctp_current == NULL ?
2030 		    B_TRUE : sctp->sctp_current->isv4);
2031 		BUMP_LOCAL(sctp->sctp_opkts);
2032 #ifdef DEBUG
2033 		loop_cnt++;
2034 #endif
2035 		mutex_enter(&sctp->sctp_sendq_lock);
2036 	}
2037 
2038 	sctp->sctp_sendq_tail = NULL;
2039 	sctp->sctp_sendq_sending = B_FALSE;
2040 #ifdef DEBUG
2041 	if (loop_cnt > sendq_loop_cnt)
2042 		sendq_loop_cnt = loop_cnt;
2043 #endif
2044 	mutex_exit(&sctp->sctp_sendq_lock);
2045 }
2046 
2047 #ifdef DEBUG
2048 uint32_t recvq_loop_cnt = 0;
2049 uint32_t recvq_call = 0;
2050 #endif
2051 
2052 /*
2053  * Find the next recvq_tq to use.  This routine will go thru all the
2054  * taskqs until it can dispatch a job for the sctp.  If this fails,
2055  * it will create a new taskq and try it.
2056  */
2057 static boolean_t
2058 sctp_find_next_tq(sctp_t *sctp)
2059 {
2060 	int next_tq, try;
2061 	taskq_t *tq;
2062 	sctp_stack_t	*sctps = sctp->sctp_sctps;
2063 
2064 	/*
2065 	 * Note that since we don't hold a lock on sctp_rq_tq_lock for
2066 	 * performance reason, recvq_ta_list_cur_sz can be changed during
2067 	 * this loop.  The problem this will create is that the loop may
2068 	 * not have tried all the recvq_tq.  This should be OK.
2069 	 */
2070 	next_tq = atomic_add_32_nv(&sctps->sctps_recvq_tq_list_cur, 1) %
2071 	    sctps->sctps_recvq_tq_list_cur_sz;
2072 	for (try = 0; try < sctps->sctps_recvq_tq_list_cur_sz; try++) {
2073 		tq = sctps->sctps_recvq_tq_list[next_tq];
2074 		if (taskq_dispatch(tq, sctp_process_recvq, sctp,
2075 		    TQ_NOSLEEP) != NULL) {
2076 			sctp->sctp_recvq_tq = tq;
2077 			return (B_TRUE);
2078 		}
2079 		next_tq = (next_tq + 1) % sctps->sctps_recvq_tq_list_cur_sz;
2080 	}
2081 
2082 	/*
2083 	 * Create one more taskq and try it.  Note that sctp_inc_taskq()
2084 	 * may not have created another taskq if the number of recvq
2085 	 * taskqs is at the maximum.  We are probably in a pretty bad
2086 	 * shape if this actually happens...
2087 	 */
2088 	sctp_inc_taskq(sctps);
2089 	tq = sctps->sctps_recvq_tq_list[sctps->sctps_recvq_tq_list_cur_sz - 1];
2090 	if (taskq_dispatch(tq, sctp_process_recvq, sctp, TQ_NOSLEEP) != NULL) {
2091 		sctp->sctp_recvq_tq = tq;
2092 		return (B_TRUE);
2093 	}
2094 	SCTP_KSTAT(sctps, sctp_find_next_tq);
2095 	return (B_FALSE);
2096 }
2097 
2098 /*
2099  * To add a message to the recvq.  Note that the sctp_timer_fire()
2100  * routine also uses this function to add the timer message to the
2101  * receive queue for later processing.  And it should be the only
2102  * caller of sctp_add_recvq() which sets the try_harder argument
2103  * to B_TRUE.
2104  *
2105  * If the try_harder argument is B_TRUE, this routine sctp_find_next_tq()
2106  * will try very hard to dispatch the task.  Refer to the comment
2107  * for that routine on how it does that.
2108  */
2109 boolean_t
2110 sctp_add_recvq(sctp_t *sctp, mblk_t *mp, boolean_t caller_hold_lock)
2111 {
2112 	if (!caller_hold_lock)
2113 		mutex_enter(&sctp->sctp_recvq_lock);
2114 
2115 	/* If the taskq dispatch has not been scheduled, do it now. */
2116 	if (sctp->sctp_recvq_tq == NULL) {
2117 		ASSERT(sctp->sctp_recvq == NULL);
2118 		if (!sctp_find_next_tq(sctp)) {
2119 			if (!caller_hold_lock)
2120 				mutex_exit(&sctp->sctp_recvq_lock);
2121 			return (B_FALSE);
2122 		}
2123 		/* Make sure the sctp_t will not go away. */
2124 		SCTP_REFHOLD(sctp);
2125 	}
2126 
2127 	if (sctp->sctp_recvq == NULL) {
2128 		sctp->sctp_recvq = mp;
2129 		sctp->sctp_recvq_tail = mp;
2130 	} else {
2131 		sctp->sctp_recvq_tail->b_next = mp;
2132 		sctp->sctp_recvq_tail = mp;
2133 	}
2134 
2135 	if (!caller_hold_lock)
2136 		mutex_exit(&sctp->sctp_recvq_lock);
2137 	return (B_TRUE);
2138 }
2139 
2140 static void
2141 sctp_process_recvq(void *arg)
2142 {
2143 	sctp_t		*sctp = (sctp_t *)arg;
2144 	mblk_t		*mp;
2145 	mblk_t		*ipsec_mp;
2146 #ifdef DEBUG
2147 	uint32_t	loop_cnt = 0;
2148 #endif
2149 
2150 #ifdef	_BIG_ENDIAN
2151 #define	IPVER(ip6h)	((((uint32_t *)ip6h)[0] >> 28) & 0x7)
2152 #else
2153 #define	IPVER(ip6h)	((((uint32_t *)ip6h)[0] >> 4) & 0x7)
2154 #endif
2155 
2156 	RUN_SCTP(sctp);
2157 	mutex_enter(&sctp->sctp_recvq_lock);
2158 
2159 #ifdef DEBUG
2160 	recvq_call++;
2161 #endif
2162 	/*
2163 	 * Note that while we are in this loop, other thread can put
2164 	 * new packets in the receive queue.  We may be looping for
2165 	 * quite a while.
2166 	 */
2167 	while ((mp = sctp->sctp_recvq) != NULL) {
2168 		sctp->sctp_recvq = mp->b_next;
2169 		mutex_exit(&sctp->sctp_recvq_lock);
2170 		mp->b_next = NULL;
2171 #ifdef DEBUG
2172 		loop_cnt++;
2173 #endif
2174 		ipsec_mp = mp->b_prev;
2175 		mp->b_prev = NULL;
2176 		sctp_input_data(sctp, mp, ipsec_mp);
2177 
2178 		mutex_enter(&sctp->sctp_recvq_lock);
2179 	}
2180 
2181 	sctp->sctp_recvq_tail = NULL;
2182 	sctp->sctp_recvq_tq = NULL;
2183 
2184 	mutex_exit(&sctp->sctp_recvq_lock);
2185 
2186 	WAKE_SCTP(sctp);
2187 
2188 	/* We may have sent something when processing the receive queue. */
2189 	sctp_process_sendq(sctp);
2190 #ifdef DEBUG
2191 	if (loop_cnt > recvq_loop_cnt)
2192 		recvq_loop_cnt = loop_cnt;
2193 #endif
2194 	/* Now it can go away. */
2195 	SCTP_REFRELE(sctp);
2196 }
2197 
2198 /* ARGSUSED */
2199 static int
2200 sctp_conn_cache_constructor(void *buf, void *cdrarg, int kmflags)
2201 {
2202 	conn_t	*sctp_connp = (conn_t *)buf;
2203 	sctp_t	*sctp = (sctp_t *)&sctp_connp[1];
2204 
2205 	bzero(buf, (char *)&sctp[1] - (char *)buf);
2206 
2207 	sctp->sctp_connp = sctp_connp;
2208 	mutex_init(&sctp->sctp_reflock, NULL, MUTEX_DEFAULT, NULL);
2209 	mutex_init(&sctp->sctp_lock, NULL, MUTEX_DEFAULT, NULL);
2210 	mutex_init(&sctp->sctp_recvq_lock, NULL, MUTEX_DEFAULT, NULL);
2211 	cv_init(&sctp->sctp_cv, NULL, CV_DEFAULT, NULL);
2212 	mutex_init(&sctp->sctp_sendq_lock, NULL, MUTEX_DEFAULT, NULL);
2213 
2214 	sctp_connp->conn_rq = sctp_connp->conn_wq = NULL;
2215 	sctp_connp->conn_multicast_loop = IP_DEFAULT_MULTICAST_LOOP;
2216 	sctp_connp->conn_ulp = IPPROTO_SCTP;
2217 	mutex_init(&sctp_connp->conn_lock, NULL, MUTEX_DEFAULT, NULL);
2218 	cv_init(&sctp_connp->conn_cv, NULL, CV_DEFAULT, NULL);
2219 
2220 	return (0);
2221 }
2222 
2223 /* ARGSUSED */
2224 static void
2225 sctp_conn_cache_destructor(void *buf, void *cdrarg)
2226 {
2227 	conn_t	*sctp_connp = (conn_t *)buf;
2228 	sctp_t	*sctp = (sctp_t *)&sctp_connp[1];
2229 
2230 	ASSERT(!MUTEX_HELD(&sctp->sctp_lock));
2231 	ASSERT(!MUTEX_HELD(&sctp->sctp_reflock));
2232 	ASSERT(!MUTEX_HELD(&sctp->sctp_recvq_lock));
2233 	ASSERT(!MUTEX_HELD(&sctp->sctp_sendq_lock));
2234 	ASSERT(!MUTEX_HELD(&sctp->sctp_connp->conn_lock));
2235 
2236 	ASSERT(sctp->sctp_conn_hash_next == NULL);
2237 	ASSERT(sctp->sctp_conn_hash_prev == NULL);
2238 	ASSERT(sctp->sctp_listen_hash_next == NULL);
2239 	ASSERT(sctp->sctp_listen_hash_prev == NULL);
2240 	ASSERT(sctp->sctp_listen_tfp == NULL);
2241 	ASSERT(sctp->sctp_conn_tfp == NULL);
2242 
2243 	ASSERT(sctp->sctp_faddrs == NULL);
2244 	ASSERT(sctp->sctp_nsaddrs == 0);
2245 
2246 	ASSERT(sctp->sctp_ulpd == NULL);
2247 
2248 	ASSERT(sctp->sctp_lastfaddr == NULL);
2249 	ASSERT(sctp->sctp_primary == NULL);
2250 	ASSERT(sctp->sctp_current == NULL);
2251 	ASSERT(sctp->sctp_lastdata == NULL);
2252 
2253 	ASSERT(sctp->sctp_xmit_head == NULL);
2254 	ASSERT(sctp->sctp_xmit_tail == NULL);
2255 	ASSERT(sctp->sctp_xmit_unsent == NULL);
2256 	ASSERT(sctp->sctp_xmit_unsent_tail == NULL);
2257 
2258 	ASSERT(sctp->sctp_ostrcntrs == NULL);
2259 
2260 	ASSERT(sctp->sctp_sack_info == NULL);
2261 	ASSERT(sctp->sctp_ack_mp == NULL);
2262 	ASSERT(sctp->sctp_instr == NULL);
2263 
2264 	ASSERT(sctp->sctp_iphc == NULL);
2265 	ASSERT(sctp->sctp_iphc6 == NULL);
2266 	ASSERT(sctp->sctp_ipha == NULL);
2267 	ASSERT(sctp->sctp_ip6h == NULL);
2268 	ASSERT(sctp->sctp_sctph == NULL);
2269 	ASSERT(sctp->sctp_sctph6 == NULL);
2270 
2271 	ASSERT(sctp->sctp_cookie_mp == NULL);
2272 
2273 	ASSERT(sctp->sctp_refcnt == 0);
2274 	ASSERT(sctp->sctp_timer_mp == NULL);
2275 	ASSERT(sctp->sctp_connp->conn_ref == 0);
2276 	ASSERT(sctp->sctp_heartbeat_mp == NULL);
2277 	ASSERT(sctp->sctp_ptpbhn == NULL && sctp->sctp_bind_hash == NULL);
2278 
2279 	ASSERT(sctp->sctp_shutdown_faddr == NULL);
2280 
2281 	ASSERT(sctp->sctp_cxmit_list == NULL);
2282 
2283 	ASSERT(sctp->sctp_recvq == NULL);
2284 	ASSERT(sctp->sctp_recvq_tail == NULL);
2285 	ASSERT(sctp->sctp_recvq_tq == NULL);
2286 
2287 	ASSERT(sctp->sctp_sendq == NULL);
2288 	ASSERT(sctp->sctp_sendq_tail == NULL);
2289 	ASSERT(sctp->sctp_sendq_sending == B_FALSE);
2290 
2291 	ASSERT(sctp->sctp_ipp_hopopts == NULL);
2292 	ASSERT(sctp->sctp_ipp_rtdstopts == NULL);
2293 	ASSERT(sctp->sctp_ipp_rthdr == NULL);
2294 	ASSERT(sctp->sctp_ipp_dstopts == NULL);
2295 	ASSERT(sctp->sctp_ipp_pathmtu == NULL);
2296 
2297 	mutex_destroy(&sctp->sctp_reflock);
2298 	mutex_destroy(&sctp->sctp_lock);
2299 	mutex_destroy(&sctp->sctp_recvq_lock);
2300 	cv_destroy(&sctp->sctp_cv);
2301 	mutex_destroy(&sctp->sctp_sendq_lock);
2302 
2303 	mutex_destroy(&sctp_connp->conn_lock);
2304 	cv_destroy(&sctp_connp->conn_cv);
2305 }
2306 
2307 static void
2308 sctp_conn_cache_init()
2309 {
2310 	sctp_conn_cache = kmem_cache_create("sctp_conn_cache",
2311 	    sizeof (sctp_t) + sizeof (conn_t), 0, sctp_conn_cache_constructor,
2312 	    sctp_conn_cache_destructor, NULL, NULL, NULL, 0);
2313 }
2314 
2315 static void
2316 sctp_conn_cache_fini()
2317 {
2318 	kmem_cache_destroy(sctp_conn_cache);
2319 }
2320