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