xref: /illumos-gate/usr/src/uts/common/inet/sctp/sctp_timer.c (revision 3e95bd4ab92abca814bd28e854607d1975c7dc88)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
24  */
25 
26 #include <sys/types.h>
27 #include <sys/systm.h>
28 #include <sys/stream.h>
29 #include <sys/cmn_err.h>
30 #include <sys/strsubr.h>
31 #include <sys/strsun.h>
32 
33 #include <netinet/in.h>
34 #include <netinet/ip6.h>
35 
36 #include <inet/common.h>
37 #include <inet/ip.h>
38 #include <inet/mib2.h>
39 #include <inet/ipclassifier.h>
40 #include "sctp_impl.h"
41 #include "sctp_asconf.h"
42 
43 /* Timer block states. */
44 typedef enum {
45 	SCTP_TB_RUNNING = 1,
46 	SCTP_TB_IDLE,
47 /* Could not stop/free before mblk got queued */
48 	SCTP_TB_RESCHED,	/* sctp_tb_time_left contains tick count */
49 	SCTP_TB_CANCELLED,
50 	SCTP_TB_TO_BE_FREED
51 } timer_block_state;
52 
53 typedef struct sctp_tb_s {
54 	timer_block_state	sctp_tb_state;
55 	timeout_id_t		sctp_tb_tid;
56 	mblk_t			*sctp_tb_mp;
57 	clock_t			sctp_tb_time_left;
58 } sctp_tb_t;
59 
60 static void sctp_timer_fire(sctp_tb_t *);
61 
62 /*
63  *		sctp_timer mechanism.
64  *
65  * Each timer is represented by a timer mblk. When the
66  * timer fires, and the sctp_t is busy, the timer mblk will be put on
67  * the associated sctp_t timer queue so that it can be executed when
68  * the thread holding the lock on the sctp_t is done with its job.
69  *
70  * Note that there is no lock to protect the timer mblk state.  The reason
71  * is that the timer state can only be changed by a thread holding the
72  * lock on the sctp_t.
73  *
74  * The interface consists of 4 entry points:
75  *	sctp_timer_alloc	- create a timer mblk
76  *	sctp_timer_free		- free a timer mblk
77  *	sctp_timer		- start, restart, stop the timer
78  *	sctp_timer_valid	- called by sctp_process_recvq to verify that
79  *				  the timer did indeed fire.
80  */
81 
82 
83 /*
84  * Start, restart, stop the timer.
85  * If "tim" is -1 the timer is stopped.
86  * Otherwise, the timer is stopped if it is already running, and
87  * set to fire tim clock ticks from now.
88  */
89 void
90 sctp_timer(sctp_t *sctp, mblk_t *mp, clock_t tim)
91 {
92 	sctp_tb_t *sctp_tb;
93 	int state;
94 
95 	ASSERT(sctp != NULL && mp != NULL);
96 	ASSERT((mp->b_rptr - mp->b_datap->db_base) == sizeof (sctp_tb_t));
97 	ASSERT(mp->b_datap->db_type == M_PCSIG);
98 
99 	sctp_tb = (sctp_tb_t *)mp->b_datap->db_base;
100 	if (tim >= 0) {
101 		state = sctp_tb->sctp_tb_state;
102 		sctp_tb->sctp_tb_time_left = tim;
103 		if (state == SCTP_TB_RUNNING) {
104 			if (untimeout(sctp_tb->sctp_tb_tid) < 0) {
105 				sctp_tb->sctp_tb_state = SCTP_TB_RESCHED;
106 				/* sctp_timer_valid will start timer */
107 				return;
108 			}
109 		} else if (state != SCTP_TB_IDLE) {
110 			ASSERT(state != SCTP_TB_TO_BE_FREED);
111 			if (state == SCTP_TB_CANCELLED) {
112 				sctp_tb->sctp_tb_state = SCTP_TB_RESCHED;
113 				/* sctp_timer_valid will start timer */
114 				return;
115 			}
116 			if (state == SCTP_TB_RESCHED) {
117 				/* sctp_timer_valid will start timer */
118 				return;
119 			}
120 		} else {
121 			SCTP_REFHOLD(sctp);
122 		}
123 		sctp_tb->sctp_tb_state = SCTP_TB_RUNNING;
124 		sctp_tb->sctp_tb_tid =
125 		    timeout((pfv_t)sctp_timer_fire, sctp_tb, tim);
126 		return;
127 	}
128 	switch (tim) {
129 	case -1:
130 		sctp_timer_stop(mp);
131 		break;
132 	default:
133 		ASSERT(0);
134 		break;
135 	}
136 }
137 
138 /*
139  * sctp_timer_alloc is called by sctp_init to allocate and initialize a
140  * sctp timer.
141  *
142  * Allocate an M_PCSIG timer message. The space between db_base and
143  * b_rptr is used by the sctp_timer mechanism, and after b_rptr there is
144  * space for sctpt_t.
145  */
146 mblk_t *
147 sctp_timer_alloc(sctp_t *sctp, pfv_t func, int sleep)
148 {
149 	mblk_t *mp;
150 	sctp_tb_t *sctp_tb;
151 	sctpt_t	*sctpt;
152 	sctp_stack_t	*sctps = sctp->sctp_sctps;
153 
154 	if (sleep == KM_SLEEP) {
155 		mp = allocb_wait(sizeof (sctp_t) + sizeof (sctp_tb_t), BPRI_HI,
156 		    STR_NOSIG, NULL);
157 	} else {
158 		mp = allocb(sizeof (sctp_t) + sizeof (sctp_tb_t), BPRI_HI);
159 	}
160 	if (mp != NULL) {
161 		mp->b_datap->db_type = M_PCSIG;
162 		sctp_tb = (sctp_tb_t *)mp->b_datap->db_base;
163 		mp->b_rptr = (uchar_t *)&sctp_tb[1];
164 		mp->b_wptr = mp->b_rptr + sizeof (sctpt_t);
165 		sctp_tb->sctp_tb_state = SCTP_TB_IDLE;
166 		sctp_tb->sctp_tb_mp = mp;
167 
168 		sctpt = (sctpt_t *)mp->b_rptr;
169 		sctpt->sctpt_sctp = sctp;
170 		sctpt->sctpt_faddr = NULL;	/* set when starting timer */
171 		sctpt->sctpt_pfv = func;
172 		return (mp);
173 	}
174 	SCTP_KSTAT(sctps, sctp_add_timer);
175 	return (NULL);
176 }
177 
178 /*
179  * timeout() callback function.
180  * Put the message on the process control block's queue.
181  * If the timer is stopped or freed after
182  * it has fired then sctp_timer() and sctp_timer_valid() will clean
183  * things up.
184  */
185 static void
186 sctp_timer_fire(sctp_tb_t *sctp_tb)
187 {
188 	mblk_t *mp;
189 	sctp_t *sctp;
190 	sctpt_t *sctpt;
191 
192 	mp = sctp_tb->sctp_tb_mp;
193 	ASSERT(sctp_tb == (sctp_tb_t *)mp->b_datap->db_base);
194 	ASSERT(mp->b_datap->db_type == M_PCSIG);
195 
196 	sctpt = (sctpt_t *)mp->b_rptr;
197 	sctp = sctpt->sctpt_sctp;
198 	ASSERT(sctp != NULL);
199 
200 	mutex_enter(&sctp->sctp_lock);
201 	if (sctp->sctp_running) {
202 		/*
203 		 * Put the timer mblk to the special sctp_timer_mp list.
204 		 * This timer will be handled when the thread using this
205 		 * SCTP is done with its job.
206 		 */
207 		if (sctp->sctp_timer_mp == NULL) {
208 			SCTP_REFHOLD(sctp);
209 			sctp->sctp_timer_mp = mp;
210 		} else {
211 			linkb(sctp->sctp_timer_mp, mp);
212 		}
213 		mp->b_cont = NULL;
214 		mutex_exit(&sctp->sctp_lock);
215 	} else {
216 		sctp->sctp_running = B_TRUE;
217 		mutex_exit(&sctp->sctp_lock);
218 
219 		sctp_timer_call(sctp, mp);
220 		WAKE_SCTP(sctp);
221 	}
222 	SCTP_REFRELE(sctp);
223 }
224 
225 /*
226  * Logically free a timer mblk (that might have a pending timeout().)
227  * If the timer has fired and the mblk has been put on the queue then
228  * sctp_timer_valid will free the mblk.
229  */
230 void
231 sctp_timer_free(mblk_t *mp)
232 {
233 	sctp_tb_t *sctp_tb;
234 	int state;
235 	sctpt_t *sctpt;
236 
237 	ASSERT(mp != NULL);
238 	ASSERT((mp->b_rptr - mp->b_datap->db_base) == sizeof (sctp_tb_t));
239 	ASSERT(mp->b_datap->db_type == M_PCSIG);
240 
241 	sctp_tb = (sctp_tb_t *)mp->b_datap->db_base;
242 	state = sctp_tb->sctp_tb_state;
243 
244 	dprint(5, ("sctp_timer_free %p state %d\n", (void *)mp, state));
245 
246 	if (state == SCTP_TB_RUNNING) {
247 		if (untimeout(sctp_tb->sctp_tb_tid) < 0) {
248 			sctp_tb->sctp_tb_state = SCTP_TB_TO_BE_FREED;
249 			/* sctp_timer_valid will free the mblk */
250 			return;
251 		}
252 		sctpt = (sctpt_t *)mp->b_rptr;
253 		SCTP_REFRELE(sctpt->sctpt_sctp);
254 	} else if (state != SCTP_TB_IDLE) {
255 		ASSERT(state != SCTP_TB_TO_BE_FREED);
256 		sctp_tb->sctp_tb_state = SCTP_TB_TO_BE_FREED;
257 		/* sctp_timer_valid will free the mblk */
258 		return;
259 	}
260 	freeb(mp);
261 }
262 
263 /*
264  * Called from sctp_timer(,,-1)
265  */
266 void
267 sctp_timer_stop(mblk_t *mp)
268 {
269 	sctp_tb_t *sctp_tb;
270 	int state;
271 	sctpt_t *sctpt;
272 
273 	ASSERT(mp != NULL);
274 	ASSERT(mp->b_datap->db_type == M_PCSIG);
275 
276 	sctp_tb = (sctp_tb_t *)mp->b_datap->db_base;
277 	state = sctp_tb->sctp_tb_state;
278 
279 	dprint(5, ("sctp_timer_stop %p %d\n", (void *)mp, state));
280 
281 	if (state == SCTP_TB_RUNNING) {
282 		if (untimeout(sctp_tb->sctp_tb_tid) < 0) {
283 			sctp_tb->sctp_tb_state = SCTP_TB_CANCELLED;
284 		} else {
285 			sctp_tb->sctp_tb_state = SCTP_TB_IDLE;
286 			sctpt = (sctpt_t *)mp->b_rptr;
287 			SCTP_REFRELE(sctpt->sctpt_sctp);
288 		}
289 	} else if (state == SCTP_TB_RESCHED) {
290 		sctp_tb->sctp_tb_state = SCTP_TB_CANCELLED;
291 	}
292 }
293 
294 /*
295  * The user of the sctp_timer mechanism is required to call
296  * sctp_timer_valid() for each M_PCSIG message processed in the
297  * service procedures.
298  * sctp_timer_valid will return "true" if the timer actually did fire.
299  */
300 
301 static boolean_t
302 sctp_timer_valid(mblk_t *mp)
303 {
304 	sctp_tb_t *sctp_tb;
305 	int state;
306 	sctpt_t *sctpt;
307 
308 	ASSERT(mp != NULL);
309 	ASSERT(mp->b_datap->db_type == M_PCSIG);
310 
311 	sctp_tb = (sctp_tb_t *)DB_BASE(mp);
312 	sctpt = (sctpt_t *)mp->b_rptr;
313 	state = sctp_tb->sctp_tb_state;
314 	if (state != SCTP_TB_RUNNING) {
315 		ASSERT(state != SCTP_TB_IDLE);
316 		if (state == SCTP_TB_TO_BE_FREED) {
317 			/*
318 			 * sctp_timer_free was called after the message
319 			 * was putq'ed.
320 			 */
321 			freeb(mp);
322 			return (B_FALSE);
323 		}
324 		if (state == SCTP_TB_CANCELLED) {
325 			/* The timer was stopped after the mblk was putq'ed */
326 			sctp_tb->sctp_tb_state = SCTP_TB_IDLE;
327 			return (B_FALSE);
328 		}
329 		if (state == SCTP_TB_RESCHED) {
330 			/*
331 			 * The timer was stopped and then restarted after
332 			 * the mblk was putq'ed.
333 			 * sctp_tb_time_left contains the number of ticks that
334 			 * the timer was restarted with.
335 			 * The sctp will not be disapper between the time
336 			 * the sctpt_t is marked SCTP_TB_RESCHED and when
337 			 * we get here as sctp_add_recvq() does a refhold.
338 			 */
339 			sctp_tb->sctp_tb_state = SCTP_TB_RUNNING;
340 			sctp_tb->sctp_tb_tid = timeout((pfv_t)sctp_timer_fire,
341 			    sctp_tb, sctp_tb->sctp_tb_time_left);
342 			SCTP_REFHOLD(sctpt->sctpt_sctp);
343 			return (B_FALSE);
344 		}
345 	}
346 	sctp_tb->sctp_tb_state = SCTP_TB_IDLE;
347 	return (B_TRUE);
348 }
349 
350 /*
351  * The SCTP timer call. Calls sctp_timer_valid() to verify whether
352  * timer was cancelled or not.
353  */
354 void
355 sctp_timer_call(sctp_t *sctp, mblk_t *mp)
356 {
357 	sctpt_t *sctpt = (sctpt_t *)mp->b_rptr;
358 
359 	if (sctp_timer_valid(mp)) {
360 		(*sctpt->sctpt_pfv)(sctp, sctpt->sctpt_faddr);
361 	}
362 }
363 
364 /*
365  * Delayed ack
366  */
367 void
368 sctp_ack_timer(sctp_t *sctp)
369 {
370 	sctp_stack_t	*sctps = sctp->sctp_sctps;
371 
372 	sctp->sctp_ack_timer_running = 0;
373 	sctp->sctp_sack_toggle = sctps->sctps_deferred_acks_max;
374 	BUMP_MIB(&sctps->sctps_mib, sctpOutAckDelayed);
375 	(void) sctp_sack(sctp, NULL);
376 }
377 
378 /*
379  * Peer address heartbeat timer handler
380  */
381 void
382 sctp_heartbeat_timer(sctp_t *sctp)
383 {
384 	sctp_faddr_t	*fp;
385 	int64_t		now;
386 	int64_t		earliest_expiry;
387 	int		cnt;
388 	sctp_stack_t	*sctps = sctp->sctp_sctps;
389 
390 	if (sctp->sctp_strikes >= sctp->sctp_pa_max_rxt) {
391 		/*
392 		 * If there is a peer address with no strikes,
393 		 * don't give up yet. If enough other peer
394 		 * address are down, we could otherwise fail
395 		 * the association prematurely.  This is a
396 		 * byproduct of our aggressive probe approach
397 		 * when a heartbeat fails to connect. We may
398 		 * wish to revisit this...
399 		 */
400 		if (!sctp_is_a_faddr_clean(sctp)) {
401 			/* time to give up */
402 			BUMP_MIB(&sctps->sctps_mib, sctpAborted);
403 			BUMP_MIB(&sctps->sctps_mib, sctpTimHeartBeatDrop);
404 			sctp_assoc_event(sctp, SCTP_COMM_LOST, 0, NULL);
405 			sctp_clean_death(sctp, sctp->sctp_client_errno ?
406 			    sctp->sctp_client_errno : ETIMEDOUT);
407 			return;
408 		}
409 	}
410 
411 	/* Only send heartbeats in the established state */
412 	if (sctp->sctp_state != SCTPS_ESTABLISHED) {
413 		dprint(5, ("sctp_heartbeat_timer: not in ESTABLISHED\n"));
414 		return;
415 	}
416 
417 	now = ddi_get_lbolt64();
418 	earliest_expiry = 0;
419 	cnt = sctps->sctps_maxburst;
420 
421 	/*
422 	 * Walk through all faddrs.  Since the timer should run infrequently
423 	 * and the number of peer addresses should not be big, this should
424 	 * be OK.
425 	 */
426 	for (fp = sctp->sctp_faddrs; fp != NULL; fp = fp->next) {
427 		/*
428 		 * If the peer is unreachable because there is no available
429 		 * source address, call sctp_get_dest() to see if it is
430 		 * reachable now.  If it is OK, the state will become
431 		 * unconfirmed.  And the following code to handle unconfirmed
432 		 * address will be executed.  If it is still not OK,
433 		 * re-schedule.  If heartbeat is enabled, only try this
434 		 * up to the normal heartbeat max times.  But if heartbeat
435 		 * is disable, this retry may go on forever.
436 		 */
437 		if (fp->state == SCTP_FADDRS_UNREACH) {
438 			sctp_get_dest(sctp, fp);
439 			if (fp->state == SCTP_FADDRS_UNREACH) {
440 				if (fp->hb_enabled &&
441 				    ++fp->strikes > fp->max_retr &&
442 				    sctp_faddr_dead(sctp, fp,
443 				    SCTP_FADDRS_DOWN) == -1) {
444 					/* Assoc is dead */
445 					return;
446 				}
447 				fp->hb_expiry = now + SET_HB_INTVL(fp);
448 				goto set_expiry;
449 			} else {
450 				/* Send a heartbeat immediately. */
451 				fp->hb_expiry = now;
452 			}
453 		}
454 		/*
455 		 * Don't send heartbeat to this address if it is not
456 		 * hb_enabled and the address has been confirmed.
457 		 */
458 		if (!fp->hb_enabled && fp->state != SCTP_FADDRS_UNCONFIRMED) {
459 			continue;
460 		}
461 
462 		/*
463 		 * The heartbeat timer is expired.  If the address is dead,
464 		 * we still send heartbeat to it in case it becomes alive
465 		 * again.  But we will only send once in a while, calculated
466 		 * by SET_HB_INTVL().
467 		 *
468 		 * If the address is alive and there is a hearbeat pending,
469 		 * resend the heartbeat and start exponential backoff on the
470 		 * heartbeat timeout value.  If there is no heartbeat pending,
471 		 * just send out one.
472 		 */
473 		if (now >= fp->hb_expiry) {
474 			if (fp->hb_pending) {
475 				/*
476 				 * If an address is not confirmed, no need
477 				 * to bump the overall counter as it doesn't
478 				 * matter as we will not use it to send data
479 				 * and it should not affect the association.
480 				 */
481 				switch (fp->state) {
482 				case SCTP_FADDRS_ALIVE:
483 					sctp->sctp_strikes++;
484 					/* FALLTHRU */
485 				case SCTP_FADDRS_UNCONFIRMED:
486 					/*
487 					 * Retransmission implies that RTO
488 					 * is probably not correct.
489 					 */
490 					fp->rtt_updates = 0;
491 					fp->strikes++;
492 					if (fp->strikes > fp->max_retr) {
493 						if (sctp_faddr_dead(sctp, fp,
494 						    SCTP_FADDRS_DOWN) == -1) {
495 							/* Assoc is dead */
496 							return;
497 						}
498 						/*
499 						 * Addr is down; keep initial
500 						 * RTO
501 						 */
502 						fp->rto =
503 						    sctp->sctp_rto_initial;
504 						goto dead_addr;
505 					} else {
506 						SCTP_CALC_RXT(sctp, fp,
507 						    sctp->sctp_rto_max);
508 						fp->hb_expiry = now + fp->rto;
509 					}
510 					break;
511 				case SCTP_FADDRS_DOWN:
512 dead_addr:
513 					fp->hb_expiry = now + SET_HB_INTVL(fp);
514 					break;
515 				default:
516 					continue;
517 				}
518 			} else {
519 				/*
520 				 * If there is unack'ed data, no need to
521 				 * send a heart beat.
522 				 */
523 				if (fp->suna > 0) {
524 					fp->hb_expiry = now + SET_HB_INTVL(fp);
525 					goto set_expiry;
526 				} else {
527 					fp->hb_expiry = now + fp->rto;
528 				}
529 			}
530 			/*
531 			 * Note that the total number of heartbeat we can send
532 			 * out simultaneously is limited by sctp_maxburst.  If
533 			 * the limit is exceeded, we need to wait for the next
534 			 * timeout to send them.  This should only happen if
535 			 * there is unconfirmed address.  Note that hb_pending
536 			 * is set in sctp_send_heartbeat().  So if a heartbeat
537 			 * is not sent, it will not affect the state of the
538 			 * peer address.
539 			 */
540 			if (fp->state != SCTP_FADDRS_UNCONFIRMED || cnt-- > 0)
541 				sctp_send_heartbeat(sctp, fp);
542 		}
543 set_expiry:
544 		if (fp->hb_expiry < earliest_expiry || earliest_expiry == 0)
545 			earliest_expiry = fp->hb_expiry;
546 	}
547 	if (sctp->sctp_autoclose != 0) {
548 		int64_t expire;
549 
550 		expire = sctp->sctp_active + sctp->sctp_autoclose;
551 
552 		if (expire <= now) {
553 			dprint(3, ("sctp_heartbeat_timer: autoclosing\n"));
554 			sctp_send_shutdown(sctp, 0);
555 			return;
556 		}
557 		if (expire < earliest_expiry || earliest_expiry == 0)
558 			earliest_expiry = expire;
559 	}
560 
561 	earliest_expiry -= now;
562 	if (earliest_expiry < 0)
563 		earliest_expiry = 1;
564 	sctp_timer(sctp, sctp->sctp_heartbeat_mp, earliest_expiry);
565 }
566 
567 void
568 sctp_rexmit_timer(sctp_t *sctp, sctp_faddr_t *fp)
569 {
570 	mblk_t 		*mp;
571 	uint32_t	rto_max = sctp->sctp_rto_max;
572 	sctp_stack_t	*sctps = sctp->sctp_sctps;
573 
574 	ASSERT(fp != NULL);
575 
576 	dprint(3, ("sctp_timer: faddr=%x:%x:%x:%x\n",
577 	    SCTP_PRINTADDR(fp->faddr)));
578 
579 	fp->timer_running = 0;
580 
581 	/* Check is we've reached the max for retries */
582 	if (sctp->sctp_state < SCTPS_ESTABLISHED) {
583 		if (fp->strikes >= sctp->sctp_max_init_rxt) {
584 			/* time to give up */
585 			BUMP_MIB(&sctps->sctps_mib, sctpAborted);
586 			BUMP_MIB(&sctps->sctps_mib, sctpTimRetransDrop);
587 			sctp_assoc_event(sctp, SCTP_CANT_STR_ASSOC, 0, NULL);
588 			sctp_clean_death(sctp, sctp->sctp_client_errno ?
589 			    sctp->sctp_client_errno : ETIMEDOUT);
590 			return;
591 		}
592 	} else if (sctp->sctp_state >= SCTPS_ESTABLISHED) {
593 		if (sctp->sctp_strikes >= sctp->sctp_pa_max_rxt) {
594 			/* time to give up */
595 			BUMP_MIB(&sctps->sctps_mib, sctpAborted);
596 			BUMP_MIB(&sctps->sctps_mib, sctpTimRetransDrop);
597 			sctp_assoc_event(sctp, SCTP_COMM_LOST, 0, NULL);
598 			sctp_clean_death(sctp, sctp->sctp_client_errno ?
599 			    sctp->sctp_client_errno : ETIMEDOUT);
600 			return;
601 		}
602 	}
603 
604 	if (fp->strikes >= fp->max_retr) {
605 		if (sctp_faddr_dead(sctp, fp, SCTP_FADDRS_DOWN) == -1) {
606 			return;
607 		}
608 	}
609 
610 	switch (sctp->sctp_state) {
611 	case SCTPS_SHUTDOWN_RECEIVED:
612 		(void) sctp_shutdown_received(sctp, NULL, B_FALSE, B_TRUE,
613 		    NULL);
614 
615 		/* FALLTHRU */
616 	case SCTPS_ESTABLISHED:
617 	case SCTPS_SHUTDOWN_PENDING:
618 		if (sctp->sctp_xmit_head == NULL &&
619 		    sctp->sctp_xmit_unsent == NULL) {
620 			/* Nothing to retransmit */
621 			if (sctp->sctp_state == SCTPS_SHUTDOWN_PENDING) {
622 				sctp_send_shutdown(sctp, 1);
623 			}
624 			return;
625 		}
626 
627 		BUMP_MIB(&sctps->sctps_mib, sctpTimRetrans);
628 
629 		sctp_rexmit(sctp, fp);
630 		/*
631 		 * sctp_rexmit() will increase the strikes and restart the
632 		 * timer, so return here.
633 		 */
634 		return;
635 	case SCTPS_COOKIE_WAIT:
636 		BUMP_LOCAL(sctp->sctp_T1expire);
637 rxmit_init:
638 		/* retransmit init */
639 		/*
640 		 * We don't take the conn hash lock here since the source
641 		 * address list won't be modified (it would have been done
642 		 * the first time around).
643 		 */
644 		mp = sctp_init_mp(sctp, fp);
645 		if (mp != NULL) {
646 			BUMP_MIB(&sctps->sctps_mib, sctpTimRetrans);
647 			(void) conn_ip_output(mp, fp->ixa);
648 			BUMP_LOCAL(sctp->sctp_opkts);
649 		}
650 		rto_max = sctp->sctp_rto_max_init;
651 		break;
652 	case SCTPS_COOKIE_ECHOED:
653 		BUMP_LOCAL(sctp->sctp_T1expire);
654 		if (sctp->sctp_cookie_mp == NULL) {
655 			sctp->sctp_state = SCTPS_COOKIE_WAIT;
656 			goto rxmit_init;
657 		}
658 		mp = dupmsg(sctp->sctp_cookie_mp);
659 		if (mp == NULL)
660 			break;
661 		(void) conn_ip_output(mp, fp->ixa);
662 		BUMP_LOCAL(sctp->sctp_opkts);
663 		BUMP_MIB(&sctps->sctps_mib, sctpTimRetrans);
664 		rto_max = sctp->sctp_rto_max_init;
665 		break;
666 	case SCTPS_SHUTDOWN_SENT:
667 		BUMP_LOCAL(sctp->sctp_T2expire);
668 		sctp_send_shutdown(sctp, 1);
669 		BUMP_MIB(&sctps->sctps_mib, sctpTimRetrans);
670 		break;
671 	case SCTPS_SHUTDOWN_ACK_SENT:
672 		/* We shouldn't have any more outstanding data */
673 		ASSERT(sctp->sctp_xmit_head == NULL);
674 		ASSERT(sctp->sctp_xmit_unsent == NULL);
675 
676 		BUMP_LOCAL(sctp->sctp_T2expire);
677 		(void) sctp_shutdown_received(sctp, NULL, B_FALSE, B_TRUE,
678 		    NULL);
679 		BUMP_MIB(&sctps->sctps_mib, sctpTimRetrans);
680 		break;
681 	default:
682 		ASSERT(0);
683 		break;
684 	}
685 
686 	fp->strikes++;
687 	sctp->sctp_strikes++;
688 	SCTP_CALC_RXT(sctp, fp, rto_max);
689 
690 	SCTP_FADDR_TIMER_RESTART(sctp, fp, fp->rto);
691 }
692 
693 /*
694  * RTO calculation. timesent and now are both in ms.
695  */
696 void
697 sctp_update_rtt(sctp_t *sctp, sctp_faddr_t *fp, clock_t delta)
698 {
699 	int rtt;
700 
701 	/* Calculate the RTT in ms */
702 	rtt = (int)delta;
703 	rtt = rtt > 0 ? rtt : 1;
704 
705 	dprint(5, ("sctp_update_rtt: fp = %p, rtt = %d\n", (void *)fp, rtt));
706 
707 	/* Is this the first RTT measurement? */
708 	if (fp->srtt == -1) {
709 		fp->srtt = rtt;
710 		fp->rttvar = rtt / 2;
711 		fp->rto = 3 * rtt; /* == rtt + 4 * rttvar ( == rtt / 2) */
712 	} else {
713 		int abs;
714 		/*
715 		 * Versions of the RTO equations that use fixed-point math.
716 		 * alpha and beta are NOT tunable in this implementation,
717 		 * and so are hard-coded in. alpha = 1/8, beta = 1/4.
718 		 */
719 		abs = fp->srtt - rtt;
720 		abs = abs >= 0 ? abs : -abs;
721 		fp->rttvar = (3 * fp->rttvar + abs) >> 2;
722 		fp->rttvar = fp->rttvar != 0 ? fp->rttvar : 1;
723 
724 		fp->srtt = (7 * fp->srtt + rtt) >> 3;
725 		fp->rto = fp->srtt + 4 * fp->rttvar;
726 	}
727 
728 	dprint(5, ("sctp_update_rtt: srtt = %d, rttvar = %d, rto = %d\n",
729 	    fp->srtt, fp->rttvar, fp->rto));
730 
731 	/* Bound the RTO by configured min and max values */
732 	if (fp->rto < sctp->sctp_rto_min) {
733 		fp->rto = sctp->sctp_rto_min;
734 	}
735 	if (fp->rto > sctp->sctp_rto_max) {
736 		fp->rto = sctp->sctp_rto_max;
737 	}
738 
739 	SCTP_MAX_RTO(sctp, fp);
740 	fp->rtt_updates++;
741 }
742 
743 void
744 sctp_free_faddr_timers(sctp_t *sctp)
745 {
746 	sctp_faddr_t *fp;
747 
748 	for (fp = sctp->sctp_faddrs; fp != NULL; fp = fp->next) {
749 		if (fp->timer_mp != NULL) {
750 			sctp_timer_free(fp->timer_mp);
751 			fp->timer_mp = NULL;
752 			fp->timer_running = 0;
753 		}
754 		if (fp->rc_timer_mp != NULL) {
755 			sctp_timer_free(fp->rc_timer_mp);
756 			fp->rc_timer_mp = NULL;
757 			fp->rc_timer_running = 0;
758 		}
759 	}
760 }
761 
762 void
763 sctp_stop_faddr_timers(sctp_t *sctp)
764 {
765 	sctp_faddr_t *fp;
766 
767 	for (fp = sctp->sctp_faddrs; fp != NULL; fp = fp->next) {
768 		SCTP_FADDR_TIMER_STOP(fp);
769 		SCTP_FADDR_RC_TIMER_STOP(fp);
770 	}
771 }
772 
773 void
774 sctp_process_timer(sctp_t *sctp)
775 {
776 	mblk_t *mp;
777 
778 	ASSERT(sctp->sctp_running);
779 	ASSERT(MUTEX_HELD(&sctp->sctp_lock));
780 	while ((mp = sctp->sctp_timer_mp) != NULL) {
781 		ASSERT(DB_TYPE(mp) == M_PCSIG);
782 		/*
783 		 * Since the timer mblk can be freed in sctp_timer_call(),
784 		 * we need to grab the b_cont before that.
785 		 */
786 		sctp->sctp_timer_mp = mp->b_cont;
787 		mp->b_cont = NULL;
788 		/*
789 		 * We have a reference on the sctp, the lock must be
790 		 * dropped to avoid deadlocks with functions potentially
791 		 * called in this context which in turn call untimeout().
792 		 */
793 		mutex_exit(&sctp->sctp_lock);
794 		sctp_timer_call(sctp, mp);
795 		mutex_enter(&sctp->sctp_lock);
796 	}
797 	SCTP_REFRELE(sctp);
798 }
799