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