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