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