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