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