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