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->next) {
437 		if (sctps->sctps_reclaim)
438 			pp_max_retr = MIN(sctp_pp_early_abort, fp->max_retr);
439 		else
440 			pp_max_retr = fp->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->state == SCTP_FADDRS_UNREACH) {
453 			sctp_get_dest(sctp, fp);
454 			if (fp->state == SCTP_FADDRS_UNREACH) {
455 				if (fp->hb_enabled &&
456 				    ++fp->strikes > pp_max_retr &&
457 				    sctp_faddr_dead(sctp, fp,
458 				    SCTP_FADDRS_DOWN) == -1) {
459 					/* Assoc is dead */
460 					return;
461 				}
462 				fp->hb_expiry = now + SET_HB_INTVL(fp);
463 				goto set_expiry;
464 			} else {
465 				/* Send a heartbeat immediately. */
466 				fp->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->hb_enabled && fp->state != SCTP_FADDRS_UNCONFIRMED) {
474 			continue;
475 		}
476 
477 		/*
478 		 * The heartbeat timer is expired.  If the address is dead,
479 		 * we still send heartbeat to it in case it becomes alive
480 		 * again.  But we will only send once in a while, calculated
481 		 * by SET_HB_INTVL().
482 		 *
483 		 * If the address is alive and there is a hearbeat pending,
484 		 * resend the heartbeat and start exponential backoff on the
485 		 * heartbeat timeout value.  If there is no heartbeat pending,
486 		 * just send out one.
487 		 */
488 		if (now >= fp->hb_expiry) {
489 			if (fp->hb_pending) {
490 				/*
491 				 * If an address is not confirmed, no need
492 				 * to bump the overall counter as it doesn't
493 				 * matter as we will not use it to send data
494 				 * and it should not affect the association.
495 				 */
496 				switch (fp->state) {
497 				case SCTP_FADDRS_ALIVE:
498 					sctp->sctp_strikes++;
499 					/* FALLTHRU */
500 				case SCTP_FADDRS_UNCONFIRMED:
501 					/*
502 					 * Retransmission implies that RTO
503 					 * is probably not correct.
504 					 */
505 					fp->rtt_updates = 0;
506 					fp->strikes++;
507 					if (fp->strikes > pp_max_retr) {
508 						if (sctp_faddr_dead(sctp, fp,
509 						    SCTP_FADDRS_DOWN) == -1) {
510 							/* Assoc is dead */
511 							return;
512 						}
513 						/*
514 						 * Addr is down; keep initial
515 						 * RTO
516 						 */
517 						fp->rto =
518 						    sctp->sctp_rto_initial;
519 						goto dead_addr;
520 					} else {
521 						SCTP_CALC_RXT(sctp, fp,
522 						    sctp->sctp_rto_max);
523 						fp->hb_expiry = now + fp->rto;
524 					}
525 					break;
526 				case SCTP_FADDRS_DOWN:
527 dead_addr:
528 					fp->hb_expiry = now + SET_HB_INTVL(fp);
529 					break;
530 				default:
531 					continue;
532 				}
533 			} else {
534 				/*
535 				 * If there is unack'ed data, no need to
536 				 * send a heart beat.
537 				 */
538 				if (fp->suna > 0) {
539 					fp->hb_expiry = now + SET_HB_INTVL(fp);
540 					goto set_expiry;
541 				} else {
542 					fp->hb_expiry = now + fp->rto;
543 				}
544 			}
545 			/*
546 			 * Note that the total number of heartbeat we can send
547 			 * out simultaneously is limited by sctp_maxburst.  If
548 			 * the limit is exceeded, we need to wait for the next
549 			 * timeout to send them.  This should only happen if
550 			 * there is unconfirmed address.  Note that hb_pending
551 			 * is set in sctp_send_heartbeat().  So if a heartbeat
552 			 * is not sent, it will not affect the state of the
553 			 * peer address.
554 			 */
555 			if (fp->state != SCTP_FADDRS_UNCONFIRMED || cnt-- > 0)
556 				sctp_send_heartbeat(sctp, fp);
557 		}
558 set_expiry:
559 		if (fp->hb_expiry < earliest_expiry || earliest_expiry == 0)
560 			earliest_expiry = fp->hb_expiry;
561 	}
562 	if (sctp->sctp_autoclose != 0) {
563 		int64_t expire;
564 
565 		expire = sctp->sctp_active + sctp->sctp_autoclose;
566 
567 		if (expire <= now) {
568 			dprint(3, ("sctp_heartbeat_timer: autoclosing\n"));
569 			sctp_send_shutdown(sctp, 0);
570 			return;
571 		}
572 		if (expire < earliest_expiry || earliest_expiry == 0)
573 			earliest_expiry = expire;
574 	}
575 
576 	earliest_expiry -= now;
577 	if (earliest_expiry < 0)
578 		earliest_expiry = 1;
579 	sctp_timer(sctp, sctp->sctp_heartbeat_mp, earliest_expiry);
580 }
581 
582 void
583 sctp_rexmit_timer(sctp_t *sctp, sctp_faddr_t *fp)
584 {
585 	mblk_t 		*mp;
586 	uint32_t	rto_max = sctp->sctp_rto_max;
587 	sctp_stack_t	*sctps = sctp->sctp_sctps;
588 	int		pp_max_retr, pa_max_retr;
589 
590 	ASSERT(fp != NULL);
591 
592 	dprint(3, ("sctp_timer: faddr=%x:%x:%x:%x\n",
593 	    SCTP_PRINTADDR(fp->faddr)));
594 
595 	fp->timer_running = 0;
596 
597 	if (!sctps->sctps_reclaim) {
598 		pp_max_retr = fp->max_retr;
599 		pa_max_retr = sctp->sctp_pa_max_rxt;
600 	} else {
601 		/* App may have set a very aggressive retransmission limit. */
602 		pp_max_retr = MIN(sctp_pp_early_abort, fp->max_retr);
603 		pa_max_retr = MIN(sctp_pa_early_abort, sctp->sctp_pa_max_rxt);
604 	}
605 
606 	/* Check is we've reached the max for retries */
607 	if (sctp->sctp_state < SCTPS_ESTABLISHED) {
608 		if (fp->strikes >= sctp->sctp_max_init_rxt) {
609 			/* time to give up */
610 			SCTPS_BUMP_MIB(sctps, sctpAborted);
611 			SCTPS_BUMP_MIB(sctps, sctpTimRetransDrop);
612 			sctp_assoc_event(sctp, SCTP_CANT_STR_ASSOC, 0, NULL);
613 			sctp_clean_death(sctp, sctp->sctp_client_errno ?
614 			    sctp->sctp_client_errno : ETIMEDOUT);
615 			return;
616 		}
617 	} else if (sctp->sctp_state >= SCTPS_ESTABLISHED) {
618 		if (sctp->sctp_strikes >= pa_max_retr) {
619 			/* time to give up */
620 			SCTPS_BUMP_MIB(sctps, sctpAborted);
621 			SCTPS_BUMP_MIB(sctps, sctpTimRetransDrop);
622 			sctp_assoc_event(sctp, SCTP_COMM_LOST, 0, NULL);
623 			sctp_clean_death(sctp, sctp->sctp_client_errno ?
624 			    sctp->sctp_client_errno : ETIMEDOUT);
625 			return;
626 		}
627 	}
628 
629 	if (fp->strikes >= pp_max_retr) {
630 		if (sctp_faddr_dead(sctp, fp, SCTP_FADDRS_DOWN) == -1) {
631 			return;
632 		}
633 	}
634 
635 	switch (sctp->sctp_state) {
636 	case SCTPS_SHUTDOWN_RECEIVED:
637 		(void) sctp_shutdown_received(sctp, NULL, B_FALSE, B_TRUE,
638 		    NULL);
639 
640 		/* FALLTHRU */
641 	case SCTPS_ESTABLISHED:
642 	case SCTPS_SHUTDOWN_PENDING:
643 		if (sctp->sctp_xmit_head == NULL &&
644 		    sctp->sctp_xmit_unsent == NULL) {
645 			/* Nothing to retransmit */
646 			if (sctp->sctp_state == SCTPS_SHUTDOWN_PENDING) {
647 				sctp_send_shutdown(sctp, 1);
648 			}
649 			return;
650 		}
651 
652 		SCTPS_BUMP_MIB(sctps, sctpTimRetrans);
653 
654 		sctp_rexmit(sctp, fp);
655 		/*
656 		 * sctp_rexmit() will increase the strikes and restart the
657 		 * timer, so return here.
658 		 */
659 		return;
660 	case SCTPS_COOKIE_WAIT:
661 		BUMP_LOCAL(sctp->sctp_T1expire);
662 rxmit_init:
663 		/* retransmit init */
664 		/*
665 		 * We don't take the conn hash lock here since the source
666 		 * address list won't be modified (it would have been done
667 		 * the first time around).
668 		 */
669 		mp = sctp_init_mp(sctp, fp);
670 		if (mp != NULL) {
671 			SCTPS_BUMP_MIB(sctps, sctpTimRetrans);
672 			(void) conn_ip_output(mp, fp->ixa);
673 			BUMP_LOCAL(sctp->sctp_opkts);
674 		}
675 		rto_max = sctp->sctp_rto_max_init;
676 		break;
677 	case SCTPS_COOKIE_ECHOED:
678 		BUMP_LOCAL(sctp->sctp_T1expire);
679 		if (sctp->sctp_cookie_mp == NULL) {
680 			sctp->sctp_state = SCTPS_COOKIE_WAIT;
681 			goto rxmit_init;
682 		}
683 		mp = dupmsg(sctp->sctp_cookie_mp);
684 		if (mp == NULL)
685 			break;
686 		(void) conn_ip_output(mp, fp->ixa);
687 		BUMP_LOCAL(sctp->sctp_opkts);
688 		SCTPS_BUMP_MIB(sctps, sctpTimRetrans);
689 		rto_max = sctp->sctp_rto_max_init;
690 		break;
691 	case SCTPS_SHUTDOWN_SENT:
692 		BUMP_LOCAL(sctp->sctp_T2expire);
693 		sctp_send_shutdown(sctp, 1);
694 		SCTPS_BUMP_MIB(sctps, sctpTimRetrans);
695 		break;
696 	case SCTPS_SHUTDOWN_ACK_SENT:
697 		/* We shouldn't have any more outstanding data */
698 		ASSERT(sctp->sctp_xmit_head == NULL);
699 		ASSERT(sctp->sctp_xmit_unsent == NULL);
700 
701 		BUMP_LOCAL(sctp->sctp_T2expire);
702 		(void) sctp_shutdown_received(sctp, NULL, B_FALSE, B_TRUE,
703 		    NULL);
704 		SCTPS_BUMP_MIB(sctps, sctpTimRetrans);
705 		break;
706 	default:
707 		ASSERT(0);
708 		break;
709 	}
710 
711 	fp->strikes++;
712 	sctp->sctp_strikes++;
713 	SCTP_CALC_RXT(sctp, fp, rto_max);
714 
715 	SCTP_FADDR_TIMER_RESTART(sctp, fp, fp->rto);
716 }
717 
718 /*
719  * RTO calculation. timesent and now are both in ms.
720  */
721 void
722 sctp_update_rtt(sctp_t *sctp, sctp_faddr_t *fp, clock_t delta)
723 {
724 	int rtt;
725 
726 	/* Calculate the RTT in ms */
727 	rtt = (int)delta;
728 	rtt = rtt > 0 ? rtt : 1;
729 
730 	dprint(5, ("sctp_update_rtt: fp = %p, rtt = %d\n", (void *)fp, rtt));
731 
732 	/* Is this the first RTT measurement? */
733 	if (fp->srtt == -1) {
734 		fp->srtt = rtt;
735 		fp->rttvar = rtt / 2;
736 		fp->rto = 3 * rtt; /* == rtt + 4 * rttvar ( == rtt / 2) */
737 	} else {
738 		int abs;
739 		/*
740 		 * Versions of the RTO equations that use fixed-point math.
741 		 * alpha and beta are NOT tunable in this implementation,
742 		 * and so are hard-coded in. alpha = 1/8, beta = 1/4.
743 		 */
744 		abs = fp->srtt - rtt;
745 		abs = abs >= 0 ? abs : -abs;
746 		fp->rttvar = (3 * fp->rttvar + abs) >> 2;
747 		fp->rttvar = fp->rttvar != 0 ? fp->rttvar : 1;
748 
749 		fp->srtt = (7 * fp->srtt + rtt) >> 3;
750 		fp->rto = fp->srtt + 4 * fp->rttvar;
751 	}
752 
753 	dprint(5, ("sctp_update_rtt: srtt = %d, rttvar = %d, rto = %d\n",
754 	    fp->srtt, fp->rttvar, fp->rto));
755 
756 	/* Bound the RTO by configured min and max values */
757 	if (fp->rto < sctp->sctp_rto_min) {
758 		fp->rto = sctp->sctp_rto_min;
759 	}
760 	if (fp->rto > sctp->sctp_rto_max) {
761 		fp->rto = sctp->sctp_rto_max;
762 	}
763 
764 	SCTP_MAX_RTO(sctp, fp);
765 	fp->rtt_updates++;
766 }
767 
768 void
769 sctp_free_faddr_timers(sctp_t *sctp)
770 {
771 	sctp_faddr_t *fp;
772 
773 	for (fp = sctp->sctp_faddrs; fp != NULL; fp = fp->next) {
774 		if (fp->timer_mp != NULL) {
775 			sctp_timer_free(fp->timer_mp);
776 			fp->timer_mp = NULL;
777 			fp->timer_running = 0;
778 		}
779 		if (fp->rc_timer_mp != NULL) {
780 			sctp_timer_free(fp->rc_timer_mp);
781 			fp->rc_timer_mp = NULL;
782 			fp->rc_timer_running = 0;
783 		}
784 	}
785 }
786 
787 void
788 sctp_stop_faddr_timers(sctp_t *sctp)
789 {
790 	sctp_faddr_t *fp;
791 
792 	for (fp = sctp->sctp_faddrs; fp != NULL; fp = fp->next) {
793 		SCTP_FADDR_TIMER_STOP(fp);
794 		SCTP_FADDR_RC_TIMER_STOP(fp);
795 	}
796 }
797 
798 void
799 sctp_process_timer(sctp_t *sctp)
800 {
801 	mblk_t *mp;
802 
803 	ASSERT(sctp->sctp_running);
804 	ASSERT(MUTEX_HELD(&sctp->sctp_lock));
805 	while ((mp = sctp->sctp_timer_mp) != NULL) {
806 		ASSERT(DB_TYPE(mp) == M_PCSIG);
807 		/*
808 		 * Since the timer mblk can be freed in sctp_timer_call(),
809 		 * we need to grab the b_cont before that.
810 		 */
811 		sctp->sctp_timer_mp = mp->b_cont;
812 		mp->b_cont = NULL;
813 		/*
814 		 * We have a reference on the sctp, the lock must be
815 		 * dropped to avoid deadlocks with functions potentially
816 		 * called in this context which in turn call untimeout().
817 		 */
818 		mutex_exit(&sctp->sctp_lock);
819 		sctp_timer_call(sctp, mp);
820 		mutex_enter(&sctp->sctp_lock);
821 	}
822 	SCTP_REFRELE(sctp);
823 }
824