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