17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*45916cd2Sjpk  * Common Development and Distribution License (the "License").
6*45916cd2Sjpk  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*45916cd2Sjpk  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #include <sys/types.h>
297c478bd9Sstevel@tonic-gate #include <sys/systm.h>
307c478bd9Sstevel@tonic-gate #include <sys/stream.h>
317c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
327c478bd9Sstevel@tonic-gate #include <sys/strsubr.h>
337c478bd9Sstevel@tonic-gate #include <sys/strsun.h>
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate #include <netinet/in.h>
367c478bd9Sstevel@tonic-gate #include <netinet/ip6.h>
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate #include <inet/common.h>
397c478bd9Sstevel@tonic-gate #include <inet/ip.h>
407c478bd9Sstevel@tonic-gate #include <inet/mib2.h>
417c478bd9Sstevel@tonic-gate #include <inet/ipclassifier.h>
427c478bd9Sstevel@tonic-gate #include "sctp_impl.h"
437c478bd9Sstevel@tonic-gate #include "sctp_asconf.h"
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate /* Timer block states. */
467c478bd9Sstevel@tonic-gate typedef enum {
477c478bd9Sstevel@tonic-gate 	SCTP_TB_RUNNING = 1,
487c478bd9Sstevel@tonic-gate 	SCTP_TB_IDLE,
497c478bd9Sstevel@tonic-gate /* Could not stop/free before mblk got queued */
507c478bd9Sstevel@tonic-gate 	SCTP_TB_RESCHED,	/* sctp_tb_time_left contains tick count */
517c478bd9Sstevel@tonic-gate 	SCTP_TB_CANCELLED,
527c478bd9Sstevel@tonic-gate 	SCTP_TB_TO_BE_FREED
537c478bd9Sstevel@tonic-gate } timer_block_state;
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate typedef struct sctp_tb_s {
567c478bd9Sstevel@tonic-gate 	timer_block_state	sctp_tb_state;
577c478bd9Sstevel@tonic-gate 	timeout_id_t		sctp_tb_tid;
587c478bd9Sstevel@tonic-gate 	mblk_t			*sctp_tb_mp;
597c478bd9Sstevel@tonic-gate 	clock_t			sctp_tb_time_left;
607c478bd9Sstevel@tonic-gate } sctp_tb_t;
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate static void sctp_timer_fire(sctp_tb_t *);
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate /*
657c478bd9Sstevel@tonic-gate  *		sctp_timer mechanism.
667c478bd9Sstevel@tonic-gate  *
677c478bd9Sstevel@tonic-gate  * Each timer is represented by a timer mblk. When the
687c478bd9Sstevel@tonic-gate  * timer fires, and the sctp_t is busy, the timer mblk will be put on
697c478bd9Sstevel@tonic-gate  * the associated sctp_t timer queue so that it can be executed when
707c478bd9Sstevel@tonic-gate  * the thread holding the lock on the sctp_t is done with its job.
717c478bd9Sstevel@tonic-gate  *
727c478bd9Sstevel@tonic-gate  * Note that there is no lock to protect the timer mblk state.  The reason
737c478bd9Sstevel@tonic-gate  * is that the timer state can only be changed by a thread holding the
747c478bd9Sstevel@tonic-gate  * lock on the sctp_t.
757c478bd9Sstevel@tonic-gate  *
767c478bd9Sstevel@tonic-gate  * The interface consists of 4 entry points:
777c478bd9Sstevel@tonic-gate  *	sctp_timer_alloc	- create a timer mblk
787c478bd9Sstevel@tonic-gate  *	sctp_timer_free		- free a timer mblk
797c478bd9Sstevel@tonic-gate  *	sctp_timer		- start, restart, stop the timer
807c478bd9Sstevel@tonic-gate  *	sctp_timer_valid	- called by sctp_process_recvq to verify that
817c478bd9Sstevel@tonic-gate  *				  the timer did indeed fire.
827c478bd9Sstevel@tonic-gate  */
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate /*
867c478bd9Sstevel@tonic-gate  * Start, restart, stop the timer.
877c478bd9Sstevel@tonic-gate  * If "tim" is -1 the timer is stopped.
887c478bd9Sstevel@tonic-gate  * Otherwise, the timer is stopped if it is already running, and
897c478bd9Sstevel@tonic-gate  * set to fire tim clock ticks from now.
907c478bd9Sstevel@tonic-gate  */
917c478bd9Sstevel@tonic-gate void
927c478bd9Sstevel@tonic-gate sctp_timer(sctp_t *sctp, mblk_t *mp, clock_t tim)
937c478bd9Sstevel@tonic-gate {
947c478bd9Sstevel@tonic-gate 	sctp_tb_t *sctp_tb;
957c478bd9Sstevel@tonic-gate 	int state;
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate 	ASSERT(sctp != NULL && mp != NULL);
987c478bd9Sstevel@tonic-gate 	ASSERT((mp->b_rptr - mp->b_datap->db_base) == sizeof (sctp_tb_t));
997c478bd9Sstevel@tonic-gate 	ASSERT(mp->b_datap->db_type == M_PCSIG);
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate 	sctp_tb = (sctp_tb_t *)mp->b_datap->db_base;
1027c478bd9Sstevel@tonic-gate 	if (tim >= 0) {
1037c478bd9Sstevel@tonic-gate 		state = sctp_tb->sctp_tb_state;
1047c478bd9Sstevel@tonic-gate 		sctp_tb->sctp_tb_time_left = tim;
1057c478bd9Sstevel@tonic-gate 		if (state == SCTP_TB_RUNNING) {
1067c478bd9Sstevel@tonic-gate 			if (untimeout(sctp_tb->sctp_tb_tid) < 0) {
1077c478bd9Sstevel@tonic-gate 				sctp_tb->sctp_tb_state = SCTP_TB_RESCHED;
1087c478bd9Sstevel@tonic-gate 				/* sctp_timer_valid will start timer */
1097c478bd9Sstevel@tonic-gate 				return;
1107c478bd9Sstevel@tonic-gate 			}
1117c478bd9Sstevel@tonic-gate 		} else if (state != SCTP_TB_IDLE) {
1127c478bd9Sstevel@tonic-gate 			ASSERT(state != SCTP_TB_TO_BE_FREED);
1137c478bd9Sstevel@tonic-gate 			if (state == SCTP_TB_CANCELLED) {
1147c478bd9Sstevel@tonic-gate 				sctp_tb->sctp_tb_state = SCTP_TB_RESCHED;
1157c478bd9Sstevel@tonic-gate 				/* sctp_timer_valid will start timer */
1167c478bd9Sstevel@tonic-gate 				return;
1177c478bd9Sstevel@tonic-gate 			}
1187c478bd9Sstevel@tonic-gate 			if (state == SCTP_TB_RESCHED) {
1197c478bd9Sstevel@tonic-gate 				/* sctp_timer_valid will start timer */
1207c478bd9Sstevel@tonic-gate 				return;
1217c478bd9Sstevel@tonic-gate 			}
1227c478bd9Sstevel@tonic-gate 		} else {
1237c478bd9Sstevel@tonic-gate 			SCTP_REFHOLD(sctp);
1247c478bd9Sstevel@tonic-gate 		}
1257c478bd9Sstevel@tonic-gate 		sctp_tb->sctp_tb_state = SCTP_TB_RUNNING;
1267c478bd9Sstevel@tonic-gate 		sctp_tb->sctp_tb_tid =
1277c478bd9Sstevel@tonic-gate 		    timeout((pfv_t)sctp_timer_fire, sctp_tb, tim);
1287c478bd9Sstevel@tonic-gate 		return;
1297c478bd9Sstevel@tonic-gate 	}
1307c478bd9Sstevel@tonic-gate 	switch (tim) {
1317c478bd9Sstevel@tonic-gate 	case -1:
1327c478bd9Sstevel@tonic-gate 		sctp_timer_stop(mp);
1337c478bd9Sstevel@tonic-gate 		break;
1347c478bd9Sstevel@tonic-gate 	default:
1357c478bd9Sstevel@tonic-gate 		ASSERT(0);
1367c478bd9Sstevel@tonic-gate 		break;
1377c478bd9Sstevel@tonic-gate 	}
1387c478bd9Sstevel@tonic-gate }
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate /*
1417c478bd9Sstevel@tonic-gate  * sctp_timer_alloc is called by sctp_init to allocate and initialize a
1427c478bd9Sstevel@tonic-gate  * sctp timer.
1437c478bd9Sstevel@tonic-gate  *
1447c478bd9Sstevel@tonic-gate  * Allocate an M_PCSIG timer message. The space between db_base and
1457c478bd9Sstevel@tonic-gate  * b_rptr is used by the sctp_timer mechanism, and after b_rptr there is
1467c478bd9Sstevel@tonic-gate  * space for sctpt_t.
1477c478bd9Sstevel@tonic-gate  */
1487c478bd9Sstevel@tonic-gate mblk_t *
1497c478bd9Sstevel@tonic-gate sctp_timer_alloc(sctp_t *sctp, pfv_t func)
1507c478bd9Sstevel@tonic-gate {
1517c478bd9Sstevel@tonic-gate 	mblk_t *mp;
1527c478bd9Sstevel@tonic-gate 	sctp_tb_t *sctp_tb;
1537c478bd9Sstevel@tonic-gate 	sctpt_t	*sctpt;
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate 	if ((mp = allocb(sizeof (sctp_t) + sizeof (sctp_tb_t), BPRI_HI))) {
1567c478bd9Sstevel@tonic-gate 		mp->b_datap->db_type = M_PCSIG;
1577c478bd9Sstevel@tonic-gate 		sctp_tb = (sctp_tb_t *)mp->b_datap->db_base;
1587c478bd9Sstevel@tonic-gate 		mp->b_rptr = (uchar_t *)&sctp_tb[1];
1597c478bd9Sstevel@tonic-gate 		mp->b_wptr = mp->b_rptr + sizeof (sctpt_t);
1607c478bd9Sstevel@tonic-gate 		sctp_tb->sctp_tb_state = SCTP_TB_IDLE;
1617c478bd9Sstevel@tonic-gate 		sctp_tb->sctp_tb_mp = mp;
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate 		sctpt = (sctpt_t *)mp->b_rptr;
1647c478bd9Sstevel@tonic-gate 		sctpt->sctpt_sctp = sctp;
1657c478bd9Sstevel@tonic-gate 		sctpt->sctpt_faddr = NULL;	/* set when starting timer */
1667c478bd9Sstevel@tonic-gate 		sctpt->sctpt_pfv = func;
1677c478bd9Sstevel@tonic-gate 		return (mp);
1687c478bd9Sstevel@tonic-gate 	}
1697c478bd9Sstevel@tonic-gate 	return (NULL);
1707c478bd9Sstevel@tonic-gate }
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate /*
1737c478bd9Sstevel@tonic-gate  * timeout() callback function.
1747c478bd9Sstevel@tonic-gate  * Put the message on the process control block's queue.
1757c478bd9Sstevel@tonic-gate  * If the timer is stopped or freed after
1767c478bd9Sstevel@tonic-gate  * it has fired then sctp_timer() and sctp_timer_valid() will clean
1777c478bd9Sstevel@tonic-gate  * things up.
1787c478bd9Sstevel@tonic-gate  */
1797c478bd9Sstevel@tonic-gate static void
1807c478bd9Sstevel@tonic-gate sctp_timer_fire(sctp_tb_t *sctp_tb)
1817c478bd9Sstevel@tonic-gate {
1827c478bd9Sstevel@tonic-gate 	mblk_t *mp;
1837c478bd9Sstevel@tonic-gate 	sctp_t *sctp;
1847c478bd9Sstevel@tonic-gate 	sctpt_t *sctpt;
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate 	mp = sctp_tb->sctp_tb_mp;
1877c478bd9Sstevel@tonic-gate 	ASSERT(sctp_tb == (sctp_tb_t *)mp->b_datap->db_base);
1887c478bd9Sstevel@tonic-gate 	ASSERT(mp->b_datap->db_type == M_PCSIG);
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate 	sctpt = (sctpt_t *)mp->b_rptr;
1917c478bd9Sstevel@tonic-gate 	sctp = sctpt->sctpt_sctp;
1927c478bd9Sstevel@tonic-gate 	ASSERT(sctp != NULL);
1937c478bd9Sstevel@tonic-gate 
1947c478bd9Sstevel@tonic-gate 	mutex_enter(&sctp->sctp_lock);
1957c478bd9Sstevel@tonic-gate 	if (sctp->sctp_running) {
1967c478bd9Sstevel@tonic-gate 		/*
1977c478bd9Sstevel@tonic-gate 		 * Put the timer mblk to the special sctp_timer_mp list.
1987c478bd9Sstevel@tonic-gate 		 * This timer will be handled when the thread using this
1997c478bd9Sstevel@tonic-gate 		 * SCTP is done with its job.
2007c478bd9Sstevel@tonic-gate 		 */
2017c478bd9Sstevel@tonic-gate 		if (sctp->sctp_timer_mp == NULL) {
2027c478bd9Sstevel@tonic-gate 			SCTP_REFHOLD(sctp);
2037c478bd9Sstevel@tonic-gate 			sctp->sctp_timer_mp = mp;
2047c478bd9Sstevel@tonic-gate 		} else {
2057c478bd9Sstevel@tonic-gate 			linkb(sctp->sctp_timer_mp, mp);
2067c478bd9Sstevel@tonic-gate 		}
2077c478bd9Sstevel@tonic-gate 		mp->b_cont = NULL;
2087c478bd9Sstevel@tonic-gate 		mutex_exit(&sctp->sctp_lock);
2097c478bd9Sstevel@tonic-gate 	} else {
2107c478bd9Sstevel@tonic-gate 		sctp->sctp_running = B_TRUE;
2117c478bd9Sstevel@tonic-gate 		mutex_exit(&sctp->sctp_lock);
2127c478bd9Sstevel@tonic-gate 
2137c478bd9Sstevel@tonic-gate 		sctp_timer_call(sctp, mp);
2147c478bd9Sstevel@tonic-gate 		WAKE_SCTP(sctp);
2157c478bd9Sstevel@tonic-gate 		sctp_process_sendq(sctp);
2167c478bd9Sstevel@tonic-gate 	}
2177c478bd9Sstevel@tonic-gate 	SCTP_REFRELE(sctp);
2187c478bd9Sstevel@tonic-gate }
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate /*
2217c478bd9Sstevel@tonic-gate  * Logically free a timer mblk (that might have a pending timeout().)
2227c478bd9Sstevel@tonic-gate  * If the timer has fired and the mblk has been put on the queue then
2237c478bd9Sstevel@tonic-gate  * sctp_timer_valid will free the mblk.
2247c478bd9Sstevel@tonic-gate  */
2257c478bd9Sstevel@tonic-gate void
2267c478bd9Sstevel@tonic-gate sctp_timer_free(mblk_t *mp)
2277c478bd9Sstevel@tonic-gate {
2287c478bd9Sstevel@tonic-gate 	sctp_tb_t *sctp_tb;
2297c478bd9Sstevel@tonic-gate 	int state;
2307c478bd9Sstevel@tonic-gate 	sctpt_t *sctpt;
2317c478bd9Sstevel@tonic-gate 
2327c478bd9Sstevel@tonic-gate 	ASSERT(mp != NULL);
2337c478bd9Sstevel@tonic-gate 	ASSERT((mp->b_rptr - mp->b_datap->db_base) == sizeof (sctp_tb_t));
2347c478bd9Sstevel@tonic-gate 	ASSERT(mp->b_datap->db_type == M_PCSIG);
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate 	sctp_tb = (sctp_tb_t *)mp->b_datap->db_base;
2377c478bd9Sstevel@tonic-gate 	state = sctp_tb->sctp_tb_state;
2387c478bd9Sstevel@tonic-gate 
239*45916cd2Sjpk 	dprint(5, ("sctp_timer_free %p state %d\n", (void *)mp, state));
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate 	if (state == SCTP_TB_RUNNING) {
2427c478bd9Sstevel@tonic-gate 		if (untimeout(sctp_tb->sctp_tb_tid) < 0) {
2437c478bd9Sstevel@tonic-gate 			sctp_tb->sctp_tb_state = SCTP_TB_TO_BE_FREED;
2447c478bd9Sstevel@tonic-gate 			/* sctp_timer_valid will free the mblk */
2457c478bd9Sstevel@tonic-gate 			return;
2467c478bd9Sstevel@tonic-gate 		}
2477c478bd9Sstevel@tonic-gate 		sctpt = (sctpt_t *)mp->b_rptr;
2487c478bd9Sstevel@tonic-gate 		SCTP_REFRELE(sctpt->sctpt_sctp);
2497c478bd9Sstevel@tonic-gate 	} else if (state != SCTP_TB_IDLE) {
2507c478bd9Sstevel@tonic-gate 		ASSERT(state != SCTP_TB_TO_BE_FREED);
2517c478bd9Sstevel@tonic-gate 		sctp_tb->sctp_tb_state = SCTP_TB_TO_BE_FREED;
2527c478bd9Sstevel@tonic-gate 		/* sctp_timer_valid will free the mblk */
2537c478bd9Sstevel@tonic-gate 		return;
2547c478bd9Sstevel@tonic-gate 	}
2557c478bd9Sstevel@tonic-gate 	freeb(mp);
2567c478bd9Sstevel@tonic-gate }
2577c478bd9Sstevel@tonic-gate 
2587c478bd9Sstevel@tonic-gate /*
2597c478bd9Sstevel@tonic-gate  * Called from sctp_timer(,,-1)
2607c478bd9Sstevel@tonic-gate  */
2617c478bd9Sstevel@tonic-gate void
2627c478bd9Sstevel@tonic-gate sctp_timer_stop(mblk_t *mp)
2637c478bd9Sstevel@tonic-gate {
2647c478bd9Sstevel@tonic-gate 	sctp_tb_t *sctp_tb;
2657c478bd9Sstevel@tonic-gate 	int state;
2667c478bd9Sstevel@tonic-gate 	sctpt_t *sctpt;
2677c478bd9Sstevel@tonic-gate 
2687c478bd9Sstevel@tonic-gate 	ASSERT(mp != NULL);
2697c478bd9Sstevel@tonic-gate 	ASSERT(mp->b_datap->db_type == M_PCSIG);
2707c478bd9Sstevel@tonic-gate 
2717c478bd9Sstevel@tonic-gate 	sctp_tb = (sctp_tb_t *)mp->b_datap->db_base;
2727c478bd9Sstevel@tonic-gate 	state = sctp_tb->sctp_tb_state;
2737c478bd9Sstevel@tonic-gate 
274*45916cd2Sjpk 	dprint(5, ("sctp_timer_stop %p %d\n", (void *)mp, state));
2757c478bd9Sstevel@tonic-gate 
2767c478bd9Sstevel@tonic-gate 	if (state == SCTP_TB_RUNNING) {
2777c478bd9Sstevel@tonic-gate 		if (untimeout(sctp_tb->sctp_tb_tid) < 0) {
2787c478bd9Sstevel@tonic-gate 			sctp_tb->sctp_tb_state = SCTP_TB_CANCELLED;
2797c478bd9Sstevel@tonic-gate 		} else {
2807c478bd9Sstevel@tonic-gate 			sctp_tb->sctp_tb_state = SCTP_TB_IDLE;
2817c478bd9Sstevel@tonic-gate 			sctpt = (sctpt_t *)mp->b_rptr;
2827c478bd9Sstevel@tonic-gate 			SCTP_REFRELE(sctpt->sctpt_sctp);
2837c478bd9Sstevel@tonic-gate 		}
2847c478bd9Sstevel@tonic-gate 	} else if (state == SCTP_TB_RESCHED) {
2857c478bd9Sstevel@tonic-gate 		sctp_tb->sctp_tb_state = SCTP_TB_CANCELLED;
2867c478bd9Sstevel@tonic-gate 	}
2877c478bd9Sstevel@tonic-gate }
2887c478bd9Sstevel@tonic-gate 
2897c478bd9Sstevel@tonic-gate /*
2907c478bd9Sstevel@tonic-gate  * The user of the sctp_timer mechanism is required to call
2917c478bd9Sstevel@tonic-gate  * sctp_timer_valid() for each M_PCSIG message processed in the
2927c478bd9Sstevel@tonic-gate  * service procedures.
2937c478bd9Sstevel@tonic-gate  * sctp_timer_valid will return "true" if the timer actually did fire.
2947c478bd9Sstevel@tonic-gate  */
2957c478bd9Sstevel@tonic-gate 
2967c478bd9Sstevel@tonic-gate static boolean_t
2977c478bd9Sstevel@tonic-gate sctp_timer_valid(mblk_t *mp)
2987c478bd9Sstevel@tonic-gate {
2997c478bd9Sstevel@tonic-gate 	sctp_tb_t *sctp_tb;
3007c478bd9Sstevel@tonic-gate 	int state;
3017c478bd9Sstevel@tonic-gate 	sctpt_t *sctpt;
3027c478bd9Sstevel@tonic-gate 
3037c478bd9Sstevel@tonic-gate 	ASSERT(mp != NULL);
3047c478bd9Sstevel@tonic-gate 	ASSERT(mp->b_datap->db_type == M_PCSIG);
3057c478bd9Sstevel@tonic-gate 
3067c478bd9Sstevel@tonic-gate 	sctp_tb = (sctp_tb_t *)DB_BASE(mp);
3077c478bd9Sstevel@tonic-gate 	sctpt = (sctpt_t *)mp->b_rptr;
3087c478bd9Sstevel@tonic-gate 	state = sctp_tb->sctp_tb_state;
3097c478bd9Sstevel@tonic-gate 	if (state != SCTP_TB_RUNNING) {
3107c478bd9Sstevel@tonic-gate 		ASSERT(state != SCTP_TB_IDLE);
3117c478bd9Sstevel@tonic-gate 		if (state == SCTP_TB_TO_BE_FREED) {
3127c478bd9Sstevel@tonic-gate 			/*
3137c478bd9Sstevel@tonic-gate 			 * sctp_timer_free was called after the message
3147c478bd9Sstevel@tonic-gate 			 * was putq'ed.
3157c478bd9Sstevel@tonic-gate 			 */
3167c478bd9Sstevel@tonic-gate 			freeb(mp);
3177c478bd9Sstevel@tonic-gate 			return (B_FALSE);
3187c478bd9Sstevel@tonic-gate 		}
3197c478bd9Sstevel@tonic-gate 		if (state == SCTP_TB_CANCELLED) {
3207c478bd9Sstevel@tonic-gate 			/* The timer was stopped after the mblk was putq'ed */
3217c478bd9Sstevel@tonic-gate 			sctp_tb->sctp_tb_state = SCTP_TB_IDLE;
3227c478bd9Sstevel@tonic-gate 			return (B_FALSE);
3237c478bd9Sstevel@tonic-gate 		}
3247c478bd9Sstevel@tonic-gate 		if (state == SCTP_TB_RESCHED) {
3257c478bd9Sstevel@tonic-gate 			/*
3267c478bd9Sstevel@tonic-gate 			 * The timer was stopped and then restarted after
3277c478bd9Sstevel@tonic-gate 			 * the mblk was putq'ed.
3287c478bd9Sstevel@tonic-gate 			 * sctp_tb_time_left contains the number of ticks that
3297c478bd9Sstevel@tonic-gate 			 * the timer was restarted with.
3307c478bd9Sstevel@tonic-gate 			 * The sctp will not be disapper between the time
3317c478bd9Sstevel@tonic-gate 			 * the sctpt_t is marked SCTP_TB_RESCHED and when
3327c478bd9Sstevel@tonic-gate 			 * we get here as sctp_add_recvq() does a refhold.
3337c478bd9Sstevel@tonic-gate 			 */
3347c478bd9Sstevel@tonic-gate 			sctp_tb->sctp_tb_state = SCTP_TB_RUNNING;
3357c478bd9Sstevel@tonic-gate 			sctp_tb->sctp_tb_tid = timeout((pfv_t)sctp_timer_fire,
3367c478bd9Sstevel@tonic-gate 			    sctp_tb, sctp_tb->sctp_tb_time_left);
3377c478bd9Sstevel@tonic-gate 			SCTP_REFHOLD(sctpt->sctpt_sctp);
3387c478bd9Sstevel@tonic-gate 			return (B_FALSE);
3397c478bd9Sstevel@tonic-gate 		}
3407c478bd9Sstevel@tonic-gate 	}
3417c478bd9Sstevel@tonic-gate 	sctp_tb->sctp_tb_state = SCTP_TB_IDLE;
3427c478bd9Sstevel@tonic-gate 	return (B_TRUE);
3437c478bd9Sstevel@tonic-gate }
3447c478bd9Sstevel@tonic-gate 
3457c478bd9Sstevel@tonic-gate /*
3467c478bd9Sstevel@tonic-gate  * The SCTP timer call. Calls sctp_timer_valid() to verify whether
3477c478bd9Sstevel@tonic-gate  * timer was cancelled or not.
3487c478bd9Sstevel@tonic-gate  */
3497c478bd9Sstevel@tonic-gate void
3507c478bd9Sstevel@tonic-gate sctp_timer_call(sctp_t *sctp, mblk_t *mp)
3517c478bd9Sstevel@tonic-gate {
3527c478bd9Sstevel@tonic-gate 	sctpt_t *sctpt = (sctpt_t *)mp->b_rptr;
3537c478bd9Sstevel@tonic-gate 
3547c478bd9Sstevel@tonic-gate 	if (sctp_timer_valid(mp)) {
3557c478bd9Sstevel@tonic-gate 		(*sctpt->sctpt_pfv)(sctp, sctpt->sctpt_faddr);
3567c478bd9Sstevel@tonic-gate 	}
3577c478bd9Sstevel@tonic-gate }
3587c478bd9Sstevel@tonic-gate 
3597c478bd9Sstevel@tonic-gate /*
3607c478bd9Sstevel@tonic-gate  * Delayed ack
3617c478bd9Sstevel@tonic-gate  */
3627c478bd9Sstevel@tonic-gate void
3637c478bd9Sstevel@tonic-gate sctp_ack_timer(sctp_t *sctp)
3647c478bd9Sstevel@tonic-gate {
3657c478bd9Sstevel@tonic-gate 	sctp->sctp_ack_timer_running = 0;
3667c478bd9Sstevel@tonic-gate 	sctp->sctp_sack_toggle = 2;
3677c478bd9Sstevel@tonic-gate 	BUMP_MIB(&sctp_mib, sctpOutAckDelayed);
3687c478bd9Sstevel@tonic-gate 	sctp_sack(sctp, NULL);
3697c478bd9Sstevel@tonic-gate }
3707c478bd9Sstevel@tonic-gate 
3717c478bd9Sstevel@tonic-gate /*
3727c478bd9Sstevel@tonic-gate  * Peer address heartbeat timer handler
3737c478bd9Sstevel@tonic-gate  */
3747c478bd9Sstevel@tonic-gate void
3757c478bd9Sstevel@tonic-gate sctp_heartbeat_timer(sctp_t *sctp)
3767c478bd9Sstevel@tonic-gate {
3777c478bd9Sstevel@tonic-gate 	sctp_faddr_t	*fp;
3787c478bd9Sstevel@tonic-gate 	int64_t		now;
3797c478bd9Sstevel@tonic-gate 	int64_t		earliest_expiry;
3807c478bd9Sstevel@tonic-gate 	int		cnt;
3817c478bd9Sstevel@tonic-gate 
3827c478bd9Sstevel@tonic-gate 	if (sctp->sctp_strikes >= sctp->sctp_pa_max_rxt) {
3837c478bd9Sstevel@tonic-gate 		/*
3847c478bd9Sstevel@tonic-gate 		 * If there is a peer address with no strikes,
3857c478bd9Sstevel@tonic-gate 		 * don't give up yet. If enough other peer
3867c478bd9Sstevel@tonic-gate 		 * address are down, we could otherwise fail
3877c478bd9Sstevel@tonic-gate 		 * the association prematurely.  This is a
3887c478bd9Sstevel@tonic-gate 		 * byproduct of our aggressive probe approach
3897c478bd9Sstevel@tonic-gate 		 * when a heartbeat fails to connect. We may
3907c478bd9Sstevel@tonic-gate 		 * wish to revisit this...
3917c478bd9Sstevel@tonic-gate 		 */
3927c478bd9Sstevel@tonic-gate 		if (!sctp_is_a_faddr_clean(sctp)) {
3937c478bd9Sstevel@tonic-gate 			/* time to give up */
3947c478bd9Sstevel@tonic-gate 			BUMP_MIB(&sctp_mib, sctpAborted);
3957c478bd9Sstevel@tonic-gate 			BUMP_MIB(&sctp_mib, sctpTimHeartBeatDrop);
3967c478bd9Sstevel@tonic-gate 			sctp_assoc_event(sctp, SCTP_COMM_LOST, 0, NULL);
3977c478bd9Sstevel@tonic-gate 			sctp_clean_death(sctp, sctp->sctp_client_errno ?
3987c478bd9Sstevel@tonic-gate 			    sctp->sctp_client_errno : ETIMEDOUT);
3997c478bd9Sstevel@tonic-gate 			return;
4007c478bd9Sstevel@tonic-gate 		}
4017c478bd9Sstevel@tonic-gate 	}
4027c478bd9Sstevel@tonic-gate 
4037c478bd9Sstevel@tonic-gate 	/* Only send heartbeats in the established state */
4047c478bd9Sstevel@tonic-gate 	if (sctp->sctp_state != SCTPS_ESTABLISHED) {
4057c478bd9Sstevel@tonic-gate 		dprint(5, ("sctp_heartbeat_timer: not in ESTABLISHED\n"));
4067c478bd9Sstevel@tonic-gate 		return;
4077c478bd9Sstevel@tonic-gate 	}
4087c478bd9Sstevel@tonic-gate 
4097c478bd9Sstevel@tonic-gate 	now = lbolt64;
4107c478bd9Sstevel@tonic-gate 	earliest_expiry = 0;
4117c478bd9Sstevel@tonic-gate 	cnt = sctp_maxburst;
4127c478bd9Sstevel@tonic-gate 
4137c478bd9Sstevel@tonic-gate 	/*
4147c478bd9Sstevel@tonic-gate 	 * Walk through all faddrs.  Since the timer should run infrequently
4157c478bd9Sstevel@tonic-gate 	 * and the number of peer addresses should not be big, this should
4167c478bd9Sstevel@tonic-gate 	 * be OK.
4177c478bd9Sstevel@tonic-gate 	 */
4187c478bd9Sstevel@tonic-gate 	for (fp = sctp->sctp_faddrs; fp != NULL; fp = fp->next) {
4197c478bd9Sstevel@tonic-gate 		/*
4207c478bd9Sstevel@tonic-gate 		 * Don't send heartbeat to this address if
4217c478bd9Sstevel@tonic-gate 		 * 1. it is not reachable OR
4227c478bd9Sstevel@tonic-gate 		 * 2. hb_interval == 0 and the address has been confirmed.
4237c478bd9Sstevel@tonic-gate 		 */
4247c478bd9Sstevel@tonic-gate 		if (fp->state == SCTP_FADDRS_UNREACH ||
4257c478bd9Sstevel@tonic-gate 		    (fp->hb_interval == 0 &&
4267c478bd9Sstevel@tonic-gate 		    fp->state != SCTP_FADDRS_UNCONFIRMED)) {
4277c478bd9Sstevel@tonic-gate 			continue;
4287c478bd9Sstevel@tonic-gate 		}
4297c478bd9Sstevel@tonic-gate 
4307c478bd9Sstevel@tonic-gate 		/*
4317c478bd9Sstevel@tonic-gate 		 * The heartbeat timer is expired.  If the address is dead,
4327c478bd9Sstevel@tonic-gate 		 * we still send heartbeat to it in case it becomes alive
4337c478bd9Sstevel@tonic-gate 		 * again.  But we will only send once every hb_interval.
4347c478bd9Sstevel@tonic-gate 		 *
4357c478bd9Sstevel@tonic-gate 		 * If the address is alive and there is a hearbeat pending,
4367c478bd9Sstevel@tonic-gate 		 * resend the heartbeat and start exponential backoff on the
4377c478bd9Sstevel@tonic-gate 		 * heartbeat timeout value.  If there is no heartbeat pending,
4387c478bd9Sstevel@tonic-gate 		 * just send out one.
4397c478bd9Sstevel@tonic-gate 		 */
4407c478bd9Sstevel@tonic-gate 		if (now >= fp->hb_expiry) {
4417c478bd9Sstevel@tonic-gate 			if (fp->hb_pending) {
4427c478bd9Sstevel@tonic-gate 				/*
4437c478bd9Sstevel@tonic-gate 				 * If an address is not confirmed, no need
4447c478bd9Sstevel@tonic-gate 				 * to bump the overall counter as it doesn't
4457c478bd9Sstevel@tonic-gate 				 * matter as we will not use it to send data
4467c478bd9Sstevel@tonic-gate 				 * and it should not affect the association.
4477c478bd9Sstevel@tonic-gate 				 */
4487c478bd9Sstevel@tonic-gate 				switch (fp->state) {
4497c478bd9Sstevel@tonic-gate 				case SCTP_FADDRS_ALIVE:
4507c478bd9Sstevel@tonic-gate 					sctp->sctp_strikes++;
4517c478bd9Sstevel@tonic-gate 					/* FALLTHRU */
4527c478bd9Sstevel@tonic-gate 				case SCTP_FADDRS_UNCONFIRMED:
4537c478bd9Sstevel@tonic-gate 					/*
4547c478bd9Sstevel@tonic-gate 					 * Retransmission implies that RTO
4557c478bd9Sstevel@tonic-gate 					 * is probably not correct.
4567c478bd9Sstevel@tonic-gate 					 */
4577c478bd9Sstevel@tonic-gate 					fp->rtt_updates = 0;
4587c478bd9Sstevel@tonic-gate 					fp->strikes++;
4597c478bd9Sstevel@tonic-gate 					if (fp->strikes > fp->max_retr) {
4607c478bd9Sstevel@tonic-gate 						if (sctp_faddr_dead(sctp, fp,
4617c478bd9Sstevel@tonic-gate 						    SCTP_FADDRS_DOWN) == -1) {
4627c478bd9Sstevel@tonic-gate 							/* Assoc is dead */
4637c478bd9Sstevel@tonic-gate 							return;
4647c478bd9Sstevel@tonic-gate 						}
4657c478bd9Sstevel@tonic-gate 						/*
4667c478bd9Sstevel@tonic-gate 						 * Addr is down; keep initial
4677c478bd9Sstevel@tonic-gate 						 * RTO
4687c478bd9Sstevel@tonic-gate 						 */
4697c478bd9Sstevel@tonic-gate 						fp->rto =
4707c478bd9Sstevel@tonic-gate 						    sctp->sctp_rto_initial;
4717c478bd9Sstevel@tonic-gate 						goto dead_addr;
4727c478bd9Sstevel@tonic-gate 					} else {
4737c478bd9Sstevel@tonic-gate 						SCTP_CALC_RXT(fp,
4747c478bd9Sstevel@tonic-gate 						    sctp->sctp_rto_max);
4757c478bd9Sstevel@tonic-gate 						fp->hb_expiry = now + fp->rto;
4767c478bd9Sstevel@tonic-gate 					}
4777c478bd9Sstevel@tonic-gate 					break;
4787c478bd9Sstevel@tonic-gate 				case SCTP_FADDRS_DOWN:
4797c478bd9Sstevel@tonic-gate dead_addr:
4807c478bd9Sstevel@tonic-gate 					fp->hb_expiry = now + SET_HB_INTVL(fp);
4817c478bd9Sstevel@tonic-gate 					break;
4827c478bd9Sstevel@tonic-gate 				default:
4837c478bd9Sstevel@tonic-gate 					continue;
4847c478bd9Sstevel@tonic-gate 				}
4857c478bd9Sstevel@tonic-gate 			} else {
4867c478bd9Sstevel@tonic-gate 				fp->hb_expiry = now + fp->rto;
4877c478bd9Sstevel@tonic-gate 			}
4887c478bd9Sstevel@tonic-gate 			/*
4897c478bd9Sstevel@tonic-gate 			 * Note that the total number of heartbeat we can send
4907c478bd9Sstevel@tonic-gate 			 * out simultaneously is limited by sctp_maxburst.  If
4917c478bd9Sstevel@tonic-gate 			 * the limit is exceeded, we need to wait for the next
4927c478bd9Sstevel@tonic-gate 			 * timeout to send them.  This should only happen if
4937c478bd9Sstevel@tonic-gate 			 * there is unconfirmed address.  Note that hb_pending
4947c478bd9Sstevel@tonic-gate 			 * is set in sctp_send_heartbeat().  So if a heartbeat
4957c478bd9Sstevel@tonic-gate 			 * is not sent, it will not affect the state of the
4967c478bd9Sstevel@tonic-gate 			 * peer address.
4977c478bd9Sstevel@tonic-gate 			 */
4987c478bd9Sstevel@tonic-gate 			if (fp->state != SCTP_FADDRS_UNCONFIRMED || cnt-- > 0)
4997c478bd9Sstevel@tonic-gate 				sctp_send_heartbeat(sctp, fp);
5007c478bd9Sstevel@tonic-gate 		}
5017c478bd9Sstevel@tonic-gate 		if (fp->hb_expiry < earliest_expiry || earliest_expiry == 0)
5027c478bd9Sstevel@tonic-gate 			earliest_expiry = fp->hb_expiry;
5037c478bd9Sstevel@tonic-gate 	}
5047c478bd9Sstevel@tonic-gate 	if (sctp->sctp_autoclose != 0) {
5057c478bd9Sstevel@tonic-gate 		int64_t expire;
5067c478bd9Sstevel@tonic-gate 
5077c478bd9Sstevel@tonic-gate 		expire = sctp->sctp_active + sctp->sctp_autoclose;
5087c478bd9Sstevel@tonic-gate 
5097c478bd9Sstevel@tonic-gate 		if (expire <= now) {
5107c478bd9Sstevel@tonic-gate 			dprint(3, ("sctp_heartbeat_timer: autoclosing\n"));
5117c478bd9Sstevel@tonic-gate 			sctp_send_shutdown(sctp, 0);
5127c478bd9Sstevel@tonic-gate 			return;
5137c478bd9Sstevel@tonic-gate 		}
5147c478bd9Sstevel@tonic-gate 		if (expire < earliest_expiry || earliest_expiry == 0)
5157c478bd9Sstevel@tonic-gate 			earliest_expiry = expire;
5167c478bd9Sstevel@tonic-gate 	}
5177c478bd9Sstevel@tonic-gate 
5187c478bd9Sstevel@tonic-gate 	earliest_expiry -= now;
5197c478bd9Sstevel@tonic-gate 	if (earliest_expiry < 0)
5207c478bd9Sstevel@tonic-gate 		earliest_expiry = 1;
5217c478bd9Sstevel@tonic-gate 	sctp_timer(sctp, sctp->sctp_heartbeat_mp, earliest_expiry);
5227c478bd9Sstevel@tonic-gate }
5237c478bd9Sstevel@tonic-gate 
5247c478bd9Sstevel@tonic-gate void
5257c478bd9Sstevel@tonic-gate sctp_rexmit_timer(sctp_t *sctp, sctp_faddr_t *fp)
5267c478bd9Sstevel@tonic-gate {
5277c478bd9Sstevel@tonic-gate 	mblk_t 		*mp;
5287c478bd9Sstevel@tonic-gate 	uint32_t	rto_max = sctp->sctp_rto_max;
5297c478bd9Sstevel@tonic-gate 
5307c478bd9Sstevel@tonic-gate 	ASSERT(fp != NULL);
5317c478bd9Sstevel@tonic-gate 
5327c478bd9Sstevel@tonic-gate 	dprint(3, ("sctp_timer: faddr=%x:%x:%x:%x\n",
5337c478bd9Sstevel@tonic-gate 	    SCTP_PRINTADDR(fp->faddr)));
5347c478bd9Sstevel@tonic-gate 
5357c478bd9Sstevel@tonic-gate 	fp->timer_running = 0;
5367c478bd9Sstevel@tonic-gate 
5377c478bd9Sstevel@tonic-gate 	/* Check is we've reached the max for retries */
5387c478bd9Sstevel@tonic-gate 	if (sctp->sctp_state < SCTPS_ESTABLISHED) {
5397c478bd9Sstevel@tonic-gate 		if (fp->strikes >= sctp->sctp_max_init_rxt) {
5407c478bd9Sstevel@tonic-gate 			/* time to give up */
5417c478bd9Sstevel@tonic-gate 			BUMP_MIB(&sctp_mib, sctpAborted);
5427c478bd9Sstevel@tonic-gate 			BUMP_MIB(&sctp_mib, sctpTimRetransDrop);
5437c478bd9Sstevel@tonic-gate 			sctp_assoc_event(sctp, SCTP_CANT_STR_ASSOC, 0, NULL);
5447c478bd9Sstevel@tonic-gate 			sctp_clean_death(sctp, sctp->sctp_client_errno ?
5457c478bd9Sstevel@tonic-gate 			    sctp->sctp_client_errno : ETIMEDOUT);
5467c478bd9Sstevel@tonic-gate 			return;
5477c478bd9Sstevel@tonic-gate 		}
5487c478bd9Sstevel@tonic-gate 	} else if (sctp->sctp_state >= SCTPS_ESTABLISHED) {
5497c478bd9Sstevel@tonic-gate 		if (sctp->sctp_strikes >= sctp->sctp_pa_max_rxt) {
5507c478bd9Sstevel@tonic-gate 			/* time to give up */
5517c478bd9Sstevel@tonic-gate 			BUMP_MIB(&sctp_mib, sctpAborted);
5527c478bd9Sstevel@tonic-gate 			BUMP_MIB(&sctp_mib, sctpTimRetransDrop);
5537c478bd9Sstevel@tonic-gate 			sctp_assoc_event(sctp, SCTP_COMM_LOST, 0, NULL);
5547c478bd9Sstevel@tonic-gate 			sctp_clean_death(sctp, sctp->sctp_client_errno ?
5557c478bd9Sstevel@tonic-gate 			    sctp->sctp_client_errno : ETIMEDOUT);
5567c478bd9Sstevel@tonic-gate 			return;
5577c478bd9Sstevel@tonic-gate 		}
5587c478bd9Sstevel@tonic-gate 	}
5597c478bd9Sstevel@tonic-gate 
5607c478bd9Sstevel@tonic-gate 	if (fp->strikes >= fp->max_retr) {
5617c478bd9Sstevel@tonic-gate 		if (sctp_faddr_dead(sctp, fp, SCTP_FADDRS_DOWN) == -1) {
5627c478bd9Sstevel@tonic-gate 			return;
5637c478bd9Sstevel@tonic-gate 		}
5647c478bd9Sstevel@tonic-gate 	}
5657c478bd9Sstevel@tonic-gate 
5667c478bd9Sstevel@tonic-gate 	switch (sctp->sctp_state) {
5677c478bd9Sstevel@tonic-gate 	case SCTPS_ESTABLISHED:
5687c478bd9Sstevel@tonic-gate 		/*
5697c478bd9Sstevel@tonic-gate 		 * Reset the heartbeat expiry time.  We don't need a heartbeat
5707c478bd9Sstevel@tonic-gate 		 * timer running if we are retransmitting.  Otherwise, the drop
5717c478bd9Sstevel@tonic-gate 		 * of heartbeat may just make this peer address to be marked
5727c478bd9Sstevel@tonic-gate 		 * dead faster as fp->strikes is also increased for heartbeat.
5737c478bd9Sstevel@tonic-gate 		 */
5747c478bd9Sstevel@tonic-gate 		fp->hb_expiry = lbolt64 + SET_HB_INTVL(fp);
5757c478bd9Sstevel@tonic-gate 		fp->hb_pending = B_FALSE;
5767c478bd9Sstevel@tonic-gate 
5777c478bd9Sstevel@tonic-gate 		/* FALLTHRU */
5787c478bd9Sstevel@tonic-gate 	case SCTPS_SHUTDOWN_PENDING:
5797c478bd9Sstevel@tonic-gate 	case SCTPS_SHUTDOWN_RECEIVED:
5807c478bd9Sstevel@tonic-gate 		if (sctp->sctp_state == SCTPS_SHUTDOWN_RECEIVED) {
5817c478bd9Sstevel@tonic-gate 			(void) sctp_shutdown_received(sctp, NULL, 0, 1);
5827c478bd9Sstevel@tonic-gate 		}
5837c478bd9Sstevel@tonic-gate 
5847c478bd9Sstevel@tonic-gate 		if (sctp->sctp_xmit_head == NULL &&
5857c478bd9Sstevel@tonic-gate 		    sctp->sctp_xmit_unsent == NULL) {
5867c478bd9Sstevel@tonic-gate 			/* Nothing to retransmit */
5877c478bd9Sstevel@tonic-gate 			if (sctp->sctp_state == SCTPS_SHUTDOWN_PENDING) {
5887c478bd9Sstevel@tonic-gate 				sctp_send_shutdown(sctp, 1);
5897c478bd9Sstevel@tonic-gate 			}
5907c478bd9Sstevel@tonic-gate 			return;
5917c478bd9Sstevel@tonic-gate 		}
5927c478bd9Sstevel@tonic-gate 
5937c478bd9Sstevel@tonic-gate 		BUMP_MIB(&sctp_mib, sctpTimRetrans);
5947c478bd9Sstevel@tonic-gate 
5957c478bd9Sstevel@tonic-gate 		sctp_rexmit(sctp, fp);
5967c478bd9Sstevel@tonic-gate 		/*
5977c478bd9Sstevel@tonic-gate 		 * sctp_rexmit() will increase the strikes and restart the
5987c478bd9Sstevel@tonic-gate 		 * timer, so return here.
5997c478bd9Sstevel@tonic-gate 		 */
6007c478bd9Sstevel@tonic-gate 		return;
6017c478bd9Sstevel@tonic-gate 	case SCTPS_COOKIE_WAIT:
6027c478bd9Sstevel@tonic-gate 		BUMP_LOCAL(sctp->sctp_T1expire);
6037c478bd9Sstevel@tonic-gate rxmit_init:
6047c478bd9Sstevel@tonic-gate 		/* retransmit init */
605f551bb10Svi 		/*
606f551bb10Svi 		 * We don't take the conn hash lock here since the source
607f551bb10Svi 		 * address list won't be modified (it would have been done
608f551bb10Svi 		 * the first time around).
609f551bb10Svi 		 */
6107c478bd9Sstevel@tonic-gate 		mp = sctp_init_mp(sctp);
6117c478bd9Sstevel@tonic-gate 		if (mp != NULL) {
6127c478bd9Sstevel@tonic-gate 			BUMP_MIB(&sctp_mib, sctpTimRetrans);
6137c478bd9Sstevel@tonic-gate 			sctp_add_sendq(sctp, mp);
6147c478bd9Sstevel@tonic-gate 		}
6157c478bd9Sstevel@tonic-gate 		rto_max = sctp->sctp_init_rto_max;
6167c478bd9Sstevel@tonic-gate 		break;
6177c478bd9Sstevel@tonic-gate 	case SCTPS_COOKIE_ECHOED: {
6187c478bd9Sstevel@tonic-gate 		ipha_t *iph;
6197c478bd9Sstevel@tonic-gate 
6207c478bd9Sstevel@tonic-gate 		BUMP_LOCAL(sctp->sctp_T1expire);
6217c478bd9Sstevel@tonic-gate 		if (sctp->sctp_cookie_mp == NULL) {
6227c478bd9Sstevel@tonic-gate 			sctp->sctp_state = SCTPS_COOKIE_WAIT;
6237c478bd9Sstevel@tonic-gate 			goto rxmit_init;
6247c478bd9Sstevel@tonic-gate 		}
6257c478bd9Sstevel@tonic-gate 		mp = dupmsg(sctp->sctp_cookie_mp);
6267c478bd9Sstevel@tonic-gate 		if (mp == NULL)
6277c478bd9Sstevel@tonic-gate 			break;
6287c478bd9Sstevel@tonic-gate 		iph = (ipha_t *)mp->b_rptr;
6297c478bd9Sstevel@tonic-gate 		/* Reset the IP ident. */
6307c478bd9Sstevel@tonic-gate 		if (IPH_HDR_VERSION(iph) == IPV4_VERSION)
6317c478bd9Sstevel@tonic-gate 			iph->ipha_ident = 0;
6327c478bd9Sstevel@tonic-gate 		sctp_add_sendq(sctp, mp);
6337c478bd9Sstevel@tonic-gate 		BUMP_MIB(&sctp_mib, sctpTimRetrans);
6347c478bd9Sstevel@tonic-gate 		rto_max = sctp->sctp_init_rto_max;
6357c478bd9Sstevel@tonic-gate 		break;
6367c478bd9Sstevel@tonic-gate 	}
6377c478bd9Sstevel@tonic-gate 	case SCTPS_SHUTDOWN_SENT:
6387c478bd9Sstevel@tonic-gate 		BUMP_LOCAL(sctp->sctp_T2expire);
6397c478bd9Sstevel@tonic-gate 		sctp_send_shutdown(sctp, 1);
6407c478bd9Sstevel@tonic-gate 		BUMP_MIB(&sctp_mib, sctpTimRetrans);
6417c478bd9Sstevel@tonic-gate 		break;
6427c478bd9Sstevel@tonic-gate 	case SCTPS_SHUTDOWN_ACK_SENT:
6437c478bd9Sstevel@tonic-gate 		/* We shouldn't have any more outstanding data */
6447c478bd9Sstevel@tonic-gate 		ASSERT(sctp->sctp_xmit_head == NULL);
6457c478bd9Sstevel@tonic-gate 		ASSERT(sctp->sctp_xmit_unsent == NULL);
6467c478bd9Sstevel@tonic-gate 
6477c478bd9Sstevel@tonic-gate 		BUMP_LOCAL(sctp->sctp_T2expire);
6487c478bd9Sstevel@tonic-gate 		(void) sctp_shutdown_received(sctp, NULL, 0, 1);
6497c478bd9Sstevel@tonic-gate 		BUMP_MIB(&sctp_mib, sctpTimRetrans);
6507c478bd9Sstevel@tonic-gate 		break;
6517c478bd9Sstevel@tonic-gate 	default:
6527c478bd9Sstevel@tonic-gate 		ASSERT(0);
6537c478bd9Sstevel@tonic-gate 		break;
6547c478bd9Sstevel@tonic-gate 	}
6557c478bd9Sstevel@tonic-gate 
6567c478bd9Sstevel@tonic-gate 	fp->strikes++;
6577c478bd9Sstevel@tonic-gate 	sctp->sctp_strikes++;
6587c478bd9Sstevel@tonic-gate 	SCTP_CALC_RXT(fp, rto_max);
6597c478bd9Sstevel@tonic-gate 
6607c478bd9Sstevel@tonic-gate 	SCTP_FADDR_TIMER_RESTART(sctp, fp, fp->rto);
6617c478bd9Sstevel@tonic-gate }
6627c478bd9Sstevel@tonic-gate 
6637c478bd9Sstevel@tonic-gate /*
6647c478bd9Sstevel@tonic-gate  * RTO calculation. timesent and now are both in ms.
6657c478bd9Sstevel@tonic-gate  */
6667c478bd9Sstevel@tonic-gate void
6677c478bd9Sstevel@tonic-gate sctp_update_rtt(sctp_t *sctp, sctp_faddr_t *fp, clock_t delta)
6687c478bd9Sstevel@tonic-gate {
6697c478bd9Sstevel@tonic-gate 	int rtt;
6707c478bd9Sstevel@tonic-gate 
6717c478bd9Sstevel@tonic-gate 	/* Calculate the RTT in ms */
6727c478bd9Sstevel@tonic-gate 	rtt = (int)delta;
6737c478bd9Sstevel@tonic-gate 	rtt = rtt > 0 ? rtt : 1;
6747c478bd9Sstevel@tonic-gate 
675*45916cd2Sjpk 	dprint(5, ("sctp_update_rtt: fp = %p, rtt = %d\n", (void *)fp, rtt));
6767c478bd9Sstevel@tonic-gate 
6777c478bd9Sstevel@tonic-gate 	/* Is this the first RTT measurement? */
6787c478bd9Sstevel@tonic-gate 	if (fp->srtt == -1) {
6797c478bd9Sstevel@tonic-gate 		fp->srtt = rtt;
6807c478bd9Sstevel@tonic-gate 		fp->rttvar = rtt / 2;
6817c478bd9Sstevel@tonic-gate 		fp->rto = 3 * rtt; /* == rtt + 4 * rttvar ( == rtt / 2) */
6827c478bd9Sstevel@tonic-gate 	} else {
6837c478bd9Sstevel@tonic-gate 		int abs;
6847c478bd9Sstevel@tonic-gate 		/*
6857c478bd9Sstevel@tonic-gate 		 * Versions of the RTO equations that use fixed-point math.
6867c478bd9Sstevel@tonic-gate 		 * alpha and beta are NOT tunable in this implementation,
6877c478bd9Sstevel@tonic-gate 		 * and so are hard-coded in. alpha = 1/8, beta = 1/4.
6887c478bd9Sstevel@tonic-gate 		 */
6897c478bd9Sstevel@tonic-gate 		abs = fp->srtt - rtt;
6907c478bd9Sstevel@tonic-gate 		abs = abs >= 0 ? abs : -abs;
6917c478bd9Sstevel@tonic-gate 		fp->rttvar = (3 * fp->rttvar + abs) >> 2;
6927c478bd9Sstevel@tonic-gate 		fp->rttvar = fp->rttvar != 0 ? fp->rttvar : 1;
6937c478bd9Sstevel@tonic-gate 
6947c478bd9Sstevel@tonic-gate 		fp->srtt = (7 * fp->srtt + rtt) >> 3;
6957c478bd9Sstevel@tonic-gate 		fp->rto = fp->srtt + 4 * fp->rttvar;
6967c478bd9Sstevel@tonic-gate 	}
6977c478bd9Sstevel@tonic-gate 
6987c478bd9Sstevel@tonic-gate 	dprint(5, ("sctp_update_rtt: srtt = %d, rttvar = %d, rto = %d\n",
6997c478bd9Sstevel@tonic-gate 	    fp->srtt, fp->rttvar, fp->rto));
7007c478bd9Sstevel@tonic-gate 
7017c478bd9Sstevel@tonic-gate 	/* Bound the RTO by configured min and max values */
7027c478bd9Sstevel@tonic-gate 	if (fp->rto < sctp->sctp_rto_min) {
7037c478bd9Sstevel@tonic-gate 		fp->rto = sctp->sctp_rto_min;
7047c478bd9Sstevel@tonic-gate 	}
7057c478bd9Sstevel@tonic-gate 	if (fp->rto > sctp->sctp_rto_max) {
7067c478bd9Sstevel@tonic-gate 		fp->rto = sctp->sctp_rto_max;
7077c478bd9Sstevel@tonic-gate 	}
7087c478bd9Sstevel@tonic-gate 
7097c478bd9Sstevel@tonic-gate 	fp->rtt_updates++;
7107c478bd9Sstevel@tonic-gate }
7117c478bd9Sstevel@tonic-gate 
7127c478bd9Sstevel@tonic-gate void
7137c478bd9Sstevel@tonic-gate sctp_free_faddr_timers(sctp_t *sctp)
7147c478bd9Sstevel@tonic-gate {
7157c478bd9Sstevel@tonic-gate 	sctp_faddr_t *fp;
7167c478bd9Sstevel@tonic-gate 
7177c478bd9Sstevel@tonic-gate 	for (fp = sctp->sctp_faddrs; fp != NULL; fp = fp->next) {
7187c478bd9Sstevel@tonic-gate 		if (fp->timer_mp != NULL) {
7197c478bd9Sstevel@tonic-gate 			sctp_timer_free(fp->timer_mp);
7207c478bd9Sstevel@tonic-gate 			fp->timer_mp = NULL;
7217c478bd9Sstevel@tonic-gate 			fp->timer_running = 0;
7227c478bd9Sstevel@tonic-gate 		}
7237c478bd9Sstevel@tonic-gate 		if (fp->rc_timer_mp != NULL) {
7247c478bd9Sstevel@tonic-gate 			sctp_timer_free(fp->rc_timer_mp);
7257c478bd9Sstevel@tonic-gate 			fp->rc_timer_mp = NULL;
7267c478bd9Sstevel@tonic-gate 			fp->rc_timer_running = 0;
7277c478bd9Sstevel@tonic-gate 		}
7287c478bd9Sstevel@tonic-gate 	}
7297c478bd9Sstevel@tonic-gate }
7307c478bd9Sstevel@tonic-gate 
7317c478bd9Sstevel@tonic-gate void
7327c478bd9Sstevel@tonic-gate sctp_stop_faddr_timers(sctp_t *sctp)
7337c478bd9Sstevel@tonic-gate {
7347c478bd9Sstevel@tonic-gate 	sctp_faddr_t *fp;
7357c478bd9Sstevel@tonic-gate 
7367c478bd9Sstevel@tonic-gate 	for (fp = sctp->sctp_faddrs; fp != NULL; fp = fp->next) {
7377c478bd9Sstevel@tonic-gate 		SCTP_FADDR_TIMER_STOP(fp);
7387c478bd9Sstevel@tonic-gate 		SCTP_FADDR_RC_TIMER_STOP(fp);
7397c478bd9Sstevel@tonic-gate 	}
7407c478bd9Sstevel@tonic-gate }
7417c478bd9Sstevel@tonic-gate 
7427c478bd9Sstevel@tonic-gate void
7437c478bd9Sstevel@tonic-gate sctp_process_timer(sctp_t *sctp)
7447c478bd9Sstevel@tonic-gate {
7457c478bd9Sstevel@tonic-gate 	mblk_t *mp;
7467c478bd9Sstevel@tonic-gate 
7477c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_running);
7487c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&sctp->sctp_lock));
7497c478bd9Sstevel@tonic-gate 	while ((mp = sctp->sctp_timer_mp) != NULL) {
7507c478bd9Sstevel@tonic-gate 		ASSERT(DB_TYPE(mp) == M_PCSIG);
7517c478bd9Sstevel@tonic-gate 		/*
7527c478bd9Sstevel@tonic-gate 		 * Since the timer mblk can be freed in sctp_timer_call(),
7537c478bd9Sstevel@tonic-gate 		 * we need to grab the b_cont before that.
7547c478bd9Sstevel@tonic-gate 		 */
7557c478bd9Sstevel@tonic-gate 		sctp->sctp_timer_mp = mp->b_cont;
7567c478bd9Sstevel@tonic-gate 		mp->b_cont = NULL;
7577c478bd9Sstevel@tonic-gate 		sctp_timer_call(sctp, mp);
7587c478bd9Sstevel@tonic-gate 	}
7597c478bd9Sstevel@tonic-gate 	SCTP_REFRELE(sctp);
7607c478bd9Sstevel@tonic-gate }
761