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