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