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