xref: /titanic_52/usr/src/uts/common/inet/sctp/sctp.c (revision 6185db853e024a486ff8837e6784dd290d866112)
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 2006 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 
47 #include <sys/errno.h>
48 #include <sys/signal.h>
49 #include <sys/socket.h>
50 #include <sys/isa_defs.h>
51 #include <netinet/in.h>
52 #include <netinet/tcp.h>
53 #include <netinet/ip6.h>
54 #include <netinet/icmp6.h>
55 #include <netinet/sctp.h>
56 #include <net/if.h>
57 
58 #include <inet/common.h>
59 #include <inet/ip.h>
60 #include <inet/ip6.h>
61 #include <inet/mi.h>
62 #include <inet/mib2.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 extern major_t SCTP6_MAJ;
75 extern major_t SCTP_MAJ;
76 
77 int sctpdebug;
78 sin6_t	sctp_sin6_null;	/* Zero address for quick clears */
79 
80 extern mblk_t *sctp_pad_mp;	/* pad unaligned data chunks */
81 
82 static void	sctp_closei_local(sctp_t *sctp);
83 static int	sctp_init_values(sctp_t *, sctp_t *, int);
84 static void	sctp_icmp_error_ipv6(sctp_t *sctp, mblk_t *mp);
85 static void	sctp_process_recvq(void *);
86 static void	sctp_rq_tq_init(void);
87 static void	sctp_rq_tq_fini(void);
88 static void	sctp_conn_cache_init();
89 static void	sctp_conn_cache_fini();
90 static int	sctp_conn_cache_constructor();
91 static void	sctp_conn_cache_destructor();
92 
93 /*
94  * SCTP receive queue taskq
95  *
96  * At SCTP initialization time, a default taskq is created for
97  * servicing packets received when the interrupt thread cannot
98  * get a hold on the sctp_t.  The number of taskq can be increased in
99  * sctp_find_next_tq() when an existing taskq cannot be dispatched.
100  * The taskqs are never removed.  But the max number of taskq which
101  * can be created is controlled by sctp_recvq_tq_list_max_sz.  Note
102  * that SCTP recvq taskq is not tied to any specific CPU or ill.
103  *
104  * Those taskqs are stored in an array recvq_tq_list.  And they are
105  * used in a round robin fashion.  The current taskq being used is
106  * determined by recvq_tq_list_cur.
107  */
108 
109 /* This lock protects the SCTP recvq_tq_list array and recvq_tq_list_cur_sz. */
110 static kmutex_t	sctp_rq_tq_lock;
111 int		sctp_recvq_tq_list_max_sz = 16;
112 static taskq_t	**recvq_tq_list;
113 
114 /* Current number of recvq taskq.  At least 1 for the default taskq. */
115 static uint32_t	recvq_tq_list_cur_sz = 1;
116 static uint32_t	recvq_tq_list_cur = 0;
117 
118 /* The minimum number of threads for each taskq. */
119 int		sctp_recvq_tq_thr_min = 4;
120 /* The maximum number of threads for each taskq. */
121 int		sctp_recvq_tq_thr_max = 16;
122 /* The minimum number of tasks for each taskq. */
123 int		sctp_recvq_tq_task_min = 5;
124 /* The maxiimum number of tasks for each taskq. */
125 int		sctp_recvq_tq_task_max = 50;
126 
127 /*
128  * Default queue used for sending packets.  No need to have lock for it
129  * as it should never be changed.
130  */
131 queue_t	*sctp_g_q;
132 int sctp_g_q_fd;
133 /* The default sctp_t for responding out of the blue packets. */
134 sctp_t *gsctp;
135 
136 /* Protected by sctp_g_lock */
137 list_t	sctp_g_list;	/* SCTP instance data chain */
138 kmutex_t sctp_g_lock;
139 
140 /*  sctp_t/conn_t kmem cache */
141 struct kmem_cache	*sctp_conn_cache;
142 
143 #define	SCTP_CONDEMNED(sctp)				\
144 	mutex_enter(&(sctp)->sctp_reflock);		\
145 	((sctp)->sctp_condemned = B_TRUE);		\
146 	mutex_exit(&(sctp)->sctp_reflock);
147 
148 /* Link/unlink a sctp_t to/from the global list. */
149 #define	SCTP_LINK(sctp)					\
150 	mutex_enter(&sctp_g_lock);			\
151 	list_insert_tail(&sctp_g_list, (sctp));		\
152 	mutex_exit(&sctp_g_lock);
153 
154 #define	SCTP_UNLINK(sctp)				\
155 	mutex_enter(&sctp_g_lock);			\
156 	ASSERT((sctp)->sctp_condemned);			\
157 	list_remove(&sctp_g_list, (sctp));		\
158 	mutex_exit(&sctp_g_lock);
159 
160 /*
161  * Hooks for Sun Cluster. On non-clustered nodes these will remain NULL.
162  * PSARC/2005/602.
163  */
164 void (*cl_sctp_listen)(sa_family_t, uchar_t *, uint_t, in_port_t) = NULL;
165 void (*cl_sctp_unlisten)(sa_family_t, uchar_t *, uint_t, in_port_t) = NULL;
166 void (*cl_sctp_connect)(sa_family_t, uchar_t *, uint_t, in_port_t,
167     uchar_t *, uint_t, in_port_t, boolean_t, cl_sctp_handle_t) = NULL;
168 void (*cl_sctp_disconnect)(sa_family_t, cl_sctp_handle_t) = NULL;
169 void (*cl_sctp_assoc_change)(sa_family_t, uchar_t *, size_t, uint_t,
170     uchar_t *, size_t, uint_t, int, cl_sctp_handle_t) = NULL;
171 void (*cl_sctp_check_addrs)(sa_family_t, in_port_t, uchar_t **, size_t,
172     uint_t *, boolean_t) = NULL;
173 /*
174  * Return the version number of the SCTP kernel interface.
175  */
176 int
177 sctp_itf_ver(int cl_ver)
178 {
179 	if (cl_ver != SCTP_ITF_VER)
180 		return (-1);
181 	return (SCTP_ITF_VER);
182 }
183 
184 /*
185  * Called when we need a new sctp instantiation but don't really have a
186  * new q to hang it off of. Copy the priv flag from the passed in structure.
187  */
188 sctp_t *
189 sctp_create_eager(sctp_t *psctp)
190 {
191 	sctp_t	*sctp;
192 	mblk_t	*ack_mp, *hb_mp;
193 	conn_t	*connp, *pconnp;
194 	cred_t *credp;
195 
196 	if ((connp = ipcl_conn_create(IPCL_SCTPCONN, KM_NOSLEEP)) == NULL)
197 		return (NULL);
198 
199 	connp->conn_ulp_labeled = is_system_labeled();
200 
201 	sctp = CONN2SCTP(connp);
202 
203 	if ((ack_mp = sctp_timer_alloc(sctp, sctp_ack_timer)) == NULL ||
204 	    (hb_mp = sctp_timer_alloc(sctp, sctp_heartbeat_timer)) == NULL) {
205 		if (ack_mp != NULL)
206 			freeb(ack_mp);
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 		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 
246 	sctp->sctp_mss = psctp->sctp_mss;
247 	sctp->sctp_detached = B_TRUE;
248 	/*
249 	 * Link to the global as soon as possible so that this sctp_t
250 	 * can be found.
251 	 */
252 	SCTP_LINK(sctp);
253 
254 	return (sctp);
255 }
256 
257 /*
258  * We are dying for some reason.  Try to do it gracefully.
259  */
260 void
261 sctp_clean_death(sctp_t *sctp, int err)
262 {
263 	ASSERT(sctp != NULL);
264 	ASSERT((sctp->sctp_family == AF_INET &&
265 	    sctp->sctp_ipversion == IPV4_VERSION) ||
266 	    (sctp->sctp_family == AF_INET6 &&
267 	    (sctp->sctp_ipversion == IPV4_VERSION ||
268 	    sctp->sctp_ipversion == IPV6_VERSION)));
269 
270 	dprint(3, ("sctp_clean_death %p, state %d\n", (void *)sctp,
271 	    sctp->sctp_state));
272 
273 	sctp->sctp_client_errno = err;
274 	/*
275 	 * Check to see if we need to notify upper layer.
276 	 */
277 	if ((sctp->sctp_state >= SCTPS_COOKIE_WAIT) &&
278 	    !SCTP_IS_DETACHED(sctp)) {
279 		if (sctp->sctp_xmit_head || sctp->sctp_xmit_unsent) {
280 			sctp_regift_xmitlist(sctp);
281 		}
282 		if (sctp->sctp_ulp_disconnected(sctp->sctp_ulpd, err)) {
283 			/*
284 			 * Socket is gone, detach.
285 			 */
286 			sctp->sctp_detached = B_TRUE;
287 			sctp->sctp_ulpd = NULL;
288 			bzero(&sctp->sctp_upcalls, sizeof (sctp_upcalls_t));
289 		}
290 	}
291 
292 	/* Remove this sctp from all hashes. */
293 	sctp_closei_local(sctp);
294 
295 	/*
296 	 * If the sctp_t is detached, we need to finish freeing up
297 	 * the resources.  At this point, ip_fanout_sctp() should have
298 	 * a hold on this sctp_t.  Some thread doing snmp stuff can
299 	 * have a hold.  And a taskq can also have a hold waiting to
300 	 * work.  sctp_unlink() the sctp_t from the global list so
301 	 * that no new thread can find it.  Then do a SCTP_REFRELE().
302 	 * The sctp_t will be freed after all those threads are done.
303 	 */
304 	if (SCTP_IS_DETACHED(sctp)) {
305 		SCTP_CONDEMNED(sctp);
306 		SCTP_REFRELE(sctp);
307 	}
308 }
309 
310 /*
311  * Called by upper layer when it wants to close this association.
312  * Depending on the state of this assoication, we need to do
313  * different things.
314  *
315  * If the state is below COOKIE_ECHOED or it is COOKIE_ECHOED but with
316  * no sent data, just remove this sctp from all the hashes.  This
317  * makes sure that all packets from the other end will go to the default
318  * sctp handling.  The upper layer will then do a sctp_close() to clean
319  * up.
320  *
321  * Otherwise, check and see if SO_LINGER is set.  If it is set, check
322  * the value.  If the value is 0, consider this an abortive close.  Send
323  * an ABORT message and kill the associatiion.
324  *
325  */
326 int
327 sctp_disconnect(sctp_t *sctp)
328 {
329 	int	error = 0;
330 
331 	dprint(3, ("sctp_disconnect %p, state %d\n", (void *)sctp,
332 	    sctp->sctp_state));
333 
334 	RUN_SCTP(sctp);
335 
336 	switch (sctp->sctp_state) {
337 	case SCTPS_IDLE:
338 	case SCTPS_BOUND:
339 	case SCTPS_LISTEN:
340 		break;
341 	case SCTPS_COOKIE_WAIT:
342 	case SCTPS_COOKIE_ECHOED:
343 		/*
344 		 * Close during the connect 3-way handshake
345 		 * but here there may or may not be pending data
346 		 * already on queue. Process almost same as in
347 		 * the ESTABLISHED state.
348 		 */
349 		if (sctp->sctp_xmit_head == NULL &&
350 		    sctp->sctp_xmit_unsent == NULL) {
351 			break;
352 		}
353 		/* FALLTHRU */
354 	default:
355 		/*
356 		 * If SO_LINGER has set a zero linger time, abort the
357 		 * connection with a reset.
358 		 */
359 		if (sctp->sctp_linger && sctp->sctp_lingertime == 0) {
360 			sctp_user_abort(sctp, NULL, B_FALSE);
361 			break;
362 		}
363 
364 		/*
365 		 * In there is unread data, send an ABORT
366 		 */
367 		if (sctp->sctp_rxqueued > 0 || sctp->sctp_irwnd >
368 		    sctp->sctp_rwnd) {
369 			sctp_user_abort(sctp, NULL, B_FALSE);
370 			break;
371 		}
372 		/*
373 		 * Transmit the shutdown before detaching the sctp_t.
374 		 * After sctp_detach returns this queue/perimeter
375 		 * no longer owns the sctp_t thus others can modify it.
376 		 */
377 		sctp_send_shutdown(sctp, 0);
378 
379 		/* Pass gathered wisdom to IP for keeping */
380 		sctp_update_ire(sctp);
381 
382 		/*
383 		 * If lingering on close then wait until the shutdown
384 		 * is complete, or the SO_LINGER time passes, or an
385 		 * ABORT is sent/received.  Note that sctp_disconnect()
386 		 * can be called more than once.  Make sure that only
387 		 * one thread waits.
388 		 */
389 		if (sctp->sctp_linger && sctp->sctp_lingertime > 0 &&
390 		    sctp->sctp_state >= SCTPS_ESTABLISHED &&
391 		    !sctp->sctp_lingering) {
392 			clock_t stoptime;	/* in ticks */
393 			clock_t ret;
394 
395 			/*
396 			 * Process the sendq to send the SHUTDOWN out
397 			 * before waiting.
398 			 */
399 			sctp_process_sendq(sctp);
400 
401 			sctp->sctp_lingering = 1;
402 			sctp->sctp_client_errno = 0;
403 			stoptime = lbolt + sctp->sctp_lingertime;
404 
405 			mutex_enter(&sctp->sctp_lock);
406 			sctp->sctp_running = B_FALSE;
407 			while (sctp->sctp_state >= SCTPS_ESTABLISHED &&
408 			    sctp->sctp_client_errno == 0) {
409 				cv_broadcast(&sctp->sctp_cv);
410 				ret = cv_timedwait_sig(&sctp->sctp_cv,
411 				    &sctp->sctp_lock, stoptime);
412 				if (ret < 0) {
413 					/* Stoptime has reached. */
414 					sctp->sctp_client_errno = EWOULDBLOCK;
415 					break;
416 				} else if (ret == 0) {
417 					/* Got a signal. */
418 					break;
419 				}
420 			}
421 			error = sctp->sctp_client_errno;
422 			sctp->sctp_client_errno = 0;
423 			mutex_exit(&sctp->sctp_lock);
424 		}
425 
426 		WAKE_SCTP(sctp);
427 		sctp_process_sendq(sctp);
428 		return (error);
429 	}
430 
431 
432 	/* Remove this sctp from all hashes so nobody can find it. */
433 	sctp_closei_local(sctp);
434 	WAKE_SCTP(sctp);
435 	return (error);
436 }
437 
438 void
439 sctp_close(sctp_t *sctp)
440 {
441 	dprint(3, ("sctp_close %p, state %d\n", (void *)sctp,
442 	    sctp->sctp_state));
443 
444 	RUN_SCTP(sctp);
445 	sctp->sctp_detached = 1;
446 	sctp->sctp_ulpd = NULL;
447 	bzero(&sctp->sctp_upcalls, sizeof (sctp_upcalls_t));
448 	bzero(&sctp->sctp_events, sizeof (sctp->sctp_events));
449 
450 	/* If the graceful shutdown has not been completed, just return. */
451 	if (sctp->sctp_state != SCTPS_IDLE) {
452 		WAKE_SCTP(sctp);
453 		return;
454 	}
455 
456 	/*
457 	 * Since sctp_t is in SCTPS_IDLE state, so the only thread which
458 	 * can have a hold on the sctp_t is doing snmp stuff.  Just do
459 	 * a SCTP_REFRELE() here after the SCTP_UNLINK().  It will
460 	 * be freed when the other thread is done.
461 	 */
462 	SCTP_CONDEMNED(sctp);
463 	WAKE_SCTP(sctp);
464 	SCTP_REFRELE(sctp);
465 }
466 
467 /*
468  * Unlink from global list and do the eager close.
469  * Remove the refhold implicit in being on the global list.
470  */
471 void
472 sctp_close_eager(sctp_t *sctp)
473 {
474 	SCTP_CONDEMNED(sctp);
475 	sctp_closei_local(sctp);
476 	SCTP_REFRELE(sctp);
477 }
478 
479 /*
480  * The sctp_t is going away. Remove it from all lists and set it
481  * to SCTPS_IDLE. The caller has to remove it from the
482  * global list. The freeing up of memory is deferred until
483  * sctp_free(). This is needed since a thread in sctp_input() might have
484  * done a SCTP_REFHOLD on this structure before it was removed from the
485  * hashes.
486  */
487 static void
488 sctp_closei_local(sctp_t *sctp)
489 {
490 	mblk_t	*mp;
491 	ire_t	*ire = NULL;
492 	conn_t	*connp = sctp->sctp_connp;
493 
494 	/* Stop and free the timers */
495 	sctp_free_faddr_timers(sctp);
496 	if ((mp = sctp->sctp_heartbeat_mp) != NULL) {
497 		sctp_timer_free(mp);
498 		sctp->sctp_heartbeat_mp = NULL;
499 	}
500 	if ((mp = sctp->sctp_ack_mp) != NULL) {
501 		sctp_timer_free(mp);
502 		sctp->sctp_ack_mp = NULL;
503 	}
504 
505 	/* Set the CONN_CLOSING flag so that IP will not cache IRE again. */
506 	mutex_enter(&connp->conn_lock);
507 	connp->conn_state_flags |= CONN_CLOSING;
508 	ire = connp->conn_ire_cache;
509 	connp->conn_ire_cache = NULL;
510 	mutex_exit(&connp->conn_lock);
511 	if (ire != NULL)
512 		IRE_REFRELE_NOTR(ire);
513 
514 	/* Remove from all hashes. */
515 	sctp_bind_hash_remove(sctp);
516 	sctp_conn_hash_remove(sctp);
517 	sctp_listen_hash_remove(sctp);
518 	sctp->sctp_state = SCTPS_IDLE;
519 
520 	/*
521 	 * Clean up the recvq as much as possible.  All those packets
522 	 * will be silently dropped as this sctp_t is now in idle state.
523 	 */
524 	mutex_enter(&sctp->sctp_recvq_lock);
525 	while ((mp = sctp->sctp_recvq) != NULL) {
526 		mblk_t *ipsec_mp;
527 
528 		sctp->sctp_recvq = mp->b_next;
529 		mp->b_next = NULL;
530 		if ((ipsec_mp = mp->b_prev) != NULL) {
531 			freeb(ipsec_mp);
532 			mp->b_prev = NULL;
533 		}
534 		freemsg(mp);
535 	}
536 	mutex_exit(&sctp->sctp_recvq_lock);
537 }
538 
539 /*
540  * Free memory associated with the sctp/ip header template.
541  */
542 static void
543 sctp_headers_free(sctp_t *sctp)
544 {
545 	if (sctp->sctp_iphc != NULL) {
546 		kmem_free(sctp->sctp_iphc, sctp->sctp_iphc_len);
547 		sctp->sctp_iphc = NULL;
548 		sctp->sctp_ipha = NULL;
549 		sctp->sctp_hdr_len = 0;
550 		sctp->sctp_ip_hdr_len = 0;
551 		sctp->sctp_iphc_len = 0;
552 		sctp->sctp_sctph = NULL;
553 		sctp->sctp_hdr_len = 0;
554 	}
555 	if (sctp->sctp_iphc6 != NULL) {
556 		kmem_free(sctp->sctp_iphc6, sctp->sctp_iphc6_len);
557 		sctp->sctp_iphc6 = NULL;
558 		sctp->sctp_ip6h = NULL;
559 		sctp->sctp_hdr6_len = 0;
560 		sctp->sctp_ip_hdr6_len = 0;
561 		sctp->sctp_iphc6_len = 0;
562 		sctp->sctp_sctph6 = NULL;
563 		sctp->sctp_hdr6_len = 0;
564 	}
565 }
566 
567 static void
568 sctp_free_xmit_data(sctp_t *sctp)
569 {
570 	mblk_t	*ump = NULL;
571 	mblk_t	*nump;
572 	mblk_t	*mp;
573 	mblk_t	*nmp;
574 
575 	sctp->sctp_xmit_unacked = NULL;
576 	ump = sctp->sctp_xmit_head;
577 	sctp->sctp_xmit_tail = sctp->sctp_xmit_head = NULL;
578 free_unsent:
579 	for (; ump != NULL; ump = nump) {
580 		for (mp = ump->b_cont; mp != NULL; mp = nmp) {
581 			nmp = mp->b_next;
582 			mp->b_next = NULL;
583 			mp->b_prev = NULL;
584 			freemsg(mp);
585 		}
586 		ASSERT(DB_REF(ump) == 1);
587 		nump = ump->b_next;
588 		ump->b_next = NULL;
589 		ump->b_prev = NULL;
590 		ump->b_cont = NULL;
591 		freeb(ump);
592 	}
593 	if ((ump = sctp->sctp_xmit_unsent) == NULL) {
594 		ASSERT(sctp->sctp_xmit_unsent_tail == NULL);
595 		return;
596 	}
597 	sctp->sctp_xmit_unsent = sctp->sctp_xmit_unsent_tail = NULL;
598 	goto free_unsent;
599 }
600 
601 /*
602  * Cleanup all the messages in the stream queue and the reassembly lists.
603  * If 'free' is true, then delete the streams as well.
604  */
605 void
606 sctp_instream_cleanup(sctp_t *sctp, boolean_t free)
607 {
608 	int	i;
609 	mblk_t	*mp;
610 	mblk_t	*mp1;
611 
612 	if (sctp->sctp_instr != NULL) {
613 		/* walk thru and flush out anything remaining in the Q */
614 		for (i = 0; i < sctp->sctp_num_istr; i++) {
615 			mp = sctp->sctp_instr[i].istr_msgs;
616 			while (mp != NULL) {
617 				mp1 = mp->b_next;
618 				mp->b_next = mp->b_prev = NULL;
619 				freemsg(mp);
620 				mp = mp1;
621 			}
622 			sctp->sctp_instr[i].istr_msgs = NULL;
623 			sctp_free_reass((sctp->sctp_instr) + i);
624 			sctp->sctp_instr[i].nextseq = 0;
625 		}
626 		if (free) {
627 			kmem_free(sctp->sctp_instr,
628 			    sizeof (*sctp->sctp_instr) * sctp->sctp_num_istr);
629 			sctp->sctp_instr = NULL;
630 			sctp->sctp_num_istr = 0;
631 		}
632 	}
633 	/* un-ordered fragments */
634 	if (sctp->sctp_uo_frags != NULL) {
635 		for (mp = sctp->sctp_uo_frags; mp != NULL; mp = mp1) {
636 			mp1 = mp->b_next;
637 			mp->b_next = mp->b_prev = NULL;
638 			freemsg(mp);
639 		}
640 	}
641 }
642 
643 /*
644  * Last reference to the sctp_t is gone. Free all memory associated with it.
645  * Called from SCTP_REFRELE. Called inline in sctp_close()
646  */
647 void
648 sctp_free(conn_t *connp)
649 {
650 	sctp_t *sctp = CONN2SCTP(connp);
651 	int		cnt;
652 
653 	/* Unlink it from the global list */
654 	SCTP_UNLINK(sctp);
655 
656 	ASSERT(connp->conn_ref == 0);
657 	ASSERT(connp->conn_ulp == IPPROTO_SCTP);
658 	ASSERT(!MUTEX_HELD(&sctp->sctp_reflock));
659 	ASSERT(sctp->sctp_refcnt == 0);
660 
661 	ASSERT(sctp->sctp_ptpbhn == NULL && sctp->sctp_bind_hash == NULL);
662 	ASSERT(sctp->sctp_conn_hash_next == NULL &&
663 	    sctp->sctp_conn_hash_prev == NULL);
664 
665 
666 	/* Free up all the resources. */
667 
668 	/* blow away sctp stream management */
669 	if (sctp->sctp_ostrcntrs != NULL) {
670 		kmem_free(sctp->sctp_ostrcntrs,
671 		    sizeof (uint16_t) * sctp->sctp_num_ostr);
672 		sctp->sctp_ostrcntrs = NULL;
673 	}
674 	sctp_instream_cleanup(sctp, B_TRUE);
675 
676 	/* Remove all data transfer resources. */
677 	sctp->sctp_istr_nmsgs = 0;
678 	sctp->sctp_rxqueued = 0;
679 	sctp_free_xmit_data(sctp);
680 	sctp->sctp_unacked = 0;
681 	sctp->sctp_unsent = 0;
682 	if (sctp->sctp_cxmit_list != NULL)
683 		sctp_asconf_free_cxmit(sctp, NULL);
684 
685 	sctp->sctp_lastdata = NULL;
686 
687 	/* Clear out default xmit settings */
688 	sctp->sctp_def_stream = 0;
689 	sctp->sctp_def_flags = 0;
690 	sctp->sctp_def_ppid = 0;
691 	sctp->sctp_def_context = 0;
692 	sctp->sctp_def_timetolive = 0;
693 
694 	if (sctp->sctp_sack_info != NULL) {
695 		sctp_free_set(sctp->sctp_sack_info);
696 		sctp->sctp_sack_info = NULL;
697 	}
698 	sctp->sctp_sack_gaps = 0;
699 
700 	if (sctp->sctp_cookie_mp != NULL) {
701 		freemsg(sctp->sctp_cookie_mp);
702 		sctp->sctp_cookie_mp = NULL;
703 	}
704 
705 	/* Remove all the address resources. */
706 	sctp_zap_addrs(sctp);
707 	for (cnt = 0; cnt < SCTP_IPIF_HASH; cnt++) {
708 		ASSERT(sctp->sctp_saddrs[cnt].ipif_count == 0);
709 		list_destroy(&sctp->sctp_saddrs[cnt].sctp_ipif_list);
710 	}
711 
712 	ip6_pkt_free(&sctp->sctp_sticky_ipp);
713 
714 	if (sctp->sctp_hopopts != NULL) {
715 		mi_free(sctp->sctp_hopopts);
716 		sctp->sctp_hopopts = NULL;
717 		sctp->sctp_hopoptslen = 0;
718 	}
719 	ASSERT(sctp->sctp_hopoptslen == 0);
720 	if (sctp->sctp_dstopts != NULL) {
721 		mi_free(sctp->sctp_dstopts);
722 		sctp->sctp_dstopts = NULL;
723 		sctp->sctp_dstoptslen = 0;
724 	}
725 	ASSERT(sctp->sctp_dstoptslen == 0);
726 	if (sctp->sctp_rtdstopts != NULL) {
727 		mi_free(sctp->sctp_rtdstopts);
728 		sctp->sctp_rtdstopts = NULL;
729 		sctp->sctp_rtdstoptslen = 0;
730 	}
731 	ASSERT(sctp->sctp_rtdstoptslen == 0);
732 	if (sctp->sctp_rthdr != NULL) {
733 		mi_free(sctp->sctp_rthdr);
734 		sctp->sctp_rthdr = NULL;
735 		sctp->sctp_rthdrlen = 0;
736 	}
737 	ASSERT(sctp->sctp_rthdrlen == 0);
738 	sctp_headers_free(sctp);
739 
740 	sctp->sctp_shutdown_faddr = NULL;
741 
742 	/* Clear all the bitfields. */
743 	bzero(&sctp->sctp_bits, sizeof (sctp->sctp_bits));
744 
745 	/* It is time to update the global statistics. */
746 	UPDATE_MIB(&sctp_mib, sctpOutSCTPPkts, sctp->sctp_opkts);
747 	UPDATE_MIB(&sctp_mib, sctpOutCtrlChunks, sctp->sctp_obchunks);
748 	UPDATE_MIB(&sctp_mib, sctpOutOrderChunks, sctp->sctp_odchunks);
749 	UPDATE_MIB(&sctp_mib, sctpOutUnorderChunks, sctp->sctp_oudchunks);
750 	UPDATE_MIB(&sctp_mib, sctpRetransChunks, sctp->sctp_rxtchunks);
751 	UPDATE_MIB(&sctp_mib, sctpInSCTPPkts, sctp->sctp_ipkts);
752 	UPDATE_MIB(&sctp_mib, sctpInCtrlChunks, sctp->sctp_ibchunks);
753 	UPDATE_MIB(&sctp_mib, sctpInOrderChunks, sctp->sctp_idchunks);
754 	UPDATE_MIB(&sctp_mib, sctpInUnorderChunks, sctp->sctp_iudchunks);
755 	UPDATE_MIB(&sctp_mib, sctpFragUsrMsgs, sctp->sctp_fragdmsgs);
756 	UPDATE_MIB(&sctp_mib, sctpReasmUsrMsgs, sctp->sctp_reassmsgs);
757 	sctp->sctp_opkts = 0;
758 	sctp->sctp_obchunks = 0;
759 	sctp->sctp_odchunks = 0;
760 	sctp->sctp_oudchunks = 0;
761 	sctp->sctp_rxtchunks = 0;
762 	sctp->sctp_ipkts = 0;
763 	sctp->sctp_ibchunks = 0;
764 	sctp->sctp_idchunks = 0;
765 	sctp->sctp_iudchunks = 0;
766 	sctp->sctp_fragdmsgs = 0;
767 	sctp->sctp_reassmsgs = 0;
768 
769 	sctp->sctp_autoclose = 0;
770 	sctp->sctp_tx_adaption_code = 0;
771 
772 	sctp->sctp_v6label_len = 0;
773 	sctp->sctp_v4label_len = 0;
774 
775 	/* Clean up conn_t stuff */
776 	connp->conn_policy_cached = B_FALSE;
777 	if (connp->conn_latch != NULL) {
778 		IPLATCH_REFRELE(connp->conn_latch);
779 		connp->conn_latch = NULL;
780 	}
781 	if (connp->conn_policy != NULL) {
782 		IPPH_REFRELE(connp->conn_policy);
783 		connp->conn_policy = NULL;
784 	}
785 	if (connp->conn_ipsec_opt_mp != NULL) {
786 		freemsg(connp->conn_ipsec_opt_mp);
787 		connp->conn_ipsec_opt_mp = NULL;
788 	}
789 	if (connp->conn_cred != NULL) {
790 		crfree(connp->conn_cred);
791 		connp->conn_cred = NULL;
792 	}
793 
794 	kmem_cache_free(sctp_conn_cache, connp);
795 }
796 
797 /* Diagnostic routine used to return a string associated with the sctp state. */
798 char *
799 sctp_display(sctp_t *sctp, char *sup_buf)
800 {
801 	char	*buf;
802 	char	buf1[30];
803 	static char	priv_buf[INET6_ADDRSTRLEN * 2 + 80];
804 	char	*cp;
805 
806 	if (sctp == NULL)
807 		return ("NULL_SCTP");
808 
809 	buf = (sup_buf != NULL) ? sup_buf : priv_buf;
810 
811 	switch (sctp->sctp_state) {
812 	case SCTPS_IDLE:
813 		cp = "SCTP_IDLE";
814 		break;
815 	case SCTPS_BOUND:
816 		cp = "SCTP_BOUND";
817 		break;
818 	case SCTPS_LISTEN:
819 		cp = "SCTP_LISTEN";
820 		break;
821 	case SCTPS_COOKIE_WAIT:
822 		cp = "SCTP_COOKIE_WAIT";
823 		break;
824 	case SCTPS_COOKIE_ECHOED:
825 		cp = "SCTP_COOKIE_ECHOED";
826 		break;
827 	case SCTPS_ESTABLISHED:
828 		cp = "SCTP_ESTABLISHED";
829 		break;
830 	case SCTPS_SHUTDOWN_PENDING:
831 		cp = "SCTP_SHUTDOWN_PENDING";
832 		break;
833 	case SCTPS_SHUTDOWN_SENT:
834 		cp = "SCTPS_SHUTDOWN_SENT";
835 		break;
836 	case SCTPS_SHUTDOWN_RECEIVED:
837 		cp = "SCTPS_SHUTDOWN_RECEIVED";
838 		break;
839 	case SCTPS_SHUTDOWN_ACK_SENT:
840 		cp = "SCTPS_SHUTDOWN_ACK_SENT";
841 		break;
842 	default:
843 		(void) mi_sprintf(buf1, "SCTPUnkState(%d)", sctp->sctp_state);
844 		cp = buf1;
845 		break;
846 	}
847 	(void) mi_sprintf(buf, "[%u, %u] %s",
848 	    ntohs(sctp->sctp_lport), ntohs(sctp->sctp_fport), cp);
849 
850 	return (buf);
851 }
852 
853 /*
854  * Initialize protocol control block. If a parent exists, inherit
855  * all values set through setsockopt().
856  */
857 static int
858 sctp_init_values(sctp_t *sctp, sctp_t *psctp, int sleep)
859 {
860 	int	err;
861 	int	cnt;
862 	conn_t 	*connp, *pconnp;
863 
864 	ASSERT((sctp->sctp_family == AF_INET &&
865 	    sctp->sctp_ipversion == IPV4_VERSION) ||
866 	    (sctp->sctp_family == AF_INET6 &&
867 	    (sctp->sctp_ipversion == IPV4_VERSION ||
868 	    sctp->sctp_ipversion == IPV6_VERSION)));
869 
870 	sctp->sctp_nsaddrs = 0;
871 	for (cnt = 0; cnt < SCTP_IPIF_HASH; cnt++) {
872 		sctp->sctp_saddrs[cnt].ipif_count = 0;
873 		list_create(&sctp->sctp_saddrs[cnt].sctp_ipif_list,
874 		    sizeof (sctp_saddr_ipif_t), offsetof(sctp_saddr_ipif_t,
875 		    saddr_ipif));
876 	}
877 	sctp->sctp_ports = 0;
878 	sctp->sctp_running = B_FALSE;
879 	sctp->sctp_state = SCTPS_IDLE;
880 
881 	sctp->sctp_refcnt = 1;
882 
883 	sctp->sctp_strikes = 0;
884 
885 	sctp->sctp_last_mtu_probe = lbolt64;
886 	sctp->sctp_mtu_probe_intvl = sctp_mtu_probe_interval;
887 
888 	sctp->sctp_sack_gaps = 0;
889 	sctp->sctp_sack_toggle = 2;
890 
891 	if (psctp != NULL) {
892 		/*
893 		 * Inherit from parent
894 		 */
895 		sctp->sctp_iphc = kmem_zalloc(psctp->sctp_iphc_len,
896 		    KM_NOSLEEP);
897 		if (sctp->sctp_iphc == NULL)
898 			return (ENOMEM);
899 		sctp->sctp_iphc_len = psctp->sctp_iphc_len;
900 		sctp->sctp_hdr_len = psctp->sctp_hdr_len;
901 
902 		sctp->sctp_iphc6 = kmem_zalloc(psctp->sctp_iphc6_len,
903 		    KM_NOSLEEP);
904 		if (sctp->sctp_iphc6 == NULL) {
905 			sctp->sctp_iphc6_len = 0;
906 			return (ENOMEM);
907 		}
908 		sctp->sctp_iphc6_len = psctp->sctp_iphc6_len;
909 		sctp->sctp_hdr6_len = psctp->sctp_hdr6_len;
910 
911 		sctp->sctp_ip_hdr_len = psctp->sctp_ip_hdr_len;
912 		sctp->sctp_ip_hdr6_len = psctp->sctp_ip_hdr6_len;
913 
914 		/*
915 		 * Copy the IP+SCTP header templates from listener
916 		 */
917 		bcopy(psctp->sctp_iphc, sctp->sctp_iphc,
918 		    psctp->sctp_hdr_len);
919 		sctp->sctp_ipha = (ipha_t *)sctp->sctp_iphc;
920 		sctp->sctp_sctph = (sctp_hdr_t *)(sctp->sctp_iphc +
921 		    sctp->sctp_ip_hdr_len);
922 
923 		bcopy(psctp->sctp_iphc6, sctp->sctp_iphc6,
924 		    psctp->sctp_hdr6_len);
925 		if (((ip6i_t *)(sctp->sctp_iphc6))->ip6i_nxt == IPPROTO_RAW) {
926 			sctp->sctp_ip6h = (ip6_t *)(sctp->sctp_iphc6 +
927 			    sizeof (ip6i_t));
928 		} else {
929 			sctp->sctp_ip6h = (ip6_t *)sctp->sctp_iphc6;
930 		}
931 		sctp->sctp_sctph6 = (sctp_hdr_t *)(sctp->sctp_iphc6 +
932 		    sctp->sctp_ip_hdr6_len);
933 
934 		sctp->sctp_cookie_lifetime = psctp->sctp_cookie_lifetime;
935 		sctp->sctp_xmit_lowater = psctp->sctp_xmit_lowater;
936 		sctp->sctp_xmit_hiwater = psctp->sctp_xmit_hiwater;
937 		sctp->sctp_cwnd_max = psctp->sctp_cwnd_max;
938 		sctp->sctp_rwnd = psctp->sctp_rwnd;
939 		sctp->sctp_irwnd = psctp->sctp_rwnd;
940 
941 		sctp->sctp_rto_max = psctp->sctp_rto_max;
942 		sctp->sctp_init_rto_max = psctp->sctp_init_rto_max;
943 		sctp->sctp_rto_min = psctp->sctp_rto_min;
944 		sctp->sctp_rto_initial = psctp->sctp_rto_initial;
945 		sctp->sctp_pa_max_rxt = psctp->sctp_pa_max_rxt;
946 		sctp->sctp_pp_max_rxt = psctp->sctp_pp_max_rxt;
947 		sctp->sctp_max_init_rxt = psctp->sctp_max_init_rxt;
948 
949 		sctp->sctp_def_stream = psctp->sctp_def_stream;
950 		sctp->sctp_def_flags = psctp->sctp_def_flags;
951 		sctp->sctp_def_ppid = psctp->sctp_def_ppid;
952 		sctp->sctp_def_context = psctp->sctp_def_context;
953 		sctp->sctp_def_timetolive = psctp->sctp_def_timetolive;
954 
955 		sctp->sctp_num_istr = psctp->sctp_num_istr;
956 		sctp->sctp_num_ostr = psctp->sctp_num_ostr;
957 
958 		sctp->sctp_hb_interval = psctp->sctp_hb_interval;
959 		sctp->sctp_autoclose = psctp->sctp_autoclose;
960 		sctp->sctp_tx_adaption_code = psctp->sctp_tx_adaption_code;
961 
962 		/* xxx should be a better way to copy these flags xxx */
963 		sctp->sctp_debug = psctp->sctp_debug;
964 		sctp->sctp_bound_to_all = psctp->sctp_bound_to_all;
965 		sctp->sctp_cansleep = psctp->sctp_cansleep;
966 		sctp->sctp_send_adaption = psctp->sctp_send_adaption;
967 		sctp->sctp_ndelay = psctp->sctp_ndelay;
968 		sctp->sctp_events = psctp->sctp_events;
969 		sctp->sctp_ipv6_recvancillary = psctp->sctp_ipv6_recvancillary;
970 
971 		/* Copy IP-layer options */
972 		connp = sctp->sctp_connp;
973 		pconnp = psctp->sctp_connp;
974 
975 		connp->conn_broadcast = pconnp->conn_broadcast;
976 		connp->conn_loopback = pconnp->conn_loopback;
977 		connp->conn_dontroute = pconnp->conn_dontroute;
978 		connp->conn_reuseaddr = pconnp->conn_reuseaddr;
979 
980 	} else {
981 		/*
982 		 * Initialize the header template
983 		 */
984 		if ((err = sctp_header_init_ipv4(sctp, sleep)) != 0) {
985 			return (err);
986 		}
987 		if ((err = sctp_header_init_ipv6(sctp, sleep)) != 0) {
988 			return (err);
989 		}
990 
991 		/*
992 		 * Set to system defaults
993 		 */
994 		sctp->sctp_cookie_lifetime = MSEC_TO_TICK(sctp_cookie_life);
995 		sctp->sctp_xmit_lowater = sctp_xmit_lowat;
996 		sctp->sctp_xmit_hiwater = sctp_xmit_hiwat;
997 		sctp->sctp_cwnd_max = sctp_cwnd_max_;
998 		sctp->sctp_rwnd = sctp_recv_hiwat;
999 		sctp->sctp_irwnd = sctp->sctp_rwnd;
1000 		sctp->sctp_rto_max = MSEC_TO_TICK(sctp_rto_maxg);
1001 		sctp->sctp_init_rto_max = sctp->sctp_rto_max;
1002 		sctp->sctp_rto_min = MSEC_TO_TICK(sctp_rto_ming);
1003 		sctp->sctp_rto_initial = MSEC_TO_TICK(sctp_rto_initialg);
1004 		sctp->sctp_pa_max_rxt = sctp_pa_max_retr;
1005 		sctp->sctp_pp_max_rxt = sctp_pp_max_retr;
1006 		sctp->sctp_max_init_rxt = sctp_max_init_retr;
1007 
1008 		sctp->sctp_num_istr = sctp_max_in_streams;
1009 		sctp->sctp_num_ostr = sctp_initial_out_streams;
1010 
1011 		sctp->sctp_hb_interval = MSEC_TO_TICK(sctp_heartbeat_interval);
1012 	}
1013 	sctp->sctp_understands_asconf = B_TRUE;
1014 	sctp->sctp_understands_addip = B_TRUE;
1015 	sctp->sctp_prsctp_aware = B_FALSE;
1016 
1017 	sctp->sctp_connp->conn_ref = 1;
1018 	sctp->sctp_connp->conn_fully_bound = B_FALSE;
1019 
1020 	sctp->sctp_prsctpdrop = 0;
1021 	sctp->sctp_msgcount = 0;
1022 
1023 	return (0);
1024 }
1025 
1026 /*
1027  * Extracts the init tag from an INIT chunk and checks if it matches
1028  * the sctp's verification tag. Returns 0 if it doesn't match, 1 if
1029  * it does.
1030  */
1031 static boolean_t
1032 sctp_icmp_verf(sctp_t *sctp, sctp_hdr_t *sh, mblk_t *mp)
1033 {
1034 	sctp_chunk_hdr_t *sch;
1035 	uint32_t verf, *vp;
1036 
1037 	sch = (sctp_chunk_hdr_t *)(sh + 1);
1038 	vp = (uint32_t *)(sch + 1);
1039 
1040 	/* Need at least the data chunk hdr and the first 4 bytes of INIT */
1041 	if ((unsigned char *)(vp + 1) > mp->b_wptr) {
1042 		return (B_FALSE);
1043 	}
1044 
1045 	bcopy(vp, &verf, sizeof (verf));
1046 
1047 	if (verf == sctp->sctp_lvtag) {
1048 		return (B_TRUE);
1049 	}
1050 	return (B_FALSE);
1051 }
1052 
1053 /*
1054  * sctp_icmp_error is called by sctp_input() to process ICMP error messages
1055  * passed up by IP.  The queue is the default queue.  We need to find a sctp_t
1056  * that corresponds to the returned datagram.  Passes the message back in on
1057  * the correct queue once it has located the connection.
1058  * Assumes that IP has pulled up everything up to and including
1059  * the ICMP header.
1060  */
1061 void
1062 sctp_icmp_error(sctp_t *sctp, mblk_t *mp)
1063 {
1064 	icmph_t *icmph;
1065 	ipha_t	*ipha;
1066 	int	iph_hdr_length;
1067 	sctp_hdr_t *sctph;
1068 	mblk_t *first_mp;
1069 	uint32_t new_mtu;
1070 	in6_addr_t dst;
1071 	sctp_faddr_t *fp;
1072 
1073 	dprint(1, ("sctp_icmp_error: sctp=%p, mp=%p\n", (void *)sctp,
1074 	    (void *)mp));
1075 
1076 	first_mp = mp;
1077 
1078 	ipha = (ipha_t *)mp->b_rptr;
1079 	if (IPH_HDR_VERSION(ipha) != IPV4_VERSION) {
1080 		ASSERT(IPH_HDR_VERSION(ipha) == IPV6_VERSION);
1081 		sctp_icmp_error_ipv6(sctp, first_mp);
1082 		return;
1083 	}
1084 
1085 	iph_hdr_length = IPH_HDR_LENGTH(ipha);
1086 	icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length];
1087 	ipha = (ipha_t *)&icmph[1];
1088 	iph_hdr_length = IPH_HDR_LENGTH(ipha);
1089 	sctph = (sctp_hdr_t *)((char *)ipha + iph_hdr_length);
1090 	if ((uchar_t *)(sctph + 1) >= mp->b_wptr) {
1091 		/* not enough data for SCTP header */
1092 		freemsg(first_mp);
1093 		return;
1094 	}
1095 
1096 	switch (icmph->icmph_type) {
1097 	case ICMP_DEST_UNREACHABLE:
1098 		switch (icmph->icmph_code) {
1099 		case ICMP_FRAGMENTATION_NEEDED:
1100 			/*
1101 			 * Reduce the MSS based on the new MTU.  This will
1102 			 * eliminate any fragmentation locally.
1103 			 * N.B.  There may well be some funny side-effects on
1104 			 * the local send policy and the remote receive policy.
1105 			 * Pending further research, we provide
1106 			 * sctp_ignore_path_mtu just in case this proves
1107 			 * disastrous somewhere.
1108 			 *
1109 			 * After updating the MSS, retransmit part of the
1110 			 * dropped segment using the new mss by calling
1111 			 * sctp_wput_slow().  Need to adjust all those
1112 			 * params to make sure sctp_wput_slow() work properly.
1113 			 */
1114 			if (sctp_ignore_path_mtu)
1115 				break;
1116 
1117 			/* find the offending faddr */
1118 			IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, &dst);
1119 			fp = sctp_lookup_faddr(sctp, &dst);
1120 			if (fp == NULL) {
1121 				break;
1122 			}
1123 
1124 			new_mtu = ntohs(icmph->icmph_du_mtu);
1125 
1126 			if (new_mtu - sctp->sctp_hdr_len >= fp->sfa_pmss)
1127 				break;
1128 
1129 			/*
1130 			 * Make sure that sfa_pmss is a multiple of
1131 			 * SCTP_ALIGN.
1132 			 */
1133 			fp->sfa_pmss = (new_mtu - sctp->sctp_hdr_len) &
1134 				~(SCTP_ALIGN - 1);
1135 			fp->pmtu_discovered = 1;
1136 
1137 			break;
1138 		case ICMP_PORT_UNREACHABLE:
1139 		case ICMP_PROTOCOL_UNREACHABLE:
1140 			switch (sctp->sctp_state) {
1141 			case SCTPS_COOKIE_WAIT:
1142 			case SCTPS_COOKIE_ECHOED:
1143 				/* make sure the verification tag matches */
1144 				if (!sctp_icmp_verf(sctp, sctph, mp)) {
1145 					break;
1146 				}
1147 				BUMP_MIB(&sctp_mib, sctpAborted);
1148 				sctp_clean_death(sctp, ECONNREFUSED);
1149 				break;
1150 			}
1151 			break;
1152 		case ICMP_HOST_UNREACHABLE:
1153 		case ICMP_NET_UNREACHABLE:
1154 			/* Record the error in case we finally time out. */
1155 			sctp->sctp_client_errno = (icmph->icmph_code ==
1156 			    ICMP_HOST_UNREACHABLE) ? EHOSTUNREACH : ENETUNREACH;
1157 			break;
1158 		default:
1159 			break;
1160 		}
1161 		break;
1162 	case ICMP_SOURCE_QUENCH: {
1163 		/* Reduce the sending rate as if we got a retransmit timeout */
1164 		break;
1165 	}
1166 	}
1167 	freemsg(first_mp);
1168 }
1169 
1170 /*
1171  * sctp_icmp_error_ipv6() is called by sctp_icmp_error() to process ICMPv6
1172  * error messages passed up by IP.
1173  * Assumes that IP has pulled up all the extension headers as well
1174  * as the ICMPv6 header.
1175  */
1176 static void
1177 sctp_icmp_error_ipv6(sctp_t *sctp, mblk_t *mp)
1178 {
1179 	icmp6_t *icmp6;
1180 	ip6_t	*ip6h;
1181 	uint16_t	iph_hdr_length;
1182 	sctp_hdr_t *sctpha;
1183 	uint8_t	*nexthdrp;
1184 	uint32_t new_mtu;
1185 	sctp_faddr_t *fp;
1186 
1187 	ip6h = (ip6_t *)mp->b_rptr;
1188 	iph_hdr_length = (ip6h->ip6_nxt != IPPROTO_SCTP) ?
1189 	    ip_hdr_length_v6(mp, ip6h) : IPV6_HDR_LEN;
1190 
1191 	icmp6 = (icmp6_t *)&mp->b_rptr[iph_hdr_length];
1192 	ip6h = (ip6_t *)&icmp6[1];
1193 	if (!ip_hdr_length_nexthdr_v6(mp, ip6h, &iph_hdr_length, &nexthdrp)) {
1194 		freemsg(mp);
1195 		return;
1196 	}
1197 	ASSERT(*nexthdrp == IPPROTO_SCTP);
1198 
1199 	/* XXX need ifindex to find connection */
1200 	sctpha = (sctp_hdr_t *)((char *)ip6h + iph_hdr_length);
1201 	if ((uchar_t *)sctpha >= mp->b_wptr) {
1202 		/* not enough data for SCTP header */
1203 		freemsg(mp);
1204 		return;
1205 	}
1206 	switch (icmp6->icmp6_type) {
1207 	case ICMP6_PACKET_TOO_BIG:
1208 		/*
1209 		 * Reduce the MSS based on the new MTU.  This will
1210 		 * eliminate any fragmentation locally.
1211 		 * N.B.  There may well be some funny side-effects on
1212 		 * the local send policy and the remote receive policy.
1213 		 * Pending further research, we provide
1214 		 * sctp_ignore_path_mtu just in case this proves
1215 		 * disastrous somewhere.
1216 		 *
1217 		 * After updating the MSS, retransmit part of the
1218 		 * dropped segment using the new mss by calling
1219 		 * sctp_wput_slow().  Need to adjust all those
1220 		 * params to make sure sctp_wput_slow() work properly.
1221 		 */
1222 		if (sctp_ignore_path_mtu)
1223 			break;
1224 
1225 		/* find the offending faddr */
1226 		fp = sctp_lookup_faddr(sctp, &ip6h->ip6_dst);
1227 		if (fp == NULL) {
1228 			break;
1229 		}
1230 
1231 		new_mtu = ntohs(icmp6->icmp6_mtu);
1232 
1233 		if (new_mtu - sctp->sctp_hdr6_len >= fp->sfa_pmss)
1234 			break;
1235 
1236 		/* Make sure that sfa_pmss is a multiple of SCTP_ALIGN. */
1237 		fp->sfa_pmss = (new_mtu - sctp->sctp_hdr6_len) &
1238 			~(SCTP_ALIGN - 1);
1239 		fp->pmtu_discovered = 1;
1240 
1241 		break;
1242 
1243 	case ICMP6_DST_UNREACH:
1244 		switch (icmp6->icmp6_code) {
1245 		case ICMP6_DST_UNREACH_NOPORT:
1246 			/* make sure the verification tag matches */
1247 			if (!sctp_icmp_verf(sctp, sctpha, mp)) {
1248 				break;
1249 			}
1250 			if (sctp->sctp_state == SCTPS_COOKIE_WAIT ||
1251 			    sctp->sctp_state == SCTPS_COOKIE_ECHOED) {
1252 				BUMP_MIB(&sctp_mib, sctpAborted);
1253 				sctp_clean_death(sctp, ECONNREFUSED);
1254 			}
1255 			break;
1256 
1257 		case ICMP6_DST_UNREACH_ADMIN:
1258 		case ICMP6_DST_UNREACH_NOROUTE:
1259 		case ICMP6_DST_UNREACH_NOTNEIGHBOR:
1260 		case ICMP6_DST_UNREACH_ADDR:
1261 			/* Record the error in case we finally time out. */
1262 			sctp->sctp_client_errno = EHOSTUNREACH;
1263 			break;
1264 		default:
1265 			break;
1266 		}
1267 		break;
1268 
1269 	case ICMP6_PARAM_PROB:
1270 		/* If this corresponds to an ICMP_PROTOCOL_UNREACHABLE */
1271 		if (icmp6->icmp6_code == ICMP6_PARAMPROB_NEXTHEADER &&
1272 		    (uchar_t *)ip6h + icmp6->icmp6_pptr ==
1273 		    (uchar_t *)nexthdrp) {
1274 			/* make sure the verification tag matches */
1275 			if (!sctp_icmp_verf(sctp, sctpha, mp)) {
1276 				break;
1277 			}
1278 			if (sctp->sctp_state == SCTPS_COOKIE_WAIT) {
1279 				BUMP_MIB(&sctp_mib, sctpAborted);
1280 				sctp_clean_death(sctp, ECONNREFUSED);
1281 			}
1282 			break;
1283 		}
1284 		break;
1285 
1286 	case ICMP6_TIME_EXCEEDED:
1287 	default:
1288 		break;
1289 	}
1290 	freemsg(mp);
1291 }
1292 
1293 /*
1294  * Called by sockfs to create a new sctp instance.
1295  *
1296  * If parent pointer is passed in, inherit settings from it.
1297  */
1298 sctp_t *
1299 sctp_create(void *sctp_ulpd, sctp_t *parent, int family, int flags,
1300     const sctp_upcalls_t *sctp_upcalls, sctp_sockbuf_limits_t *sbl,
1301     cred_t *credp)
1302 {
1303 	sctp_t		*sctp, *psctp;
1304 	conn_t		*sctp_connp;
1305 	mblk_t		*ack_mp, *hb_mp;
1306 	int		sleep = flags & SCTP_CAN_BLOCK ? KM_SLEEP : KM_NOSLEEP;
1307 
1308 	/* User must supply a credential. */
1309 	if (credp == NULL)
1310 		return (NULL);
1311 
1312 	if ((sctp_connp = ipcl_conn_create(IPCL_SCTPCONN, sleep)) == NULL) {
1313 		SCTP_KSTAT(sctp_conn_create);
1314 		return (NULL);
1315 	}
1316 	sctp_connp->conn_ulp_labeled = is_system_labeled();
1317 
1318 	psctp = (sctp_t *)parent;
1319 
1320 	sctp = CONN2SCTP(sctp_connp);
1321 
1322 	if ((ack_mp = sctp_timer_alloc(sctp, sctp_ack_timer)) == NULL ||
1323 	    (hb_mp = sctp_timer_alloc(sctp, sctp_heartbeat_timer)) == NULL) {
1324 		if (ack_mp != NULL)
1325 			freeb(ack_mp);
1326 		kmem_cache_free(sctp_conn_cache, sctp_connp);
1327 		return (NULL);
1328 	}
1329 
1330 	sctp->sctp_ack_mp = ack_mp;
1331 	sctp->sctp_heartbeat_mp = hb_mp;
1332 
1333 	switch (family) {
1334 	case AF_INET6:
1335 		sctp_connp->conn_af_isv6 = B_TRUE;
1336 		sctp->sctp_ipversion = IPV6_VERSION;
1337 		sctp->sctp_family = AF_INET6;
1338 		break;
1339 
1340 	case AF_INET:
1341 		sctp_connp->conn_af_isv6 = B_FALSE;
1342 		sctp_connp->conn_pkt_isv6 = B_FALSE;
1343 		sctp->sctp_ipversion = IPV4_VERSION;
1344 		sctp->sctp_family = AF_INET;
1345 		break;
1346 	default:
1347 		ASSERT(0);
1348 		break;
1349 	}
1350 	if (sctp_init_values(sctp, psctp, sleep) != 0) {
1351 		freeb(ack_mp);
1352 		freeb(hb_mp);
1353 		kmem_cache_free(sctp_conn_cache, sctp_connp);
1354 		return (NULL);
1355 	}
1356 	sctp->sctp_cansleep = ((flags & SCTP_CAN_BLOCK) == SCTP_CAN_BLOCK);
1357 
1358 	sctp->sctp_mss = sctp_initial_mtu - ((family == AF_INET6) ?
1359 		sctp->sctp_hdr6_len : sctp->sctp_hdr_len);
1360 
1361 	if (psctp != NULL) {
1362 		RUN_SCTP(psctp);
1363 		/*
1364 		 * Inherit local address list, local port. Parent is either
1365 		 * in SCTPS_BOUND, or SCTPS_LISTEN state.
1366 		 */
1367 		ASSERT((psctp->sctp_state == SCTPS_BOUND) ||
1368 		    (psctp->sctp_state == SCTPS_LISTEN));
1369 		if (sctp_dup_saddrs(psctp, sctp, sleep)) {
1370 			WAKE_SCTP(psctp);
1371 			freeb(ack_mp);
1372 			freeb(hb_mp);
1373 			sctp_headers_free(sctp);
1374 			kmem_cache_free(sctp_conn_cache, sctp_connp);
1375 			return (NULL);
1376 		}
1377 
1378 		/*
1379 		 * If the parent is specified, it'll be immediatelly
1380 		 * followed by sctp_connect(). So don't add this guy to
1381 		 * bind hash.
1382 		 */
1383 		sctp->sctp_lport = psctp->sctp_lport;
1384 		sctp->sctp_state = SCTPS_BOUND;
1385 		sctp->sctp_allzones = psctp->sctp_allzones;
1386 		sctp->sctp_zoneid = psctp->sctp_zoneid;
1387 		WAKE_SCTP(psctp);
1388 	} else {
1389 		sctp->sctp_zoneid = getzoneid();
1390 	}
1391 
1392 	sctp_connp->conn_cred = credp;
1393 	crhold(credp);
1394 
1395 	/*
1396 	 * If the caller has the process-wide flag set, then default to MAC
1397 	 * exempt mode.  This allows read-down to unlabeled hosts.
1398 	 */
1399 	if (getpflags(NET_MAC_AWARE, credp) != 0)
1400 		sctp_connp->conn_mac_exempt = B_TRUE;
1401 
1402 	/* Initialize SCTP instance values,  our verf tag must never be 0 */
1403 	(void) random_get_pseudo_bytes((uint8_t *)&sctp->sctp_lvtag,
1404 	    sizeof (sctp->sctp_lvtag));
1405 	if (sctp->sctp_lvtag == 0)
1406 		sctp->sctp_lvtag = (uint32_t)gethrtime();
1407 	ASSERT(sctp->sctp_lvtag != 0);
1408 
1409 	sctp->sctp_ltsn = sctp->sctp_lvtag + 1;
1410 	sctp->sctp_lcsn = sctp->sctp_ltsn;
1411 	sctp->sctp_recovery_tsn = sctp->sctp_lastack_rxd = sctp->sctp_ltsn - 1;
1412 	sctp->sctp_adv_pap = sctp->sctp_lastack_rxd;
1413 
1414 	/* Information required by upper layer */
1415 	if (sctp_ulpd != NULL) {
1416 		sctp->sctp_ulpd = sctp_ulpd;
1417 
1418 		ASSERT(sctp_upcalls != NULL);
1419 		bcopy(sctp_upcalls, &sctp->sctp_upcalls,
1420 		    sizeof (sctp_upcalls_t));
1421 		ASSERT(sbl != NULL);
1422 		/* Fill in the socket buffer limits for sctpsockfs */
1423 		sbl->sbl_txlowat = sctp->sctp_xmit_lowater;
1424 		sbl->sbl_txbuf = sctp->sctp_xmit_hiwater;
1425 		sbl->sbl_rxbuf = sctp->sctp_rwnd;
1426 		sbl->sbl_rxlowat = SCTP_RECV_LOWATER;
1427 	}
1428 	/* If no sctp_ulpd, must be creating the default sctp */
1429 	ASSERT(sctp_ulpd != NULL || gsctp == NULL);
1430 
1431 	/* Insert this in the global list. */
1432 	SCTP_LINK(sctp);
1433 
1434 	return (sctp);
1435 }
1436 
1437 void
1438 sctp_ddi_init(void)
1439 {
1440 	/* Initialize locks */
1441 	mutex_init(&sctp_g_lock, NULL, MUTEX_DEFAULT, NULL);
1442 	mutex_init(&sctp_epriv_port_lock, NULL, MUTEX_DEFAULT, NULL);
1443 
1444 	/* Initialize SCTP hash arrays. */
1445 	sctp_hash_init();
1446 
1447 	sctp_pad_mp = allocb(SCTP_ALIGN, BPRI_MED);
1448 	bzero(sctp_pad_mp->b_rptr, SCTP_ALIGN);
1449 	ASSERT(sctp_pad_mp);
1450 
1451 	if (!sctp_nd_init()) {
1452 		sctp_nd_free();
1453 	}
1454 
1455 	/* Create sctp_t/conn_t cache */
1456 	sctp_conn_cache_init();
1457 
1458 	/* Create the faddr cache */
1459 	sctp_faddr_init();
1460 
1461 	/* Create the sets cache */
1462 	sctp_sets_init();
1463 
1464 	/* Create the PR-SCTP sets cache */
1465 	sctp_ftsn_sets_init();
1466 
1467 	/* Initialize the recvq taskq. */
1468 	sctp_rq_tq_init();
1469 
1470 	/* saddr init */
1471 	sctp_saddr_init();
1472 
1473 	/* Global SCTP PCB list. */
1474 	list_create(&sctp_g_list, sizeof (sctp_t),
1475 	    offsetof(sctp_t, sctp_list));
1476 
1477 	/* Initialize tables used for CRC calculation */
1478 	sctp_crc32_init();
1479 
1480 	/* Initialize sctp kernel stats. */
1481 	sctp_kstat_init();
1482 }
1483 
1484 void
1485 sctp_ddi_destroy(void)
1486 {
1487 	sctp_nd_free();
1488 
1489 	/* Destroy sctp_t/conn_t caches */
1490 	sctp_conn_cache_fini();
1491 
1492 	/* Destroy the faddr cache */
1493 	sctp_faddr_fini();
1494 
1495 	/* Destroy the sets cache */
1496 	sctp_sets_fini();
1497 
1498 	/* Destroy the PR-SCTP sets cache */
1499 	sctp_ftsn_sets_fini();
1500 
1501 	/* Destroy the recvq taskqs. */
1502 	sctp_rq_tq_fini();
1503 
1504 	/* Destroy saddr  */
1505 	sctp_saddr_fini();
1506 
1507 	/* Global SCTP PCB list. */
1508 	list_destroy(&sctp_g_list);
1509 
1510 	/* Destroy SCTP hash arrays. */
1511 	sctp_hash_destroy();
1512 
1513 	/* Destroy SCTP kenrel stats. */
1514 	sctp_kstat_fini();
1515 
1516 	mutex_destroy(&sctp_g_lock);
1517 	mutex_destroy(&sctp_epriv_port_lock);
1518 }
1519 
1520 void
1521 sctp_display_all()
1522 {
1523 	sctp_t *sctp_walker;
1524 
1525 	mutex_enter(&sctp_g_lock);
1526 	for (sctp_walker = gsctp; sctp_walker != NULL;
1527 		sctp_walker = (sctp_t *)list_next(&sctp_g_list, sctp_walker)) {
1528 		(void) sctp_display(sctp_walker, NULL);
1529 	}
1530 	mutex_exit(&sctp_g_lock);
1531 }
1532 
1533 static void
1534 sctp_rq_tq_init(void)
1535 {
1536 	/*
1537 	 * Initialize the recvq_tq_list and create the first recvq taskq.
1538 	 * What to do if it fails?
1539 	 */
1540 	recvq_tq_list = kmem_zalloc(sctp_recvq_tq_list_max_sz *
1541 	    sizeof (taskq_t *), KM_SLEEP);
1542 	recvq_tq_list[0] = taskq_create("sctp_def_recvq_taskq",
1543 	    MIN(sctp_recvq_tq_thr_max, MAX(sctp_recvq_tq_thr_min, ncpus)),
1544 	    minclsyspri, sctp_recvq_tq_task_min, sctp_recvq_tq_task_max,
1545 	    TASKQ_PREPOPULATE);
1546 	mutex_init(&sctp_rq_tq_lock, NULL, MUTEX_DEFAULT, NULL);
1547 }
1548 
1549 static void
1550 sctp_rq_tq_fini(void)
1551 {
1552 	int i;
1553 
1554 	for (i = 0; i < recvq_tq_list_cur_sz; i++) {
1555 		ASSERT(recvq_tq_list[i] != NULL);
1556 		taskq_destroy(recvq_tq_list[i]);
1557 	}
1558 	kmem_free(recvq_tq_list, sctp_recvq_tq_list_max_sz *
1559 	    sizeof (taskq_t *));
1560 }
1561 
1562 /* Add another taskq for a new ill. */
1563 void
1564 sctp_inc_taskq(void)
1565 {
1566 	taskq_t *tq;
1567 	char tq_name[TASKQ_NAMELEN];
1568 
1569 	mutex_enter(&sctp_rq_tq_lock);
1570 	if (recvq_tq_list_cur_sz + 1 > sctp_recvq_tq_list_max_sz) {
1571 		mutex_exit(&sctp_rq_tq_lock);
1572 		cmn_err(CE_NOTE, "Cannot create more SCTP recvq taskq");
1573 		return;
1574 	}
1575 
1576 	(void) snprintf(tq_name, sizeof (tq_name), "sctp_recvq_taskq_%u",
1577 	    recvq_tq_list_cur_sz);
1578 	tq = taskq_create(tq_name,
1579 	    MIN(sctp_recvq_tq_thr_max, MAX(sctp_recvq_tq_thr_min, ncpus)),
1580 	    minclsyspri, sctp_recvq_tq_task_min, sctp_recvq_tq_task_max,
1581 	    TASKQ_PREPOPULATE);
1582 	if (tq == NULL) {
1583 		mutex_exit(&sctp_rq_tq_lock);
1584 		cmn_err(CE_NOTE, "SCTP recvq taskq creation failed");
1585 		return;
1586 	}
1587 	ASSERT(recvq_tq_list[recvq_tq_list_cur_sz] == NULL);
1588 	recvq_tq_list[recvq_tq_list_cur_sz] = tq;
1589 	atomic_add_32(&recvq_tq_list_cur_sz, 1);
1590 	mutex_exit(&sctp_rq_tq_lock);
1591 }
1592 
1593 #ifdef DEBUG
1594 uint32_t sendq_loop_cnt = 0;
1595 uint32_t sendq_collision = 0;
1596 uint32_t sendq_empty = 0;
1597 #endif
1598 
1599 void
1600 sctp_add_sendq(sctp_t *sctp, mblk_t *mp)
1601 {
1602 	mutex_enter(&sctp->sctp_sendq_lock);
1603 	if (sctp->sctp_sendq == NULL) {
1604 		sctp->sctp_sendq = mp;
1605 		sctp->sctp_sendq_tail = mp;
1606 	} else {
1607 		sctp->sctp_sendq_tail->b_next = mp;
1608 		sctp->sctp_sendq_tail = mp;
1609 	}
1610 	mutex_exit(&sctp->sctp_sendq_lock);
1611 }
1612 
1613 void
1614 sctp_process_sendq(sctp_t *sctp)
1615 {
1616 	mblk_t *mp;
1617 #ifdef DEBUG
1618 	uint32_t loop_cnt = 0;
1619 #endif
1620 
1621 	mutex_enter(&sctp->sctp_sendq_lock);
1622 	if (sctp->sctp_sendq == NULL || sctp->sctp_sendq_sending) {
1623 #ifdef DEBUG
1624 		if (sctp->sctp_sendq == NULL)
1625 			sendq_empty++;
1626 		else
1627 			sendq_collision++;
1628 #endif
1629 		mutex_exit(&sctp->sctp_sendq_lock);
1630 		return;
1631 	}
1632 	sctp->sctp_sendq_sending = B_TRUE;
1633 
1634 	/*
1635 	 * Note that while we are in this loop, other thread can put
1636 	 * new packets in the receive queue.  We may be looping for
1637 	 * quite a while.  This is OK even for an interrupt thread.
1638 	 * The reason is that SCTP should only able to send a limited
1639 	 * number of packets out in a burst.  So the number of times
1640 	 * we go through this loop should not be many.
1641 	 */
1642 	while ((mp = sctp->sctp_sendq) != NULL) {
1643 		sctp->sctp_sendq = mp->b_next;
1644 		ASSERT(sctp->sctp_connp->conn_ref > 0);
1645 		mutex_exit(&sctp->sctp_sendq_lock);
1646 		mp->b_next = NULL;
1647 		CONN_INC_REF(sctp->sctp_connp);
1648 		mp->b_flag |= MSGHASREF;
1649 		/* If we don't have sctp_current, default to IPv4 */
1650 		IP_PUT(mp, sctp->sctp_connp, sctp->sctp_current == NULL ?
1651 		    B_TRUE : sctp->sctp_current->isv4);
1652 		BUMP_LOCAL(sctp->sctp_opkts);
1653 #ifdef DEBUG
1654 		loop_cnt++;
1655 #endif
1656 		mutex_enter(&sctp->sctp_sendq_lock);
1657 	}
1658 
1659 	sctp->sctp_sendq_tail = NULL;
1660 	sctp->sctp_sendq_sending = B_FALSE;
1661 #ifdef DEBUG
1662 	if (loop_cnt > sendq_loop_cnt)
1663 		sendq_loop_cnt = loop_cnt;
1664 #endif
1665 	mutex_exit(&sctp->sctp_sendq_lock);
1666 }
1667 
1668 #ifdef DEBUG
1669 uint32_t recvq_loop_cnt = 0;
1670 uint32_t recvq_call = 0;
1671 #endif
1672 
1673 /*
1674  * Find the next recvq_tq to use.  This routine will go thru all the
1675  * taskqs until it can dispatch a job for the sctp.  If this fails,
1676  * it will create a new taskq and try it.
1677  */
1678 static boolean_t
1679 sctp_find_next_tq(sctp_t *sctp)
1680 {
1681 	int next_tq, try;
1682 	taskq_t *tq;
1683 
1684 	/*
1685 	 * Note that since we don't hold a lock on sctp_rq_tq_lock for
1686 	 * performance reason, recvq_ta_list_cur_sz can be changed during
1687 	 * this loop.  The problem this will create is that the loop may
1688 	 * not have tried all the recvq_tq.  This should be OK.
1689 	 */
1690 	next_tq = atomic_add_32_nv(&recvq_tq_list_cur, 1) %
1691 	    recvq_tq_list_cur_sz;
1692 	for (try = 0; try < recvq_tq_list_cur_sz;
1693 	    try++, next_tq = (next_tq + 1) % recvq_tq_list_cur_sz) {
1694 		tq = recvq_tq_list[next_tq];
1695 		if (taskq_dispatch(tq, sctp_process_recvq, sctp,
1696 		    TQ_NOSLEEP) != NULL) {
1697 			sctp->sctp_recvq_tq = tq;
1698 			return (B_TRUE);
1699 		}
1700 	}
1701 
1702 	/*
1703 	 * Create one more taskq and try it.  Note that sctp_inc_taskq()
1704 	 * may not have created another taskq if the number of recvq
1705 	 * taskqs is at the maximum.  We are probably in a pretty bad
1706 	 * shape if this actually happens...
1707 	 */
1708 	sctp_inc_taskq();
1709 	tq = recvq_tq_list[recvq_tq_list_cur_sz - 1];
1710 	if (taskq_dispatch(tq, sctp_process_recvq, sctp, TQ_NOSLEEP) != NULL) {
1711 		sctp->sctp_recvq_tq = tq;
1712 		return (B_TRUE);
1713 	}
1714 	SCTP_KSTAT(sctp_find_next_tq);
1715 	return (B_FALSE);
1716 }
1717 
1718 /*
1719  * To add a message to the recvq.  Note that the sctp_timer_fire()
1720  * routine also uses this function to add the timer message to the
1721  * receive queue for later processing.  And it should be the only
1722  * caller of sctp_add_recvq() which sets the try_harder argument
1723  * to B_TRUE.
1724  *
1725  * If the try_harder argument is B_TRUE, this routine sctp_find_next_tq()
1726  * will try very hard to dispatch the task.  Refer to the comment
1727  * for that routine on how it does that.
1728  */
1729 boolean_t
1730 sctp_add_recvq(sctp_t *sctp, mblk_t *mp, boolean_t caller_hold_lock)
1731 {
1732 	if (!caller_hold_lock)
1733 		mutex_enter(&sctp->sctp_recvq_lock);
1734 
1735 	/* If the taskq dispatch has not been scheduled, do it now. */
1736 	if (sctp->sctp_recvq_tq == NULL) {
1737 		ASSERT(sctp->sctp_recvq == NULL);
1738 		if (!sctp_find_next_tq(sctp)) {
1739 			if (!caller_hold_lock)
1740 				mutex_exit(&sctp->sctp_recvq_lock);
1741 			return (B_FALSE);
1742 		}
1743 		/* Make sure the sctp_t will not go away. */
1744 		SCTP_REFHOLD(sctp);
1745 	}
1746 
1747 	if (sctp->sctp_recvq == NULL) {
1748 		sctp->sctp_recvq = mp;
1749 		sctp->sctp_recvq_tail = mp;
1750 	} else {
1751 		sctp->sctp_recvq_tail->b_next = mp;
1752 		sctp->sctp_recvq_tail = mp;
1753 	}
1754 
1755 	if (!caller_hold_lock)
1756 		mutex_exit(&sctp->sctp_recvq_lock);
1757 	return (B_TRUE);
1758 }
1759 
1760 static void
1761 sctp_process_recvq(void *arg)
1762 {
1763 	sctp_t		*sctp = (sctp_t *)arg;
1764 	mblk_t		*mp;
1765 	mblk_t		*ipsec_mp;
1766 #ifdef DEBUG
1767 	uint32_t	loop_cnt = 0;
1768 #endif
1769 
1770 #ifdef	_BIG_ENDIAN
1771 #define	IPVER(ip6h)	((((uint32_t *)ip6h)[0] >> 28) & 0x7)
1772 #else
1773 #define	IPVER(ip6h)	((((uint32_t *)ip6h)[0] >> 4) & 0x7)
1774 #endif
1775 
1776 	RUN_SCTP(sctp);
1777 	mutex_enter(&sctp->sctp_recvq_lock);
1778 
1779 #ifdef DEBUG
1780 	recvq_call++;
1781 #endif
1782 	/*
1783 	 * Note that while we are in this loop, other thread can put
1784 	 * new packets in the receive queue.  We may be looping for
1785 	 * quite a while.
1786 	 */
1787 	while ((mp = sctp->sctp_recvq) != NULL) {
1788 		sctp->sctp_recvq = mp->b_next;
1789 		mutex_exit(&sctp->sctp_recvq_lock);
1790 		mp->b_next = NULL;
1791 #ifdef DEBUG
1792 		loop_cnt++;
1793 #endif
1794 		ipsec_mp = mp->b_prev;
1795 		mp->b_prev = NULL;
1796 		sctp_input_data(sctp, mp, ipsec_mp);
1797 
1798 		mutex_enter(&sctp->sctp_recvq_lock);
1799 	}
1800 
1801 	sctp->sctp_recvq_tail = NULL;
1802 	sctp->sctp_recvq_tq = NULL;
1803 
1804 	mutex_exit(&sctp->sctp_recvq_lock);
1805 
1806 	WAKE_SCTP(sctp);
1807 
1808 	/* We may have sent something when processing the receive queue. */
1809 	sctp_process_sendq(sctp);
1810 #ifdef DEBUG
1811 	if (loop_cnt > recvq_loop_cnt)
1812 		recvq_loop_cnt = loop_cnt;
1813 #endif
1814 	/* Now it can go away. */
1815 	SCTP_REFRELE(sctp);
1816 }
1817 
1818 /* ARGSUSED */
1819 static int
1820 sctp_conn_cache_constructor(void *buf, void *cdrarg, int kmflags)
1821 {
1822 	conn_t	*sctp_connp = (conn_t *)buf;
1823 	sctp_t	*sctp = (sctp_t *)&sctp_connp[1];
1824 
1825 	bzero(buf, (char *)&sctp[1] - (char *)buf);
1826 
1827 	ASSERT(sctp_g_q != NULL);
1828 	sctp->sctp_connp = sctp_connp;
1829 	mutex_init(&sctp->sctp_reflock, NULL, MUTEX_DEFAULT, NULL);
1830 	mutex_init(&sctp->sctp_lock, NULL, MUTEX_DEFAULT, NULL);
1831 	mutex_init(&sctp->sctp_recvq_lock, NULL, MUTEX_DEFAULT, NULL);
1832 	cv_init(&sctp->sctp_cv, NULL, CV_DEFAULT, NULL);
1833 	mutex_init(&sctp->sctp_sendq_lock, NULL, MUTEX_DEFAULT, NULL);
1834 
1835 	sctp_connp->conn_rq = sctp_connp->conn_wq = NULL;
1836 	sctp_connp->conn_multicast_loop = IP_DEFAULT_MULTICAST_LOOP;
1837 	sctp_connp->conn_ulp = IPPROTO_SCTP;
1838 	mutex_init(&sctp_connp->conn_lock, NULL, MUTEX_DEFAULT, NULL);
1839 	cv_init(&sctp_connp->conn_cv, NULL, CV_DEFAULT, NULL);
1840 
1841 	return (0);
1842 }
1843 
1844 /* ARGSUSED */
1845 static void
1846 sctp_conn_cache_destructor(void *buf, void *cdrarg)
1847 {
1848 	conn_t	*sctp_connp = (conn_t *)buf;
1849 	sctp_t	*sctp = (sctp_t *)&sctp_connp[1];
1850 
1851 	ASSERT(!MUTEX_HELD(&sctp->sctp_lock));
1852 	ASSERT(!MUTEX_HELD(&sctp->sctp_reflock));
1853 	ASSERT(!MUTEX_HELD(&sctp->sctp_recvq_lock));
1854 	ASSERT(!MUTEX_HELD(&sctp->sctp_sendq_lock));
1855 	ASSERT(!MUTEX_HELD(&sctp->sctp_connp->conn_lock));
1856 
1857 	ASSERT(sctp->sctp_conn_hash_next == NULL);
1858 	ASSERT(sctp->sctp_conn_hash_prev == NULL);
1859 	ASSERT(sctp->sctp_listen_hash_next == NULL);
1860 	ASSERT(sctp->sctp_listen_hash_prev == NULL);
1861 	ASSERT(sctp->sctp_listen_tfp == NULL);
1862 	ASSERT(sctp->sctp_conn_tfp == NULL);
1863 
1864 	ASSERT(sctp->sctp_faddrs == NULL);
1865 	ASSERT(sctp->sctp_nsaddrs == 0);
1866 
1867 	ASSERT(sctp->sctp_ulpd == NULL);
1868 
1869 	ASSERT(sctp->sctp_lastfaddr == NULL);
1870 	ASSERT(sctp->sctp_primary == NULL);
1871 	ASSERT(sctp->sctp_current == NULL);
1872 	ASSERT(sctp->sctp_lastdata == NULL);
1873 
1874 	ASSERT(sctp->sctp_xmit_head == NULL);
1875 	ASSERT(sctp->sctp_xmit_tail == NULL);
1876 	ASSERT(sctp->sctp_xmit_unsent == NULL);
1877 	ASSERT(sctp->sctp_xmit_unsent_tail == NULL);
1878 
1879 	ASSERT(sctp->sctp_ostrcntrs == NULL);
1880 
1881 	ASSERT(sctp->sctp_sack_info == NULL);
1882 	ASSERT(sctp->sctp_ack_mp == NULL);
1883 	ASSERT(sctp->sctp_instr == NULL);
1884 
1885 	ASSERT(sctp->sctp_iphc == NULL);
1886 	ASSERT(sctp->sctp_iphc6 == NULL);
1887 	ASSERT(sctp->sctp_ipha == NULL);
1888 	ASSERT(sctp->sctp_ip6h == NULL);
1889 	ASSERT(sctp->sctp_sctph == NULL);
1890 	ASSERT(sctp->sctp_sctph6 == NULL);
1891 
1892 	ASSERT(sctp->sctp_cookie_mp == NULL);
1893 
1894 	ASSERT(sctp->sctp_refcnt == 0);
1895 	ASSERT(sctp->sctp_timer_mp == NULL);
1896 	ASSERT(sctp->sctp_connp->conn_ref == 0);
1897 	ASSERT(sctp->sctp_heartbeat_mp == NULL);
1898 	ASSERT(sctp->sctp_ptpbhn == NULL && sctp->sctp_bind_hash == NULL);
1899 
1900 	ASSERT(sctp->sctp_shutdown_faddr == NULL);
1901 
1902 	ASSERT(sctp->sctp_cxmit_list == NULL);
1903 
1904 	ASSERT(sctp->sctp_recvq == NULL);
1905 	ASSERT(sctp->sctp_recvq_tail == NULL);
1906 	ASSERT(sctp->sctp_recvq_tq == NULL);
1907 
1908 	ASSERT(sctp->sctp_sendq == NULL);
1909 	ASSERT(sctp->sctp_sendq_tail == NULL);
1910 	ASSERT(sctp->sctp_sendq_sending == B_FALSE);
1911 
1912 	ASSERT(sctp->sctp_ipp_hopopts == NULL);
1913 	ASSERT(sctp->sctp_ipp_rtdstopts == NULL);
1914 	ASSERT(sctp->sctp_ipp_rthdr == NULL);
1915 	ASSERT(sctp->sctp_ipp_dstopts == NULL);
1916 	ASSERT(sctp->sctp_ipp_pathmtu == NULL);
1917 
1918 	mutex_destroy(&sctp->sctp_reflock);
1919 	mutex_destroy(&sctp->sctp_lock);
1920 	mutex_destroy(&sctp->sctp_recvq_lock);
1921 	cv_destroy(&sctp->sctp_cv);
1922 	mutex_destroy(&sctp->sctp_sendq_lock);
1923 
1924 	mutex_destroy(&sctp_connp->conn_lock);
1925 	cv_destroy(&sctp_connp->conn_cv);
1926 }
1927 
1928 static void
1929 sctp_conn_cache_init()
1930 {
1931 	sctp_conn_cache = kmem_cache_create("sctp_conn_cache",
1932 	    sizeof (sctp_t) + sizeof (conn_t), 0, sctp_conn_cache_constructor,
1933 	    sctp_conn_cache_destructor, NULL, NULL, NULL, 0);
1934 }
1935 
1936 static void
1937 sctp_conn_cache_fini()
1938 {
1939 	kmem_cache_destroy(sctp_conn_cache);
1940 }
1941