xref: /freebsd/sys/netinet/sctp_timer.c (revision 721351876cd4d3a8a700f62d2061331fa951a488)
1 /*-
2  * Copyright (c) 2001-2007, by Cisco Systems, Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * a) Redistributions of source code must retain the above copyright notice,
8  *   this list of conditions and the following disclaimer.
9  *
10  * b) Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in
12  *   the documentation and/or other materials provided with the distribution.
13  *
14  * c) Neither the name of Cisco Systems, Inc. nor the names of its
15  *    contributors may be used to endorse or promote products derived
16  *    from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
28  * THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 /* $KAME: sctp_timer.c,v 1.29 2005/03/06 16:04:18 itojun Exp $	 */
32 
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
35 
36 #define _IP_VHL
37 #include <netinet/sctp_os.h>
38 #include <netinet/sctp_pcb.h>
39 #ifdef INET6
40 #include <netinet6/sctp6_var.h>
41 #endif
42 #include <netinet/sctp_var.h>
43 #include <netinet/sctp_sysctl.h>
44 #include <netinet/sctp_timer.h>
45 #include <netinet/sctputil.h>
46 #include <netinet/sctp_output.h>
47 #include <netinet/sctp_header.h>
48 #include <netinet/sctp_indata.h>
49 #include <netinet/sctp_asconf.h>
50 #include <netinet/sctp_input.h>
51 #include <netinet/sctp.h>
52 #include <netinet/sctp_uio.h>
53 
54 
55 
56 void
57 sctp_early_fr_timer(struct sctp_inpcb *inp,
58     struct sctp_tcb *stcb,
59     struct sctp_nets *net)
60 {
61 	struct sctp_tmit_chunk *chk, *tp2;
62 	struct timeval now, min_wait, tv;
63 	unsigned int cur_rtt, cnt = 0, cnt_resend = 0;
64 
65 	/* an early FR is occuring. */
66 	(void)SCTP_GETTIME_TIMEVAL(&now);
67 	/* get cur rto in micro-seconds */
68 	if (net->lastsa == 0) {
69 		/* Hmm no rtt estimate yet? */
70 		cur_rtt = stcb->asoc.initial_rto >> 2;
71 	} else {
72 
73 		cur_rtt = ((net->lastsa >> 2) + net->lastsv) >> 1;
74 	}
75 	if (cur_rtt < SCTP_BASE_SYSCTL(sctp_early_fr_msec)) {
76 		cur_rtt = SCTP_BASE_SYSCTL(sctp_early_fr_msec);
77 	}
78 	cur_rtt *= 1000;
79 	tv.tv_sec = cur_rtt / 1000000;
80 	tv.tv_usec = cur_rtt % 1000000;
81 	min_wait = now;
82 	timevalsub(&min_wait, &tv);
83 	if (min_wait.tv_sec < 0 || min_wait.tv_usec < 0) {
84 		/*
85 		 * if we hit here, we don't have enough seconds on the clock
86 		 * to account for the RTO. We just let the lower seconds be
87 		 * the bounds and don't worry about it. This may mean we
88 		 * will mark a lot more than we should.
89 		 */
90 		min_wait.tv_sec = min_wait.tv_usec = 0;
91 	}
92 	chk = TAILQ_LAST(&stcb->asoc.sent_queue, sctpchunk_listhead);
93 	for (; chk != NULL; chk = tp2) {
94 		tp2 = TAILQ_PREV(chk, sctpchunk_listhead, sctp_next);
95 		if (chk->whoTo != net) {
96 			continue;
97 		}
98 		if (chk->sent == SCTP_DATAGRAM_RESEND)
99 			cnt_resend++;
100 		else if ((chk->sent > SCTP_DATAGRAM_UNSENT) &&
101 		    (chk->sent < SCTP_DATAGRAM_RESEND)) {
102 			/* pending, may need retran */
103 			if (chk->sent_rcv_time.tv_sec > min_wait.tv_sec) {
104 				/*
105 				 * we have reached a chunk that was sent
106 				 * some seconds past our min.. forget it we
107 				 * will find no more to send.
108 				 */
109 				continue;
110 			} else if (chk->sent_rcv_time.tv_sec == min_wait.tv_sec) {
111 				/*
112 				 * we must look at the micro seconds to
113 				 * know.
114 				 */
115 				if (chk->sent_rcv_time.tv_usec >= min_wait.tv_usec) {
116 					/*
117 					 * ok it was sent after our boundary
118 					 * time.
119 					 */
120 					continue;
121 				}
122 			}
123 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_EARLYFR_LOGGING_ENABLE) {
124 				sctp_log_fr(chk->rec.data.TSN_seq, chk->snd_count,
125 				    4, SCTP_FR_MARKED_EARLY);
126 			}
127 			SCTP_STAT_INCR(sctps_earlyfrmrkretrans);
128 			chk->sent = SCTP_DATAGRAM_RESEND;
129 			sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
130 			/* double book size since we are doing an early FR */
131 			chk->book_size_scale++;
132 			cnt += chk->send_size;
133 			if ((cnt + net->flight_size) > net->cwnd) {
134 				/* Mark all we could possibly resend */
135 				break;
136 			}
137 		}
138 	}
139 	if (cnt) {
140 		/*
141 		 * JRS - Use the congestion control given in the congestion
142 		 * control module
143 		 */
144 		stcb->asoc.cc_functions.sctp_cwnd_update_after_fr_timer(inp, stcb, net);
145 	} else if (cnt_resend) {
146 		sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_EARLY_FR_TMR, SCTP_SO_NOT_LOCKED);
147 	}
148 	/* Restart it? */
149 	if (net->flight_size < net->cwnd) {
150 		SCTP_STAT_INCR(sctps_earlyfrstrtmr);
151 		sctp_timer_start(SCTP_TIMER_TYPE_EARLYFR, stcb->sctp_ep, stcb, net);
152 	}
153 }
154 
155 void
156 sctp_audit_retranmission_queue(struct sctp_association *asoc)
157 {
158 	struct sctp_tmit_chunk *chk;
159 
160 	SCTPDBG(SCTP_DEBUG_TIMER4, "Audit invoked on send queue cnt:%d onqueue:%d\n",
161 	    asoc->sent_queue_retran_cnt,
162 	    asoc->sent_queue_cnt);
163 	asoc->sent_queue_retran_cnt = 0;
164 	asoc->sent_queue_cnt = 0;
165 	TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
166 		if (chk->sent == SCTP_DATAGRAM_RESEND) {
167 			sctp_ucount_incr(asoc->sent_queue_retran_cnt);
168 		}
169 		asoc->sent_queue_cnt++;
170 	}
171 	TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
172 		if (chk->sent == SCTP_DATAGRAM_RESEND) {
173 			sctp_ucount_incr(asoc->sent_queue_retran_cnt);
174 		}
175 	}
176 	TAILQ_FOREACH(chk, &asoc->asconf_send_queue, sctp_next) {
177 		if (chk->sent == SCTP_DATAGRAM_RESEND) {
178 			sctp_ucount_incr(asoc->sent_queue_retran_cnt);
179 		}
180 	}
181 	SCTPDBG(SCTP_DEBUG_TIMER4, "Audit completes retran:%d onqueue:%d\n",
182 	    asoc->sent_queue_retran_cnt,
183 	    asoc->sent_queue_cnt);
184 }
185 
186 int
187 sctp_threshold_management(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
188     struct sctp_nets *net, uint16_t threshold)
189 {
190 	if (net) {
191 		net->error_count++;
192 		SCTPDBG(SCTP_DEBUG_TIMER4, "Error count for %p now %d thresh:%d\n",
193 		    net, net->error_count,
194 		    net->failure_threshold);
195 		if (net->error_count > net->failure_threshold) {
196 			/* We had a threshold failure */
197 			if (net->dest_state & SCTP_ADDR_REACHABLE) {
198 				net->dest_state &= ~SCTP_ADDR_REACHABLE;
199 				net->dest_state |= SCTP_ADDR_NOT_REACHABLE;
200 				net->dest_state &= ~SCTP_ADDR_REQ_PRIMARY;
201 				if (net == stcb->asoc.primary_destination) {
202 					net->dest_state |= SCTP_ADDR_WAS_PRIMARY;
203 				}
204 				/*
205 				 * JRS 5/14/07 - If a destination is
206 				 * unreachable, the PF bit is turned off.
207 				 * This allows an unambiguous use of the PF
208 				 * bit for destinations that are reachable
209 				 * but potentially failed. If the
210 				 * destination is set to the unreachable
211 				 * state, also set the destination to the PF
212 				 * state.
213 				 */
214 				/*
215 				 * Add debug message here if destination is
216 				 * not in PF state.
217 				 */
218 				/* Stop any running T3 timers here? */
219 				if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) && SCTP_BASE_SYSCTL(sctp_cmt_pf)) {
220 					net->dest_state &= ~SCTP_ADDR_PF;
221 					SCTPDBG(SCTP_DEBUG_TIMER4, "Destination %p moved from PF to unreachable.\n",
222 					    net);
223 				}
224 				sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN,
225 				    stcb,
226 				    SCTP_FAILED_THRESHOLD,
227 				    (void *)net, SCTP_SO_NOT_LOCKED);
228 			}
229 		}
230 		/*********HOLD THIS COMMENT FOR PATCH OF ALTERNATE
231 		 *********ROUTING CODE
232 		 */
233 		/*********HOLD THIS COMMENT FOR END OF PATCH OF ALTERNATE
234 		 *********ROUTING CODE
235 		 */
236 	}
237 	if (stcb == NULL)
238 		return (0);
239 
240 	if (net) {
241 		if ((net->dest_state & SCTP_ADDR_UNCONFIRMED) == 0) {
242 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
243 				sctp_misc_ints(SCTP_THRESHOLD_INCR,
244 				    stcb->asoc.overall_error_count,
245 				    (stcb->asoc.overall_error_count + 1),
246 				    SCTP_FROM_SCTP_TIMER,
247 				    __LINE__);
248 			}
249 			stcb->asoc.overall_error_count++;
250 		}
251 	} else {
252 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
253 			sctp_misc_ints(SCTP_THRESHOLD_INCR,
254 			    stcb->asoc.overall_error_count,
255 			    (stcb->asoc.overall_error_count + 1),
256 			    SCTP_FROM_SCTP_TIMER,
257 			    __LINE__);
258 		}
259 		stcb->asoc.overall_error_count++;
260 	}
261 	SCTPDBG(SCTP_DEBUG_TIMER4, "Overall error count for %p now %d thresh:%u state:%x\n",
262 	    &stcb->asoc, stcb->asoc.overall_error_count,
263 	    (uint32_t) threshold,
264 	    ((net == NULL) ? (uint32_t) 0 : (uint32_t) net->dest_state));
265 	/*
266 	 * We specifically do not do >= to give the assoc one more change
267 	 * before we fail it.
268 	 */
269 	if (stcb->asoc.overall_error_count > threshold) {
270 		/* Abort notification sends a ULP notify */
271 		struct mbuf *oper;
272 
273 		oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
274 		    0, M_DONTWAIT, 1, MT_DATA);
275 		if (oper) {
276 			struct sctp_paramhdr *ph;
277 			uint32_t *ippp;
278 
279 			SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
280 			    sizeof(uint32_t);
281 			ph = mtod(oper, struct sctp_paramhdr *);
282 			ph->param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
283 			ph->param_length = htons(SCTP_BUF_LEN(oper));
284 			ippp = (uint32_t *) (ph + 1);
285 			*ippp = htonl(SCTP_FROM_SCTP_TIMER + SCTP_LOC_1);
286 		}
287 		inp->last_abort_code = SCTP_FROM_SCTP_TIMER + SCTP_LOC_1;
288 		printf("Aborting association threshold:%d overall error count:%d\n",
289 		    threshold,
290 		    stcb->asoc.overall_error_count);
291 		sctp_abort_an_association(inp, stcb, SCTP_FAILED_THRESHOLD, oper, SCTP_SO_NOT_LOCKED);
292 		return (1);
293 	}
294 	return (0);
295 }
296 
297 struct sctp_nets *
298 sctp_find_alternate_net(struct sctp_tcb *stcb,
299     struct sctp_nets *net,
300     int mode)
301 {
302 	/* Find and return an alternate network if possible */
303 	struct sctp_nets *alt, *mnet, *min_errors_net = NULL, *max_cwnd_net = NULL;
304 	int once;
305 
306 	/* JRS 5/14/07 - Initialize min_errors to an impossible value. */
307 	int min_errors = -1;
308 	uint32_t max_cwnd = 0;
309 
310 	if (stcb->asoc.numnets == 1) {
311 		/* No others but net */
312 		return (TAILQ_FIRST(&stcb->asoc.nets));
313 	}
314 	/*
315 	 * JRS 5/14/07 - If mode is set to 2, use the CMT PF find alternate
316 	 * net algorithm. This algorithm chooses the active destination (not
317 	 * in PF state) with the largest cwnd value. If all destinations are
318 	 * in PF state, unreachable, or unconfirmed, choose the desination
319 	 * that is in PF state with the lowest error count. In case of a
320 	 * tie, choose the destination that was most recently active.
321 	 */
322 	if (mode == 2) {
323 		TAILQ_FOREACH(mnet, &stcb->asoc.nets, sctp_next) {
324 			/*
325 			 * JRS 5/14/07 - If the destination is unreachable
326 			 * or unconfirmed, skip it.
327 			 */
328 			if (((mnet->dest_state & SCTP_ADDR_REACHABLE) != SCTP_ADDR_REACHABLE) ||
329 			    (mnet->dest_state & SCTP_ADDR_UNCONFIRMED)) {
330 				continue;
331 			}
332 			/*
333 			 * JRS 5/14/07 -  If the destination is reachable
334 			 * but in PF state, compare the error count of the
335 			 * destination to the minimum error count seen thus
336 			 * far. Store the destination with the lower error
337 			 * count.  If the error counts are equal, store the
338 			 * destination that was most recently active.
339 			 */
340 			if (mnet->dest_state & SCTP_ADDR_PF) {
341 				/*
342 				 * JRS 5/14/07 - If the destination under
343 				 * consideration is the current destination,
344 				 * work as if the error count is one higher.
345 				 * The actual error count will not be
346 				 * incremented until later in the t3
347 				 * handler.
348 				 */
349 				if (mnet == net) {
350 					if (min_errors == -1) {
351 						min_errors = mnet->error_count + 1;
352 						min_errors_net = mnet;
353 					} else if (mnet->error_count + 1 < min_errors) {
354 						min_errors = mnet->error_count + 1;
355 						min_errors_net = mnet;
356 					} else if (mnet->error_count + 1 == min_errors
357 					    && mnet->last_active > min_errors_net->last_active) {
358 						min_errors_net = mnet;
359 						min_errors = mnet->error_count + 1;
360 					}
361 					continue;
362 				} else {
363 					if (min_errors == -1) {
364 						min_errors = mnet->error_count;
365 						min_errors_net = mnet;
366 					} else if (mnet->error_count < min_errors) {
367 						min_errors = mnet->error_count;
368 						min_errors_net = mnet;
369 					} else if (mnet->error_count == min_errors
370 					    && mnet->last_active > min_errors_net->last_active) {
371 						min_errors_net = mnet;
372 						min_errors = mnet->error_count;
373 					}
374 					continue;
375 				}
376 			}
377 			/*
378 			 * JRS 5/14/07 - If the destination is reachable and
379 			 * not in PF state, compare the cwnd of the
380 			 * destination to the highest cwnd seen thus far.
381 			 * Store the destination with the higher cwnd value.
382 			 * If the cwnd values are equal, randomly choose one
383 			 * of the two destinations.
384 			 */
385 			if (max_cwnd < mnet->cwnd) {
386 				max_cwnd_net = mnet;
387 				max_cwnd = mnet->cwnd;
388 			} else if (max_cwnd == mnet->cwnd) {
389 				uint32_t rndval;
390 				uint8_t this_random;
391 
392 				if (stcb->asoc.hb_random_idx > 3) {
393 					rndval = sctp_select_initial_TSN(&stcb->sctp_ep->sctp_ep);
394 					memcpy(stcb->asoc.hb_random_values, &rndval, sizeof(stcb->asoc.hb_random_values));
395 					this_random = stcb->asoc.hb_random_values[0];
396 					stcb->asoc.hb_random_idx++;
397 					stcb->asoc.hb_ect_randombit = 0;
398 				} else {
399 					this_random = stcb->asoc.hb_random_values[stcb->asoc.hb_random_idx];
400 					stcb->asoc.hb_random_idx++;
401 					stcb->asoc.hb_ect_randombit = 0;
402 				}
403 				if (this_random % 2 == 1) {
404 					max_cwnd_net = mnet;
405 					max_cwnd = mnet->cwnd;
406 					//Useless ?
407 				}
408 			}
409 		}
410 		/*
411 		 * JRS 5/14/07 - After all destination have been considered
412 		 * as alternates, check to see if there was some active
413 		 * destination (not in PF state).  If not, check to see if
414 		 * there was some PF destination with the minimum number of
415 		 * errors.  If not, return the original destination.  If
416 		 * there is a min_errors_net, remove the PF flag from that
417 		 * destination, set the cwnd to one or two MTUs, and return
418 		 * the destination as an alt. If there was some active
419 		 * destination with a highest cwnd, return the destination
420 		 * as an alt.
421 		 */
422 		if (max_cwnd_net == NULL) {
423 			if (min_errors_net == NULL) {
424 				return (net);
425 			}
426 			min_errors_net->dest_state &= ~SCTP_ADDR_PF;
427 			min_errors_net->cwnd = min_errors_net->mtu * SCTP_BASE_SYSCTL(sctp_cmt_pf);
428 			if (SCTP_OS_TIMER_PENDING(&min_errors_net->rxt_timer.timer)) {
429 				sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
430 				    stcb, min_errors_net,
431 				    SCTP_FROM_SCTP_TIMER + SCTP_LOC_2);
432 			}
433 			SCTPDBG(SCTP_DEBUG_TIMER4, "Destination %p moved from PF to active with %d errors.\n",
434 			    min_errors_net, min_errors_net->error_count);
435 			return (min_errors_net);
436 		} else {
437 			return (max_cwnd_net);
438 		}
439 	}
440 	/*
441 	 * JRS 5/14/07 - If mode is set to 1, use the CMT policy for
442 	 * choosing an alternate net.
443 	 */
444 	else if (mode == 1) {
445 		TAILQ_FOREACH(mnet, &stcb->asoc.nets, sctp_next) {
446 			if (((mnet->dest_state & SCTP_ADDR_REACHABLE) != SCTP_ADDR_REACHABLE) ||
447 			    (mnet->dest_state & SCTP_ADDR_UNCONFIRMED)
448 			    ) {
449 				/*
450 				 * will skip ones that are not-reachable or
451 				 * unconfirmed
452 				 */
453 				continue;
454 			}
455 			if (max_cwnd < mnet->cwnd) {
456 				max_cwnd_net = mnet;
457 				max_cwnd = mnet->cwnd;
458 			} else if (max_cwnd == mnet->cwnd) {
459 				uint32_t rndval;
460 				uint8_t this_random;
461 
462 				if (stcb->asoc.hb_random_idx > 3) {
463 					rndval = sctp_select_initial_TSN(&stcb->sctp_ep->sctp_ep);
464 					memcpy(stcb->asoc.hb_random_values, &rndval,
465 					    sizeof(stcb->asoc.hb_random_values));
466 					this_random = stcb->asoc.hb_random_values[0];
467 					stcb->asoc.hb_random_idx = 0;
468 					stcb->asoc.hb_ect_randombit = 0;
469 				} else {
470 					this_random = stcb->asoc.hb_random_values[stcb->asoc.hb_random_idx];
471 					stcb->asoc.hb_random_idx++;
472 					stcb->asoc.hb_ect_randombit = 0;
473 				}
474 				if (this_random % 2) {
475 					max_cwnd_net = mnet;
476 					max_cwnd = mnet->cwnd;
477 				}
478 			}
479 		}
480 		if (max_cwnd_net) {
481 			return (max_cwnd_net);
482 		}
483 	}
484 	mnet = net;
485 	once = 0;
486 
487 	if (mnet == NULL) {
488 		mnet = TAILQ_FIRST(&stcb->asoc.nets);
489 	}
490 	do {
491 		alt = TAILQ_NEXT(mnet, sctp_next);
492 		if (alt == NULL) {
493 			once++;
494 			if (once > 1) {
495 				break;
496 			}
497 			alt = TAILQ_FIRST(&stcb->asoc.nets);
498 		}
499 		if (alt->ro.ro_rt == NULL) {
500 			if (alt->ro._s_addr) {
501 				sctp_free_ifa(alt->ro._s_addr);
502 				alt->ro._s_addr = NULL;
503 			}
504 			alt->src_addr_selected = 0;
505 		}
506 		if (
507 		    ((alt->dest_state & SCTP_ADDR_REACHABLE) == SCTP_ADDR_REACHABLE) &&
508 		    (alt->ro.ro_rt != NULL) &&
509 		/* sa_ignore NO_NULL_CHK */
510 		    (!(alt->dest_state & SCTP_ADDR_UNCONFIRMED))
511 		    ) {
512 			/* Found a reachable address */
513 			break;
514 		}
515 		mnet = alt;
516 	} while (alt != NULL);
517 
518 	if (alt == NULL) {
519 		/* Case where NO insv network exists (dormant state) */
520 		/* we rotate destinations */
521 		once = 0;
522 		mnet = net;
523 		do {
524 			alt = TAILQ_NEXT(mnet, sctp_next);
525 			if (alt == NULL) {
526 				once++;
527 				if (once > 1) {
528 					break;
529 				}
530 				alt = TAILQ_FIRST(&stcb->asoc.nets);
531 			}
532 			/* sa_ignore NO_NULL_CHK */
533 			if ((!(alt->dest_state & SCTP_ADDR_UNCONFIRMED)) &&
534 			    (alt != net)) {
535 				/* Found an alternate address */
536 				break;
537 			}
538 			mnet = alt;
539 		} while (alt != NULL);
540 	}
541 	if (alt == NULL) {
542 		return (net);
543 	}
544 	return (alt);
545 }
546 
547 
548 
549 static void
550 sctp_backoff_on_timeout(struct sctp_tcb *stcb,
551     struct sctp_nets *net,
552     int win_probe,
553     int num_marked)
554 {
555 	if (net->RTO == 0) {
556 		net->RTO = stcb->asoc.minrto;
557 	}
558 	net->RTO <<= 1;
559 	if (net->RTO > stcb->asoc.maxrto) {
560 		net->RTO = stcb->asoc.maxrto;
561 	}
562 	if ((win_probe == 0) && num_marked) {
563 		/* We don't apply penalty to window probe scenarios */
564 		/* JRS - Use the congestion control given in the CC module */
565 		stcb->asoc.cc_functions.sctp_cwnd_update_after_timeout(stcb, net);
566 	}
567 }
568 
569 static int
570 sctp_mark_all_for_resend(struct sctp_tcb *stcb,
571     struct sctp_nets *net,
572     struct sctp_nets *alt,
573     int window_probe,
574     int *num_marked)
575 {
576 
577 	/*
578 	 * Mark all chunks (well not all) that were sent to *net for
579 	 * retransmission. Move them to alt for there destination as well...
580 	 * We only mark chunks that have been outstanding long enough to
581 	 * have received feed-back.
582 	 */
583 	struct sctp_tmit_chunk *chk, *tp2, *could_be_sent = NULL;
584 	struct sctp_nets *lnets;
585 	struct timeval now, min_wait, tv;
586 	int cur_rtt;
587 	int audit_tf, num_mk, fir;
588 	unsigned int cnt_mk;
589 	uint32_t orig_flight, orig_tf;
590 	uint32_t tsnlast, tsnfirst;
591 
592 
593 	/* none in flight now */
594 	audit_tf = 0;
595 	fir = 0;
596 	/*
597 	 * figure out how long a data chunk must be pending before we can
598 	 * mark it ..
599 	 */
600 	(void)SCTP_GETTIME_TIMEVAL(&now);
601 	/* get cur rto in micro-seconds */
602 	cur_rtt = (((net->lastsa >> 2) + net->lastsv) >> 1);
603 	cur_rtt *= 1000;
604 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & (SCTP_EARLYFR_LOGGING_ENABLE | SCTP_FR_LOGGING_ENABLE)) {
605 		sctp_log_fr(cur_rtt,
606 		    stcb->asoc.peers_rwnd,
607 		    window_probe,
608 		    SCTP_FR_T3_MARK_TIME);
609 		sctp_log_fr(net->flight_size,
610 		    SCTP_OS_TIMER_PENDING(&net->fr_timer.timer),
611 		    SCTP_OS_TIMER_ACTIVE(&net->fr_timer.timer),
612 		    SCTP_FR_CWND_REPORT);
613 		sctp_log_fr(net->flight_size, net->cwnd, stcb->asoc.total_flight, SCTP_FR_CWND_REPORT);
614 	}
615 	tv.tv_sec = cur_rtt / 1000000;
616 	tv.tv_usec = cur_rtt % 1000000;
617 	min_wait = now;
618 	timevalsub(&min_wait, &tv);
619 	if (min_wait.tv_sec < 0 || min_wait.tv_usec < 0) {
620 		/*
621 		 * if we hit here, we don't have enough seconds on the clock
622 		 * to account for the RTO. We just let the lower seconds be
623 		 * the bounds and don't worry about it. This may mean we
624 		 * will mark a lot more than we should.
625 		 */
626 		min_wait.tv_sec = min_wait.tv_usec = 0;
627 	}
628 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & (SCTP_EARLYFR_LOGGING_ENABLE | SCTP_FR_LOGGING_ENABLE)) {
629 		sctp_log_fr(cur_rtt, now.tv_sec, now.tv_usec, SCTP_FR_T3_MARK_TIME);
630 		sctp_log_fr(0, min_wait.tv_sec, min_wait.tv_usec, SCTP_FR_T3_MARK_TIME);
631 	}
632 	/*
633 	 * Our rwnd will be incorrect here since we are not adding back the
634 	 * cnt * mbuf but we will fix that down below.
635 	 */
636 	orig_flight = net->flight_size;
637 	orig_tf = stcb->asoc.total_flight;
638 
639 	net->fast_retran_ip = 0;
640 	/* Now on to each chunk */
641 	num_mk = cnt_mk = 0;
642 	tsnfirst = tsnlast = 0;
643 	chk = TAILQ_FIRST(&stcb->asoc.sent_queue);
644 	for (; chk != NULL; chk = tp2) {
645 		tp2 = TAILQ_NEXT(chk, sctp_next);
646 		if ((compare_with_wrap(stcb->asoc.last_acked_seq,
647 		    chk->rec.data.TSN_seq,
648 		    MAX_TSN)) ||
649 		    (stcb->asoc.last_acked_seq == chk->rec.data.TSN_seq)) {
650 			/* Strange case our list got out of order? */
651 			SCTP_PRINTF("Our list is out of order?\n");
652 			panic("Out of order list");
653 		}
654 		if ((chk->whoTo == net) && (chk->sent < SCTP_DATAGRAM_ACKED)) {
655 			/*
656 			 * found one to mark: If it is less than
657 			 * DATAGRAM_ACKED it MUST not be a skipped or marked
658 			 * TSN but instead one that is either already set
659 			 * for retransmission OR one that needs
660 			 * retransmission.
661 			 */
662 
663 			/* validate its been outstanding long enough */
664 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & (SCTP_EARLYFR_LOGGING_ENABLE | SCTP_FR_LOGGING_ENABLE)) {
665 				sctp_log_fr(chk->rec.data.TSN_seq,
666 				    chk->sent_rcv_time.tv_sec,
667 				    chk->sent_rcv_time.tv_usec,
668 				    SCTP_FR_T3_MARK_TIME);
669 			}
670 			if ((chk->sent_rcv_time.tv_sec > min_wait.tv_sec) && (window_probe == 0)) {
671 				/*
672 				 * we have reached a chunk that was sent
673 				 * some seconds past our min.. forget it we
674 				 * will find no more to send.
675 				 */
676 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & (SCTP_EARLYFR_LOGGING_ENABLE | SCTP_FR_LOGGING_ENABLE)) {
677 					sctp_log_fr(0,
678 					    chk->sent_rcv_time.tv_sec,
679 					    chk->sent_rcv_time.tv_usec,
680 					    SCTP_FR_T3_STOPPED);
681 				}
682 				continue;
683 			} else if ((chk->sent_rcv_time.tv_sec == min_wait.tv_sec) &&
684 			    (window_probe == 0)) {
685 				/*
686 				 * we must look at the micro seconds to
687 				 * know.
688 				 */
689 				if (chk->sent_rcv_time.tv_usec >= min_wait.tv_usec) {
690 					/*
691 					 * ok it was sent after our boundary
692 					 * time.
693 					 */
694 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & (SCTP_EARLYFR_LOGGING_ENABLE | SCTP_FR_LOGGING_ENABLE)) {
695 						sctp_log_fr(0,
696 						    chk->sent_rcv_time.tv_sec,
697 						    chk->sent_rcv_time.tv_usec,
698 						    SCTP_FR_T3_STOPPED);
699 					}
700 					continue;
701 				}
702 			}
703 			if (PR_SCTP_TTL_ENABLED(chk->flags)) {
704 				/* Is it expired? */
705 				if ((now.tv_sec > chk->rec.data.timetodrop.tv_sec) ||
706 				    ((chk->rec.data.timetodrop.tv_sec == now.tv_sec) &&
707 				    (now.tv_usec > chk->rec.data.timetodrop.tv_usec))) {
708 					/* Yes so drop it */
709 					if (chk->data) {
710 						(void)sctp_release_pr_sctp_chunk(stcb,
711 						    chk,
712 						    (SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_SENT),
713 						    &stcb->asoc.sent_queue, SCTP_SO_NOT_LOCKED);
714 					}
715 				}
716 				continue;
717 			}
718 			if (PR_SCTP_RTX_ENABLED(chk->flags)) {
719 				/* Has it been retransmitted tv_sec times? */
720 				if (chk->snd_count > chk->rec.data.timetodrop.tv_sec) {
721 					if (chk->data) {
722 						(void)sctp_release_pr_sctp_chunk(stcb,
723 						    chk,
724 						    (SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_SENT),
725 						    &stcb->asoc.sent_queue, SCTP_SO_NOT_LOCKED);
726 					}
727 				}
728 				continue;
729 			}
730 			if (chk->sent < SCTP_DATAGRAM_RESEND) {
731 				sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
732 				num_mk++;
733 				if (fir == 0) {
734 					fir = 1;
735 					tsnfirst = chk->rec.data.TSN_seq;
736 				}
737 				tsnlast = chk->rec.data.TSN_seq;
738 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & (SCTP_EARLYFR_LOGGING_ENABLE | SCTP_FR_LOGGING_ENABLE)) {
739 					sctp_log_fr(chk->rec.data.TSN_seq, chk->snd_count,
740 					    0, SCTP_FR_T3_MARKED);
741 				}
742 				if (chk->rec.data.chunk_was_revoked) {
743 					/* deflate the cwnd */
744 					chk->whoTo->cwnd -= chk->book_size;
745 					chk->rec.data.chunk_was_revoked = 0;
746 				}
747 				net->marked_retrans++;
748 				stcb->asoc.marked_retrans++;
749 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
750 					sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_RSND_TO,
751 					    chk->whoTo->flight_size,
752 					    chk->book_size,
753 					    (uintptr_t) chk->whoTo,
754 					    chk->rec.data.TSN_seq);
755 				}
756 				sctp_flight_size_decrease(chk);
757 				sctp_total_flight_decrease(stcb, chk);
758 				stcb->asoc.peers_rwnd += chk->send_size;
759 				stcb->asoc.peers_rwnd += SCTP_BASE_SYSCTL(sctp_peer_chunk_oh);
760 			}
761 			chk->sent = SCTP_DATAGRAM_RESEND;
762 			SCTP_STAT_INCR(sctps_markedretrans);
763 
764 			/* reset the TSN for striking and other FR stuff */
765 			chk->rec.data.doing_fast_retransmit = 0;
766 			/* Clear any time so NO RTT is being done */
767 			chk->do_rtt = 0;
768 			if (alt != net) {
769 				sctp_free_remote_addr(chk->whoTo);
770 				chk->no_fr_allowed = 1;
771 				chk->whoTo = alt;
772 				atomic_add_int(&alt->ref_count, 1);
773 			} else {
774 				chk->no_fr_allowed = 0;
775 				if (TAILQ_EMPTY(&stcb->asoc.send_queue)) {
776 					chk->rec.data.fast_retran_tsn = stcb->asoc.sending_seq;
777 				} else {
778 					chk->rec.data.fast_retran_tsn = (TAILQ_FIRST(&stcb->asoc.send_queue))->rec.data.TSN_seq;
779 				}
780 			}
781 			/*
782 			 * CMT: Do not allow FRs on retransmitted TSNs.
783 			 */
784 			if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) == 1) {
785 				chk->no_fr_allowed = 1;
786 			}
787 		} else if (chk->sent == SCTP_DATAGRAM_ACKED) {
788 			/* remember highest acked one */
789 			could_be_sent = chk;
790 		}
791 		if (chk->sent == SCTP_DATAGRAM_RESEND) {
792 			cnt_mk++;
793 		}
794 	}
795 	if ((orig_flight - net->flight_size) != (orig_tf - stcb->asoc.total_flight)) {
796 		/* we did not subtract the same things? */
797 		audit_tf = 1;
798 	}
799 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & (SCTP_EARLYFR_LOGGING_ENABLE | SCTP_FR_LOGGING_ENABLE)) {
800 		sctp_log_fr(tsnfirst, tsnlast, num_mk, SCTP_FR_T3_TIMEOUT);
801 	}
802 #ifdef SCTP_DEBUG
803 	if (num_mk) {
804 		SCTPDBG(SCTP_DEBUG_TIMER1, "LAST TSN marked was %x\n",
805 		    tsnlast);
806 		SCTPDBG(SCTP_DEBUG_TIMER1, "Num marked for retransmission was %d peer-rwd:%ld\n",
807 		    num_mk, (u_long)stcb->asoc.peers_rwnd);
808 		SCTPDBG(SCTP_DEBUG_TIMER1, "LAST TSN marked was %x\n",
809 		    tsnlast);
810 		SCTPDBG(SCTP_DEBUG_TIMER1, "Num marked for retransmission was %d peer-rwd:%d\n",
811 		    num_mk,
812 		    (int)stcb->asoc.peers_rwnd);
813 	}
814 #endif
815 	*num_marked = num_mk;
816 	if ((stcb->asoc.sent_queue_retran_cnt == 0) && (could_be_sent)) {
817 		/* fix it so we retransmit the highest acked anyway */
818 		sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
819 		cnt_mk++;
820 		could_be_sent->sent = SCTP_DATAGRAM_RESEND;
821 	}
822 	if (stcb->asoc.sent_queue_retran_cnt != cnt_mk) {
823 #ifdef INVARIANTS
824 		SCTP_PRINTF("Local Audit says there are %d for retran asoc cnt:%d we marked:%d this time\n",
825 		    cnt_mk, stcb->asoc.sent_queue_retran_cnt, num_mk);
826 #endif
827 #ifndef SCTP_AUDITING_ENABLED
828 		stcb->asoc.sent_queue_retran_cnt = cnt_mk;
829 #endif
830 	}
831 	/* Now check for a ECN Echo that may be stranded */
832 	TAILQ_FOREACH(chk, &stcb->asoc.control_send_queue, sctp_next) {
833 		if ((chk->whoTo == net) &&
834 		    (chk->rec.chunk_id.id == SCTP_ECN_ECHO)) {
835 			sctp_free_remote_addr(chk->whoTo);
836 			chk->whoTo = alt;
837 			if (chk->sent != SCTP_DATAGRAM_RESEND) {
838 				chk->sent = SCTP_DATAGRAM_RESEND;
839 				sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
840 			}
841 			atomic_add_int(&alt->ref_count, 1);
842 		}
843 	}
844 	if (audit_tf) {
845 		SCTPDBG(SCTP_DEBUG_TIMER4,
846 		    "Audit total flight due to negative value net:%p\n",
847 		    net);
848 		stcb->asoc.total_flight = 0;
849 		stcb->asoc.total_flight_count = 0;
850 		/* Clear all networks flight size */
851 		TAILQ_FOREACH(lnets, &stcb->asoc.nets, sctp_next) {
852 			lnets->flight_size = 0;
853 			SCTPDBG(SCTP_DEBUG_TIMER4,
854 			    "Net:%p c-f cwnd:%d ssthresh:%d\n",
855 			    lnets, lnets->cwnd, lnets->ssthresh);
856 		}
857 		TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
858 			if (chk->sent < SCTP_DATAGRAM_RESEND) {
859 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
860 					sctp_misc_ints(SCTP_FLIGHT_LOG_UP,
861 					    chk->whoTo->flight_size,
862 					    chk->book_size,
863 					    (uintptr_t) chk->whoTo,
864 					    chk->rec.data.TSN_seq);
865 				}
866 				sctp_flight_size_increase(chk);
867 				sctp_total_flight_increase(stcb, chk);
868 			}
869 		}
870 	}
871 	/*
872 	 * Setup the ecn nonce re-sync point. We do this since
873 	 * retranmissions are NOT setup for ECN. This means that do to
874 	 * Karn's rule, we don't know the total of the peers ecn bits.
875 	 */
876 	chk = TAILQ_FIRST(&stcb->asoc.send_queue);
877 	if (chk == NULL) {
878 		stcb->asoc.nonce_resync_tsn = stcb->asoc.sending_seq;
879 	} else {
880 		stcb->asoc.nonce_resync_tsn = chk->rec.data.TSN_seq;
881 	}
882 	stcb->asoc.nonce_wait_for_ecne = 0;
883 	stcb->asoc.nonce_sum_check = 0;
884 	/* We return 1 if we only have a window probe outstanding */
885 	return (0);
886 }
887 
888 static void
889 sctp_move_all_chunks_to_alt(struct sctp_tcb *stcb,
890     struct sctp_nets *net,
891     struct sctp_nets *alt)
892 {
893 	struct sctp_association *asoc;
894 	struct sctp_stream_out *outs;
895 	struct sctp_tmit_chunk *chk;
896 	struct sctp_stream_queue_pending *sp;
897 
898 	if (net == alt)
899 		/* nothing to do */
900 		return;
901 
902 	asoc = &stcb->asoc;
903 
904 	/*
905 	 * now through all the streams checking for chunks sent to our bad
906 	 * network.
907 	 */
908 	TAILQ_FOREACH(outs, &asoc->out_wheel, next_spoke) {
909 		/* now clean up any chunks here */
910 		TAILQ_FOREACH(sp, &outs->outqueue, next) {
911 			if (sp->net == net) {
912 				sctp_free_remote_addr(sp->net);
913 				sp->net = alt;
914 				atomic_add_int(&alt->ref_count, 1);
915 			}
916 		}
917 	}
918 	/* Now check the pending queue */
919 	TAILQ_FOREACH(chk, &asoc->send_queue, sctp_next) {
920 		if (chk->whoTo == net) {
921 			sctp_free_remote_addr(chk->whoTo);
922 			chk->whoTo = alt;
923 			atomic_add_int(&alt->ref_count, 1);
924 		}
925 	}
926 
927 }
928 
929 int
930 sctp_t3rxt_timer(struct sctp_inpcb *inp,
931     struct sctp_tcb *stcb,
932     struct sctp_nets *net)
933 {
934 	struct sctp_nets *alt;
935 	int win_probe, num_mk;
936 
937 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
938 		sctp_log_fr(0, 0, 0, SCTP_FR_T3_TIMEOUT);
939 	}
940 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
941 		struct sctp_nets *lnet;
942 
943 		TAILQ_FOREACH(lnet, &stcb->asoc.nets, sctp_next) {
944 			if (net == lnet) {
945 				sctp_log_cwnd(stcb, lnet, 1, SCTP_CWND_LOG_FROM_T3);
946 			} else {
947 				sctp_log_cwnd(stcb, lnet, 0, SCTP_CWND_LOG_FROM_T3);
948 			}
949 		}
950 	}
951 	/* Find an alternate and mark those for retransmission */
952 	if ((stcb->asoc.peers_rwnd == 0) &&
953 	    (stcb->asoc.total_flight < net->mtu)) {
954 		SCTP_STAT_INCR(sctps_timowindowprobe);
955 		win_probe = 1;
956 	} else {
957 		win_probe = 0;
958 	}
959 
960 	/*
961 	 * JRS 5/14/07 - If CMT PF is on and the destination if not already
962 	 * in PF state, set the destination to PF state and store the
963 	 * current time as the time that the destination was last active. In
964 	 * addition, find an alternate destination with PF-based
965 	 * find_alt_net().
966 	 */
967 	if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) && SCTP_BASE_SYSCTL(sctp_cmt_pf)) {
968 		if ((net->dest_state & SCTP_ADDR_PF) != SCTP_ADDR_PF) {
969 			net->dest_state |= SCTP_ADDR_PF;
970 			net->last_active = sctp_get_tick_count();
971 			SCTPDBG(SCTP_DEBUG_TIMER4, "Destination %p moved from active to PF.\n",
972 			    net);
973 		}
974 		alt = sctp_find_alternate_net(stcb, net, 2);
975 	} else if (SCTP_BASE_SYSCTL(sctp_cmt_on_off)) {
976 		/*
977 		 * CMT: Using RTX_SSTHRESH policy for CMT. If CMT is being
978 		 * used, then pick dest with largest ssthresh for any
979 		 * retransmission.
980 		 */
981 		alt = net;
982 		alt = sctp_find_alternate_net(stcb, alt, 1);
983 		/*
984 		 * CUCv2: If a different dest is picked for the
985 		 * retransmission, then new (rtx-)pseudo_cumack needs to be
986 		 * tracked for orig dest. Let CUCv2 track new (rtx-)
987 		 * pseudo-cumack always.
988 		 */
989 		net->find_pseudo_cumack = 1;
990 		net->find_rtx_pseudo_cumack = 1;
991 	} else {		/* CMT is OFF */
992 		alt = sctp_find_alternate_net(stcb, net, 0);
993 	}
994 
995 	(void)sctp_mark_all_for_resend(stcb, net, alt, win_probe, &num_mk);
996 	/* FR Loss recovery just ended with the T3. */
997 	stcb->asoc.fast_retran_loss_recovery = 0;
998 
999 	/* CMT FR loss recovery ended with the T3 */
1000 	net->fast_retran_loss_recovery = 0;
1001 
1002 	/*
1003 	 * setup the sat loss recovery that prevents satellite cwnd advance.
1004 	 */
1005 	stcb->asoc.sat_t3_loss_recovery = 1;
1006 	stcb->asoc.sat_t3_recovery_tsn = stcb->asoc.sending_seq;
1007 
1008 	/* Backoff the timer and cwnd */
1009 	sctp_backoff_on_timeout(stcb, net, win_probe, num_mk);
1010 	if (win_probe == 0) {
1011 		/* We don't do normal threshold management on window probes */
1012 		if (sctp_threshold_management(inp, stcb, net,
1013 		    stcb->asoc.max_send_times)) {
1014 			/* Association was destroyed */
1015 			return (1);
1016 		} else {
1017 			if (net != stcb->asoc.primary_destination) {
1018 				/* send a immediate HB if our RTO is stale */
1019 				struct timeval now;
1020 				unsigned int ms_goneby;
1021 
1022 				(void)SCTP_GETTIME_TIMEVAL(&now);
1023 				if (net->last_sent_time.tv_sec) {
1024 					ms_goneby = (now.tv_sec - net->last_sent_time.tv_sec) * 1000;
1025 				} else {
1026 					ms_goneby = 0;
1027 				}
1028 				if ((ms_goneby > net->RTO) || (net->RTO == 0)) {
1029 					/*
1030 					 * no recent feed back in an RTO or
1031 					 * more, request a RTT update
1032 					 */
1033 					if (sctp_send_hb(stcb, 1, net) < 0)
1034 						return 1;
1035 				}
1036 			}
1037 		}
1038 	} else {
1039 		/*
1040 		 * For a window probe we don't penalize the net's but only
1041 		 * the association. This may fail it if SACKs are not coming
1042 		 * back. If sack's are coming with rwnd locked at 0, we will
1043 		 * continue to hold things waiting for rwnd to raise
1044 		 */
1045 		if (sctp_threshold_management(inp, stcb, NULL,
1046 		    stcb->asoc.max_send_times)) {
1047 			/* Association was destroyed */
1048 			return (1);
1049 		}
1050 	}
1051 	if (net->dest_state & SCTP_ADDR_NOT_REACHABLE) {
1052 		/* Move all pending over too */
1053 		sctp_move_all_chunks_to_alt(stcb, net, alt);
1054 
1055 		/*
1056 		 * Get the address that failed, to force a new src address
1057 		 * selecton and a route allocation.
1058 		 */
1059 		if (net->ro._s_addr) {
1060 			sctp_free_ifa(net->ro._s_addr);
1061 			net->ro._s_addr = NULL;
1062 		}
1063 		net->src_addr_selected = 0;
1064 
1065 		/* Force a route allocation too */
1066 		if (net->ro.ro_rt) {
1067 			RTFREE(net->ro.ro_rt);
1068 			net->ro.ro_rt = NULL;
1069 		}
1070 		/* Was it our primary? */
1071 		if ((stcb->asoc.primary_destination == net) && (alt != net)) {
1072 			/*
1073 			 * Yes, note it as such and find an alternate note:
1074 			 * this means HB code must use this to resent the
1075 			 * primary if it goes active AND if someone does a
1076 			 * change-primary then this flag must be cleared
1077 			 * from any net structures.
1078 			 */
1079 			if (sctp_set_primary_addr(stcb,
1080 			    (struct sockaddr *)NULL,
1081 			    alt) == 0) {
1082 				net->dest_state |= SCTP_ADDR_WAS_PRIMARY;
1083 			}
1084 		}
1085 	} else if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) && SCTP_BASE_SYSCTL(sctp_cmt_pf) && (net->dest_state & SCTP_ADDR_PF) == SCTP_ADDR_PF) {
1086 		/*
1087 		 * JRS 5/14/07 - If the destination hasn't failed completely
1088 		 * but is in PF state, a PF-heartbeat needs to be sent
1089 		 * manually.
1090 		 */
1091 		if (sctp_send_hb(stcb, 1, net) < 0)
1092 			return 1;
1093 	}
1094 	/*
1095 	 * Special case for cookie-echo'ed case, we don't do output but must
1096 	 * await the COOKIE-ACK before retransmission
1097 	 */
1098 	if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_ECHOED) {
1099 		/*
1100 		 * Here we just reset the timer and start again since we
1101 		 * have not established the asoc
1102 		 */
1103 		sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
1104 		return (0);
1105 	}
1106 	if (stcb->asoc.peer_supports_prsctp) {
1107 		struct sctp_tmit_chunk *lchk;
1108 
1109 		lchk = sctp_try_advance_peer_ack_point(stcb, &stcb->asoc);
1110 		/* C3. See if we need to send a Fwd-TSN */
1111 		if (compare_with_wrap(stcb->asoc.advanced_peer_ack_point,
1112 		    stcb->asoc.last_acked_seq, MAX_TSN)) {
1113 			/*
1114 			 * ISSUE with ECN, see FWD-TSN processing for notes
1115 			 * on issues that will occur when the ECN NONCE
1116 			 * stuff is put into SCTP for cross checking.
1117 			 */
1118 			send_forward_tsn(stcb, &stcb->asoc);
1119 			if (lchk) {
1120 				/* Assure a timer is up */
1121 				sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, stcb, lchk->whoTo);
1122 			}
1123 		}
1124 	}
1125 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_MONITOR_ENABLE) {
1126 		sctp_log_cwnd(stcb, net, net->cwnd, SCTP_CWND_LOG_FROM_RTX);
1127 	}
1128 	return (0);
1129 }
1130 
1131 int
1132 sctp_t1init_timer(struct sctp_inpcb *inp,
1133     struct sctp_tcb *stcb,
1134     struct sctp_nets *net)
1135 {
1136 	/* bump the thresholds */
1137 	if (stcb->asoc.delayed_connection) {
1138 		/*
1139 		 * special hook for delayed connection. The library did NOT
1140 		 * complete the rest of its sends.
1141 		 */
1142 		stcb->asoc.delayed_connection = 0;
1143 		sctp_send_initiate(inp, stcb, SCTP_SO_NOT_LOCKED);
1144 		return (0);
1145 	}
1146 	if (SCTP_GET_STATE((&stcb->asoc)) != SCTP_STATE_COOKIE_WAIT) {
1147 		return (0);
1148 	}
1149 	if (sctp_threshold_management(inp, stcb, net,
1150 	    stcb->asoc.max_init_times)) {
1151 		/* Association was destroyed */
1152 		return (1);
1153 	}
1154 	stcb->asoc.dropped_special_cnt = 0;
1155 	sctp_backoff_on_timeout(stcb, stcb->asoc.primary_destination, 1, 0);
1156 	if (stcb->asoc.initial_init_rto_max < net->RTO) {
1157 		net->RTO = stcb->asoc.initial_init_rto_max;
1158 	}
1159 	if (stcb->asoc.numnets > 1) {
1160 		/* If we have more than one addr use it */
1161 		struct sctp_nets *alt;
1162 
1163 		alt = sctp_find_alternate_net(stcb, stcb->asoc.primary_destination, 0);
1164 		if ((alt != NULL) && (alt != stcb->asoc.primary_destination)) {
1165 			sctp_move_all_chunks_to_alt(stcb, stcb->asoc.primary_destination, alt);
1166 			stcb->asoc.primary_destination = alt;
1167 		}
1168 	}
1169 	/* Send out a new init */
1170 	sctp_send_initiate(inp, stcb, SCTP_SO_NOT_LOCKED);
1171 	return (0);
1172 }
1173 
1174 /*
1175  * For cookie and asconf we actually need to find and mark for resend, then
1176  * increment the resend counter (after all the threshold management stuff of
1177  * course).
1178  */
1179 int
1180 sctp_cookie_timer(struct sctp_inpcb *inp,
1181     struct sctp_tcb *stcb,
1182     struct sctp_nets *net)
1183 {
1184 	struct sctp_nets *alt;
1185 	struct sctp_tmit_chunk *cookie;
1186 
1187 	/* first before all else we must find the cookie */
1188 	TAILQ_FOREACH(cookie, &stcb->asoc.control_send_queue, sctp_next) {
1189 		if (cookie->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
1190 			break;
1191 		}
1192 	}
1193 	if (cookie == NULL) {
1194 		if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_ECHOED) {
1195 			/* FOOBAR! */
1196 			struct mbuf *oper;
1197 
1198 			oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
1199 			    0, M_DONTWAIT, 1, MT_DATA);
1200 			if (oper) {
1201 				struct sctp_paramhdr *ph;
1202 				uint32_t *ippp;
1203 
1204 				SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
1205 				    sizeof(uint32_t);
1206 				ph = mtod(oper, struct sctp_paramhdr *);
1207 				ph->param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1208 				ph->param_length = htons(SCTP_BUF_LEN(oper));
1209 				ippp = (uint32_t *) (ph + 1);
1210 				*ippp = htonl(SCTP_FROM_SCTP_TIMER + SCTP_LOC_3);
1211 			}
1212 			inp->last_abort_code = SCTP_FROM_SCTP_TIMER + SCTP_LOC_4;
1213 			sctp_abort_an_association(inp, stcb, SCTP_INTERNAL_ERROR,
1214 			    oper, SCTP_SO_NOT_LOCKED);
1215 		} else {
1216 #ifdef INVARIANTS
1217 			panic("Cookie timer expires in wrong state?");
1218 #else
1219 			SCTP_PRINTF("Strange in state %d not cookie-echoed yet c-e timer expires?\n", SCTP_GET_STATE(&stcb->asoc));
1220 			return (0);
1221 #endif
1222 		}
1223 		return (0);
1224 	}
1225 	/* Ok we found the cookie, threshold management next */
1226 	if (sctp_threshold_management(inp, stcb, cookie->whoTo,
1227 	    stcb->asoc.max_init_times)) {
1228 		/* Assoc is over */
1229 		return (1);
1230 	}
1231 	/*
1232 	 * cleared theshold management now lets backoff the address & select
1233 	 * an alternate
1234 	 */
1235 	stcb->asoc.dropped_special_cnt = 0;
1236 	sctp_backoff_on_timeout(stcb, cookie->whoTo, 1, 0);
1237 	alt = sctp_find_alternate_net(stcb, cookie->whoTo, 0);
1238 	if (alt != cookie->whoTo) {
1239 		sctp_free_remote_addr(cookie->whoTo);
1240 		cookie->whoTo = alt;
1241 		atomic_add_int(&alt->ref_count, 1);
1242 	}
1243 	/* Now mark the retran info */
1244 	if (cookie->sent != SCTP_DATAGRAM_RESEND) {
1245 		sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
1246 	}
1247 	cookie->sent = SCTP_DATAGRAM_RESEND;
1248 	/*
1249 	 * Now call the output routine to kick out the cookie again, Note we
1250 	 * don't mark any chunks for retran so that FR will need to kick in
1251 	 * to move these (or a send timer).
1252 	 */
1253 	return (0);
1254 }
1255 
1256 int
1257 sctp_strreset_timer(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
1258     struct sctp_nets *net)
1259 {
1260 	struct sctp_nets *alt;
1261 	struct sctp_tmit_chunk *strrst = NULL, *chk = NULL;
1262 
1263 	if (stcb->asoc.stream_reset_outstanding == 0) {
1264 		return (0);
1265 	}
1266 	/* find the existing STRRESET, we use the seq number we sent out on */
1267 	(void)sctp_find_stream_reset(stcb, stcb->asoc.str_reset_seq_out, &strrst);
1268 	if (strrst == NULL) {
1269 		return (0);
1270 	}
1271 	/* do threshold management */
1272 	if (sctp_threshold_management(inp, stcb, strrst->whoTo,
1273 	    stcb->asoc.max_send_times)) {
1274 		/* Assoc is over */
1275 		return (1);
1276 	}
1277 	/*
1278 	 * cleared theshold management now lets backoff the address & select
1279 	 * an alternate
1280 	 */
1281 	sctp_backoff_on_timeout(stcb, strrst->whoTo, 1, 0);
1282 	alt = sctp_find_alternate_net(stcb, strrst->whoTo, 0);
1283 	sctp_free_remote_addr(strrst->whoTo);
1284 	strrst->whoTo = alt;
1285 	atomic_add_int(&alt->ref_count, 1);
1286 
1287 	/* See if a ECN Echo is also stranded */
1288 	TAILQ_FOREACH(chk, &stcb->asoc.control_send_queue, sctp_next) {
1289 		if ((chk->whoTo == net) &&
1290 		    (chk->rec.chunk_id.id == SCTP_ECN_ECHO)) {
1291 			sctp_free_remote_addr(chk->whoTo);
1292 			if (chk->sent != SCTP_DATAGRAM_RESEND) {
1293 				chk->sent = SCTP_DATAGRAM_RESEND;
1294 				sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
1295 			}
1296 			chk->whoTo = alt;
1297 			atomic_add_int(&alt->ref_count, 1);
1298 		}
1299 	}
1300 	if (net->dest_state & SCTP_ADDR_NOT_REACHABLE) {
1301 		/*
1302 		 * If the address went un-reachable, we need to move to
1303 		 * alternates for ALL chk's in queue
1304 		 */
1305 		sctp_move_all_chunks_to_alt(stcb, net, alt);
1306 	}
1307 	/* mark the retran info */
1308 	if (strrst->sent != SCTP_DATAGRAM_RESEND)
1309 		sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
1310 	strrst->sent = SCTP_DATAGRAM_RESEND;
1311 
1312 	/* restart the timer */
1313 	sctp_timer_start(SCTP_TIMER_TYPE_STRRESET, inp, stcb, strrst->whoTo);
1314 	return (0);
1315 }
1316 
1317 int
1318 sctp_asconf_timer(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
1319     struct sctp_nets *net)
1320 {
1321 	struct sctp_nets *alt;
1322 	struct sctp_tmit_chunk *asconf, *chk, *nchk;
1323 
1324 	/* is this a first send, or a retransmission? */
1325 	if (TAILQ_EMPTY(&stcb->asoc.asconf_send_queue)) {
1326 		/* compose a new ASCONF chunk and send it */
1327 		sctp_send_asconf(stcb, net, SCTP_ADDR_NOT_LOCKED);
1328 	} else {
1329 		/*
1330 		 * Retransmission of the existing ASCONF is needed
1331 		 */
1332 
1333 		/* find the existing ASCONF */
1334 		asconf = TAILQ_FIRST(&stcb->asoc.asconf_send_queue);
1335 		if (asconf == NULL) {
1336 			return (0);
1337 		}
1338 		/* do threshold management */
1339 		if (sctp_threshold_management(inp, stcb, asconf->whoTo,
1340 		    stcb->asoc.max_send_times)) {
1341 			/* Assoc is over */
1342 			return (1);
1343 		}
1344 		if (asconf->snd_count > stcb->asoc.max_send_times) {
1345 			/*
1346 			 * Something is rotten: our peer is not responding
1347 			 * to ASCONFs but apparently is to other chunks.
1348 			 * i.e. it is not properly handling the chunk type
1349 			 * upper bits. Mark this peer as ASCONF incapable
1350 			 * and cleanup.
1351 			 */
1352 			SCTPDBG(SCTP_DEBUG_TIMER1, "asconf_timer: Peer has not responded to our repeated ASCONFs\n");
1353 			sctp_asconf_cleanup(stcb, net);
1354 			return (0);
1355 		}
1356 		/*
1357 		 * cleared threshold management, so now backoff the net and
1358 		 * select an alternate
1359 		 */
1360 		sctp_backoff_on_timeout(stcb, asconf->whoTo, 1, 0);
1361 		alt = sctp_find_alternate_net(stcb, asconf->whoTo, 0);
1362 		if (asconf->whoTo != alt) {
1363 			sctp_free_remote_addr(asconf->whoTo);
1364 			asconf->whoTo = alt;
1365 			atomic_add_int(&alt->ref_count, 1);
1366 		}
1367 		/* See if an ECN Echo is also stranded */
1368 		TAILQ_FOREACH(chk, &stcb->asoc.control_send_queue, sctp_next) {
1369 			if ((chk->whoTo == net) &&
1370 			    (chk->rec.chunk_id.id == SCTP_ECN_ECHO)) {
1371 				sctp_free_remote_addr(chk->whoTo);
1372 				chk->whoTo = alt;
1373 				if (chk->sent != SCTP_DATAGRAM_RESEND) {
1374 					chk->sent = SCTP_DATAGRAM_RESEND;
1375 					sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
1376 				}
1377 				atomic_add_int(&alt->ref_count, 1);
1378 			}
1379 		}
1380 		for (chk = asconf; chk; chk = nchk) {
1381 			nchk = TAILQ_NEXT(chk, sctp_next);
1382 			if (chk->whoTo != alt) {
1383 				sctp_free_remote_addr(chk->whoTo);
1384 				chk->whoTo = alt;
1385 				atomic_add_int(&alt->ref_count, 1);
1386 			}
1387 			if (asconf->sent != SCTP_DATAGRAM_RESEND && chk->sent != SCTP_DATAGRAM_UNSENT)
1388 				sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
1389 			chk->sent = SCTP_DATAGRAM_RESEND;
1390 		}
1391 		if (net->dest_state & SCTP_ADDR_NOT_REACHABLE) {
1392 			/*
1393 			 * If the address went un-reachable, we need to move
1394 			 * to the alternate for ALL chunks in queue
1395 			 */
1396 			sctp_move_all_chunks_to_alt(stcb, net, alt);
1397 			net = alt;
1398 		}
1399 		/* mark the retran info */
1400 		if (asconf->sent != SCTP_DATAGRAM_RESEND)
1401 			sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
1402 		asconf->sent = SCTP_DATAGRAM_RESEND;
1403 
1404 		/* send another ASCONF if any and we can do */
1405 		sctp_send_asconf(stcb, alt, SCTP_ADDR_NOT_LOCKED);
1406 	}
1407 	return (0);
1408 }
1409 
1410 /* Mobility adaptation */
1411 void
1412 sctp_delete_prim_timer(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
1413     struct sctp_nets *net)
1414 {
1415 	if (stcb->asoc.deleted_primary == NULL) {
1416 		SCTPDBG(SCTP_DEBUG_ASCONF1, "delete_prim_timer: deleted_primary is not stored...\n");
1417 		sctp_mobility_feature_off(inp, SCTP_MOBILITY_PRIM_DELETED);
1418 		return;
1419 	}
1420 	SCTPDBG(SCTP_DEBUG_ASCONF1, "delete_prim_timer: finished to keep deleted primary ");
1421 	SCTPDBG_ADDR(SCTP_DEBUG_ASCONF1, &stcb->asoc.deleted_primary->ro._l_addr.sa);
1422 	sctp_free_remote_addr(stcb->asoc.deleted_primary);
1423 	stcb->asoc.deleted_primary = NULL;
1424 	sctp_mobility_feature_off(inp, SCTP_MOBILITY_PRIM_DELETED);
1425 	return;
1426 }
1427 
1428 /*
1429  * For the shutdown and shutdown-ack, we do not keep one around on the
1430  * control queue. This means we must generate a new one and call the general
1431  * chunk output routine, AFTER having done threshold management.
1432  */
1433 int
1434 sctp_shutdown_timer(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
1435     struct sctp_nets *net)
1436 {
1437 	struct sctp_nets *alt;
1438 
1439 	/* first threshold managment */
1440 	if (sctp_threshold_management(inp, stcb, net, stcb->asoc.max_send_times)) {
1441 		/* Assoc is over */
1442 		return (1);
1443 	}
1444 	/* second select an alternative */
1445 	alt = sctp_find_alternate_net(stcb, net, 0);
1446 
1447 	/* third generate a shutdown into the queue for out net */
1448 	if (alt) {
1449 		sctp_send_shutdown(stcb, alt);
1450 	} else {
1451 		/*
1452 		 * if alt is NULL, there is no dest to send to??
1453 		 */
1454 		return (0);
1455 	}
1456 	/* fourth restart timer */
1457 	sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, inp, stcb, alt);
1458 	return (0);
1459 }
1460 
1461 int
1462 sctp_shutdownack_timer(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
1463     struct sctp_nets *net)
1464 {
1465 	struct sctp_nets *alt;
1466 
1467 	/* first threshold managment */
1468 	if (sctp_threshold_management(inp, stcb, net, stcb->asoc.max_send_times)) {
1469 		/* Assoc is over */
1470 		return (1);
1471 	}
1472 	/* second select an alternative */
1473 	alt = sctp_find_alternate_net(stcb, net, 0);
1474 
1475 	/* third generate a shutdown into the queue for out net */
1476 	sctp_send_shutdown_ack(stcb, alt);
1477 
1478 	/* fourth restart timer */
1479 	sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNACK, inp, stcb, alt);
1480 	return (0);
1481 }
1482 
1483 static void
1484 sctp_audit_stream_queues_for_size(struct sctp_inpcb *inp,
1485     struct sctp_tcb *stcb)
1486 {
1487 	struct sctp_stream_out *outs;
1488 	struct sctp_stream_queue_pending *sp;
1489 	unsigned int chks_in_queue = 0;
1490 	int being_filled = 0;
1491 
1492 	/*
1493 	 * This function is ONLY called when the send/sent queues are empty.
1494 	 */
1495 	if ((stcb == NULL) || (inp == NULL))
1496 		return;
1497 
1498 	if (stcb->asoc.sent_queue_retran_cnt) {
1499 		SCTP_PRINTF("Hmm, sent_queue_retran_cnt is non-zero %d\n",
1500 		    stcb->asoc.sent_queue_retran_cnt);
1501 		stcb->asoc.sent_queue_retran_cnt = 0;
1502 	}
1503 	SCTP_TCB_SEND_LOCK(stcb);
1504 	if (TAILQ_EMPTY(&stcb->asoc.out_wheel)) {
1505 		int i, cnt = 0;
1506 
1507 		/* Check to see if a spoke fell off the wheel */
1508 		for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
1509 			if (!TAILQ_EMPTY(&stcb->asoc.strmout[i].outqueue)) {
1510 				sctp_insert_on_wheel(stcb, &stcb->asoc, &stcb->asoc.strmout[i], 1);
1511 				cnt++;
1512 			}
1513 		}
1514 		if (cnt) {
1515 			/* yep, we lost a spoke or two */
1516 			SCTP_PRINTF("Found an additional %d streams NOT on outwheel, corrected\n", cnt);
1517 		} else {
1518 			/* no spokes lost, */
1519 			stcb->asoc.total_output_queue_size = 0;
1520 		}
1521 		SCTP_TCB_SEND_UNLOCK(stcb);
1522 		return;
1523 	}
1524 	SCTP_TCB_SEND_UNLOCK(stcb);
1525 	/* Check to see if some data queued, if so report it */
1526 	TAILQ_FOREACH(outs, &stcb->asoc.out_wheel, next_spoke) {
1527 		if (!TAILQ_EMPTY(&outs->outqueue)) {
1528 			TAILQ_FOREACH(sp, &outs->outqueue, next) {
1529 				if (sp->msg_is_complete)
1530 					being_filled++;
1531 				chks_in_queue++;
1532 			}
1533 		}
1534 	}
1535 	if (chks_in_queue != stcb->asoc.stream_queue_cnt) {
1536 		SCTP_PRINTF("Hmm, stream queue cnt at %d I counted %d in stream out wheel\n",
1537 		    stcb->asoc.stream_queue_cnt, chks_in_queue);
1538 	}
1539 	if (chks_in_queue) {
1540 		/* call the output queue function */
1541 		sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_T3, SCTP_SO_NOT_LOCKED);
1542 		if ((TAILQ_EMPTY(&stcb->asoc.send_queue)) &&
1543 		    (TAILQ_EMPTY(&stcb->asoc.sent_queue))) {
1544 			/*
1545 			 * Probably should go in and make it go back through
1546 			 * and add fragments allowed
1547 			 */
1548 			if (being_filled == 0) {
1549 				SCTP_PRINTF("Still nothing moved %d chunks are stuck\n",
1550 				    chks_in_queue);
1551 			}
1552 		}
1553 	} else {
1554 		SCTP_PRINTF("Found no chunks on any queue tot:%lu\n",
1555 		    (u_long)stcb->asoc.total_output_queue_size);
1556 		stcb->asoc.total_output_queue_size = 0;
1557 	}
1558 }
1559 
1560 int
1561 sctp_heartbeat_timer(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
1562     struct sctp_nets *net, int cnt_of_unconf)
1563 {
1564 	int ret;
1565 
1566 	if (net) {
1567 		if (net->hb_responded == 0) {
1568 			if (net->ro._s_addr) {
1569 				/*
1570 				 * Invalidate the src address if we did not
1571 				 * get a response last time.
1572 				 */
1573 				sctp_free_ifa(net->ro._s_addr);
1574 				net->ro._s_addr = NULL;
1575 				net->src_addr_selected = 0;
1576 			}
1577 			sctp_backoff_on_timeout(stcb, net, 1, 0);
1578 		}
1579 		/* Zero PBA, if it needs it */
1580 		if (net->partial_bytes_acked) {
1581 			net->partial_bytes_acked = 0;
1582 		}
1583 	}
1584 	if ((stcb->asoc.total_output_queue_size > 0) &&
1585 	    (TAILQ_EMPTY(&stcb->asoc.send_queue)) &&
1586 	    (TAILQ_EMPTY(&stcb->asoc.sent_queue))) {
1587 		sctp_audit_stream_queues_for_size(inp, stcb);
1588 	}
1589 	/* Send a new HB, this will do threshold managment, pick a new dest */
1590 	if (cnt_of_unconf == 0) {
1591 		if (sctp_send_hb(stcb, 0, NULL) < 0) {
1592 			return (1);
1593 		}
1594 	} else {
1595 		/*
1596 		 * this will send out extra hb's up to maxburst if there are
1597 		 * any unconfirmed addresses.
1598 		 */
1599 		uint32_t cnt_sent = 0;
1600 
1601 		TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1602 			if ((net->dest_state & SCTP_ADDR_UNCONFIRMED) &&
1603 			    (net->dest_state & SCTP_ADDR_REACHABLE)) {
1604 				cnt_sent++;
1605 				if (net->hb_responded == 0) {
1606 					/* Did we respond last time? */
1607 					if (net->ro._s_addr) {
1608 						sctp_free_ifa(net->ro._s_addr);
1609 						net->ro._s_addr = NULL;
1610 						net->src_addr_selected = 0;
1611 					}
1612 				}
1613 				ret = sctp_send_hb(stcb, 1, net);
1614 				if (ret < 0)
1615 					return 1;
1616 				else if (ret == 0) {
1617 					break;
1618 				}
1619 				if (cnt_sent >= SCTP_BASE_SYSCTL(sctp_hb_maxburst))
1620 					break;
1621 			}
1622 		}
1623 	}
1624 	return (0);
1625 }
1626 
1627 int
1628 sctp_is_hb_timer_running(struct sctp_tcb *stcb)
1629 {
1630 	if (SCTP_OS_TIMER_PENDING(&stcb->asoc.hb_timer.timer)) {
1631 		/* its running */
1632 		return (1);
1633 	} else {
1634 		/* nope */
1635 		return (0);
1636 	}
1637 }
1638 
1639 int
1640 sctp_is_sack_timer_running(struct sctp_tcb *stcb)
1641 {
1642 	if (SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
1643 		/* its running */
1644 		return (1);
1645 	} else {
1646 		/* nope */
1647 		return (0);
1648 	}
1649 }
1650 
1651 #define SCTP_NUMBER_OF_MTU_SIZES 18
1652 static uint32_t mtu_sizes[] = {
1653 	68,
1654 	296,
1655 	508,
1656 	512,
1657 	544,
1658 	576,
1659 	1006,
1660 	1492,
1661 	1500,
1662 	1536,
1663 	2002,
1664 	2048,
1665 	4352,
1666 	4464,
1667 	8166,
1668 	17914,
1669 	32000,
1670 	65535
1671 };
1672 
1673 
1674 static uint32_t
1675 sctp_getnext_mtu(struct sctp_inpcb *inp, uint32_t cur_mtu)
1676 {
1677 	/* select another MTU that is just bigger than this one */
1678 	int i;
1679 
1680 	for (i = 0; i < SCTP_NUMBER_OF_MTU_SIZES; i++) {
1681 		if (cur_mtu < mtu_sizes[i]) {
1682 			/* no max_mtu is bigger than this one */
1683 			return (mtu_sizes[i]);
1684 		}
1685 	}
1686 	/* here return the highest allowable */
1687 	return (cur_mtu);
1688 }
1689 
1690 
1691 void
1692 sctp_pathmtu_timer(struct sctp_inpcb *inp,
1693     struct sctp_tcb *stcb,
1694     struct sctp_nets *net)
1695 {
1696 	uint32_t next_mtu, mtu;
1697 
1698 	next_mtu = sctp_getnext_mtu(inp, net->mtu);
1699 
1700 	if ((next_mtu > net->mtu) && (net->port == 0)) {
1701 		if ((net->src_addr_selected == 0) ||
1702 		    (net->ro._s_addr == NULL) ||
1703 		    (net->ro._s_addr->localifa_flags & SCTP_BEING_DELETED)) {
1704 			if ((net->ro._s_addr != NULL) && (net->ro._s_addr->localifa_flags & SCTP_BEING_DELETED)) {
1705 				sctp_free_ifa(net->ro._s_addr);
1706 				net->ro._s_addr = NULL;
1707 				net->src_addr_selected = 0;
1708 			} else if (net->ro._s_addr == NULL) {
1709 #if defined(INET6) && defined(SCTP_EMBEDDED_V6_SCOPE)
1710 				if (net->ro._l_addr.sa.sa_family == AF_INET6) {
1711 					struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
1712 
1713 					/* KAME hack: embed scopeid */
1714 					(void)sa6_embedscope(sin6, ip6_use_defzone);
1715 				}
1716 #endif
1717 
1718 				net->ro._s_addr = sctp_source_address_selection(inp,
1719 				    stcb,
1720 				    (sctp_route_t *) & net->ro,
1721 				    net, 0, stcb->asoc.vrf_id);
1722 #if defined(INET6) && defined(SCTP_EMBEDDED_V6_SCOPE)
1723 				if (net->ro._l_addr.sa.sa_family == AF_INET6) {
1724 					struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
1725 
1726 					(void)sa6_recoverscope(sin6);
1727 				}
1728 #endif				/* INET6 */
1729 			}
1730 			if (net->ro._s_addr)
1731 				net->src_addr_selected = 1;
1732 		}
1733 		if (net->ro._s_addr) {
1734 			mtu = SCTP_GATHER_MTU_FROM_ROUTE(net->ro._s_addr, &net->ro._s_addr.sa, net->ro.ro_rt);
1735 			if (mtu > next_mtu) {
1736 				net->mtu = next_mtu;
1737 			}
1738 		}
1739 	}
1740 	/* restart the timer */
1741 	sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net);
1742 }
1743 
1744 void
1745 sctp_autoclose_timer(struct sctp_inpcb *inp,
1746     struct sctp_tcb *stcb,
1747     struct sctp_nets *net)
1748 {
1749 	struct timeval tn, *tim_touse;
1750 	struct sctp_association *asoc;
1751 	int ticks_gone_by;
1752 
1753 	(void)SCTP_GETTIME_TIMEVAL(&tn);
1754 	if (stcb->asoc.sctp_autoclose_ticks &&
1755 	    sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE)) {
1756 		/* Auto close is on */
1757 		asoc = &stcb->asoc;
1758 		/* pick the time to use */
1759 		if (asoc->time_last_rcvd.tv_sec >
1760 		    asoc->time_last_sent.tv_sec) {
1761 			tim_touse = &asoc->time_last_rcvd;
1762 		} else {
1763 			tim_touse = &asoc->time_last_sent;
1764 		}
1765 		/* Now has long enough transpired to autoclose? */
1766 		ticks_gone_by = SEC_TO_TICKS(tn.tv_sec - tim_touse->tv_sec);
1767 		if ((ticks_gone_by > 0) &&
1768 		    (ticks_gone_by >= (int)asoc->sctp_autoclose_ticks)) {
1769 			/*
1770 			 * autoclose time has hit, call the output routine,
1771 			 * which should do nothing just to be SURE we don't
1772 			 * have hanging data. We can then safely check the
1773 			 * queues and know that we are clear to send
1774 			 * shutdown
1775 			 */
1776 			sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_AUTOCLOSE_TMR, SCTP_SO_NOT_LOCKED);
1777 			/* Are we clean? */
1778 			if (TAILQ_EMPTY(&asoc->send_queue) &&
1779 			    TAILQ_EMPTY(&asoc->sent_queue)) {
1780 				/*
1781 				 * there is nothing queued to send, so I'm
1782 				 * done...
1783 				 */
1784 				if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) {
1785 					/* only send SHUTDOWN 1st time thru */
1786 					sctp_send_shutdown(stcb, stcb->asoc.primary_destination);
1787 					if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
1788 					    (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
1789 						SCTP_STAT_DECR_GAUGE32(sctps_currestab);
1790 					}
1791 					SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
1792 					SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
1793 					sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
1794 					    stcb->sctp_ep, stcb,
1795 					    asoc->primary_destination);
1796 					sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
1797 					    stcb->sctp_ep, stcb,
1798 					    asoc->primary_destination);
1799 				}
1800 			}
1801 		} else {
1802 			/*
1803 			 * No auto close at this time, reset t-o to check
1804 			 * later
1805 			 */
1806 			int tmp;
1807 
1808 			/* fool the timer startup to use the time left */
1809 			tmp = asoc->sctp_autoclose_ticks;
1810 			asoc->sctp_autoclose_ticks -= ticks_gone_by;
1811 			sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb,
1812 			    net);
1813 			/* restore the real tick value */
1814 			asoc->sctp_autoclose_ticks = tmp;
1815 		}
1816 	}
1817 }
1818 
1819 void
1820 sctp_iterator_timer(struct sctp_iterator *it)
1821 {
1822 	int iteration_count = 0;
1823 	int inp_skip = 0;
1824 
1825 	/*
1826 	 * only one iterator can run at a time. This is the only way we can
1827 	 * cleanly pull ep's from underneath all the running interators when
1828 	 * a ep is freed.
1829 	 */
1830 	SCTP_ITERATOR_LOCK();
1831 	if (it->inp == NULL) {
1832 		/* iterator is complete */
1833 done_with_iterator:
1834 		SCTP_ITERATOR_UNLOCK();
1835 		SCTP_INP_INFO_WLOCK();
1836 		TAILQ_REMOVE(&SCTP_BASE_INFO(iteratorhead), it, sctp_nxt_itr);
1837 		/* stopping the callout is not needed, in theory */
1838 		SCTP_INP_INFO_WUNLOCK();
1839 		(void)SCTP_OS_TIMER_STOP(&it->tmr.timer);
1840 		if (it->function_atend != NULL) {
1841 			(*it->function_atend) (it->pointer, it->val);
1842 		}
1843 		SCTP_FREE(it, SCTP_M_ITER);
1844 		return;
1845 	}
1846 select_a_new_ep:
1847 	SCTP_INP_WLOCK(it->inp);
1848 	while (((it->pcb_flags) &&
1849 	    ((it->inp->sctp_flags & it->pcb_flags) != it->pcb_flags)) ||
1850 	    ((it->pcb_features) &&
1851 	    ((it->inp->sctp_features & it->pcb_features) != it->pcb_features))) {
1852 		/* endpoint flags or features don't match, so keep looking */
1853 		if (it->iterator_flags & SCTP_ITERATOR_DO_SINGLE_INP) {
1854 			SCTP_INP_WUNLOCK(it->inp);
1855 			goto done_with_iterator;
1856 		}
1857 		SCTP_INP_WUNLOCK(it->inp);
1858 		it->inp = LIST_NEXT(it->inp, sctp_list);
1859 		if (it->inp == NULL) {
1860 			goto done_with_iterator;
1861 		}
1862 		SCTP_INP_WLOCK(it->inp);
1863 	}
1864 	if ((it->inp->inp_starting_point_for_iterator != NULL) &&
1865 	    (it->inp->inp_starting_point_for_iterator != it)) {
1866 		SCTP_PRINTF("Iterator collision, waiting for one at %p\n",
1867 		    it->inp);
1868 		SCTP_INP_WUNLOCK(it->inp);
1869 		goto start_timer_return;
1870 	}
1871 	/* mark the current iterator on the endpoint */
1872 	it->inp->inp_starting_point_for_iterator = it;
1873 	SCTP_INP_WUNLOCK(it->inp);
1874 	SCTP_INP_RLOCK(it->inp);
1875 	/* now go through each assoc which is in the desired state */
1876 	if (it->done_current_ep == 0) {
1877 		if (it->function_inp != NULL)
1878 			inp_skip = (*it->function_inp) (it->inp, it->pointer, it->val);
1879 		it->done_current_ep = 1;
1880 	}
1881 	if (it->stcb == NULL) {
1882 		/* run the per instance function */
1883 		it->stcb = LIST_FIRST(&it->inp->sctp_asoc_list);
1884 	}
1885 	SCTP_INP_RUNLOCK(it->inp);
1886 	if ((inp_skip) || it->stcb == NULL) {
1887 		if (it->function_inp_end != NULL) {
1888 			inp_skip = (*it->function_inp_end) (it->inp,
1889 			    it->pointer,
1890 			    it->val);
1891 		}
1892 		goto no_stcb;
1893 	}
1894 	if ((it->stcb) &&
1895 	    (it->stcb->asoc.stcb_starting_point_for_iterator == it)) {
1896 		it->stcb->asoc.stcb_starting_point_for_iterator = NULL;
1897 	}
1898 	while (it->stcb) {
1899 		SCTP_TCB_LOCK(it->stcb);
1900 		if (it->asoc_state && ((it->stcb->asoc.state & it->asoc_state) != it->asoc_state)) {
1901 			/* not in the right state... keep looking */
1902 			SCTP_TCB_UNLOCK(it->stcb);
1903 			goto next_assoc;
1904 		}
1905 		/* mark the current iterator on the assoc */
1906 		it->stcb->asoc.stcb_starting_point_for_iterator = it;
1907 		/* see if we have limited out the iterator loop */
1908 		iteration_count++;
1909 		if (iteration_count > SCTP_ITERATOR_MAX_AT_ONCE) {
1910 	start_timer_return:
1911 			/* set a timer to continue this later */
1912 			if (it->stcb)
1913 				SCTP_TCB_UNLOCK(it->stcb);
1914 			sctp_timer_start(SCTP_TIMER_TYPE_ITERATOR,
1915 			    (struct sctp_inpcb *)it, NULL, NULL);
1916 			SCTP_ITERATOR_UNLOCK();
1917 			return;
1918 		}
1919 		/* run function on this one */
1920 		(*it->function_assoc) (it->inp, it->stcb, it->pointer, it->val);
1921 
1922 		/*
1923 		 * we lie here, it really needs to have its own type but
1924 		 * first I must verify that this won't effect things :-0
1925 		 */
1926 		if (it->no_chunk_output == 0)
1927 			sctp_chunk_output(it->inp, it->stcb, SCTP_OUTPUT_FROM_T3, SCTP_SO_NOT_LOCKED);
1928 
1929 		SCTP_TCB_UNLOCK(it->stcb);
1930 next_assoc:
1931 		it->stcb = LIST_NEXT(it->stcb, sctp_tcblist);
1932 		if (it->stcb == NULL) {
1933 			if (it->function_inp_end != NULL) {
1934 				inp_skip = (*it->function_inp_end) (it->inp,
1935 				    it->pointer,
1936 				    it->val);
1937 			}
1938 		}
1939 	}
1940 no_stcb:
1941 	/* done with all assocs on this endpoint, move on to next endpoint */
1942 	it->done_current_ep = 0;
1943 	SCTP_INP_WLOCK(it->inp);
1944 	it->inp->inp_starting_point_for_iterator = NULL;
1945 	SCTP_INP_WUNLOCK(it->inp);
1946 	if (it->iterator_flags & SCTP_ITERATOR_DO_SINGLE_INP) {
1947 		it->inp = NULL;
1948 	} else {
1949 		SCTP_INP_INFO_RLOCK();
1950 		it->inp = LIST_NEXT(it->inp, sctp_list);
1951 		SCTP_INP_INFO_RUNLOCK();
1952 	}
1953 	if (it->inp == NULL) {
1954 		goto done_with_iterator;
1955 	}
1956 	goto select_a_new_ep;
1957 }
1958