sctp_timer.c revision 171572
1163953Srrs/*-
2169382Srrs * Copyright (c) 2001-2007, by Cisco Systems, Inc. All rights reserved.
3163953Srrs *
4163953Srrs * Redistribution and use in source and binary forms, with or without
5163953Srrs * modification, are permitted provided that the following conditions are met:
6163953Srrs *
7163953Srrs * a) Redistributions of source code must retain the above copyright notice,
8163953Srrs *   this list of conditions and the following disclaimer.
9163953Srrs *
10163953Srrs * b) Redistributions in binary form must reproduce the above copyright
11163953Srrs *    notice, this list of conditions and the following disclaimer in
12163953Srrs *   the documentation and/or other materials provided with the distribution.
13163953Srrs *
14163953Srrs * c) Neither the name of Cisco Systems, Inc. nor the names of its
15163953Srrs *    contributors may be used to endorse or promote products derived
16163953Srrs *    from this software without specific prior written permission.
17163953Srrs *
18163953Srrs * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19163953Srrs * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20163953Srrs * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21163953Srrs * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22163953Srrs * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23163953Srrs * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24163953Srrs * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25163953Srrs * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26163953Srrs * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27163953Srrs * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
28163953Srrs * THE POSSIBILITY OF SUCH DAMAGE.
29163953Srrs */
30163953Srrs
31163953Srrs/* $KAME: sctp_timer.c,v 1.29 2005/03/06 16:04:18 itojun Exp $	 */
32163953Srrs
33163953Srrs#include <sys/cdefs.h>
34163953Srrs__FBSDID("$FreeBSD: head/sys/netinet/sctp_timer.c 171572 2007-07-24 20:06:02Z rrs $");
35163953Srrs
36163953Srrs#define _IP_VHL
37166086Srrs#include <netinet/sctp_os.h>
38163953Srrs#include <netinet/sctp_pcb.h>
39163953Srrs#ifdef INET6
40163953Srrs#include <netinet6/sctp6_var.h>
41163953Srrs#endif
42163953Srrs#include <netinet/sctp_var.h>
43167598Srrs#include <netinet/sctp_sysctl.h>
44163953Srrs#include <netinet/sctp_timer.h>
45163953Srrs#include <netinet/sctputil.h>
46163953Srrs#include <netinet/sctp_output.h>
47163953Srrs#include <netinet/sctp_header.h>
48163953Srrs#include <netinet/sctp_indata.h>
49163953Srrs#include <netinet/sctp_asconf.h>
50163953Srrs#include <netinet/sctp_input.h>
51163953Srrs#include <netinet/sctp.h>
52163953Srrs#include <netinet/sctp_uio.h>
53163953Srrs
54163953Srrs
55163953Srrs
56163953Srrsvoid
57163953Srrssctp_early_fr_timer(struct sctp_inpcb *inp,
58163953Srrs    struct sctp_tcb *stcb,
59163953Srrs    struct sctp_nets *net)
60163953Srrs{
61163953Srrs	struct sctp_tmit_chunk *chk, *tp2;
62163953Srrs	struct timeval now, min_wait, tv;
63163953Srrs	unsigned int cur_rtt, cnt = 0, cnt_resend = 0;
64163953Srrs
65163953Srrs	/* an early FR is occuring. */
66169378Srrs	(void)SCTP_GETTIME_TIMEVAL(&now);
67163953Srrs	/* get cur rto in micro-seconds */
68163953Srrs	if (net->lastsa == 0) {
69163953Srrs		/* Hmm no rtt estimate yet? */
70163953Srrs		cur_rtt = stcb->asoc.initial_rto >> 2;
71163953Srrs	} else {
72163953Srrs
73163953Srrs		cur_rtt = ((net->lastsa >> 2) + net->lastsv) >> 1;
74163953Srrs	}
75163953Srrs	if (cur_rtt < sctp_early_fr_msec) {
76163953Srrs		cur_rtt = sctp_early_fr_msec;
77163953Srrs	}
78163953Srrs	cur_rtt *= 1000;
79163953Srrs	tv.tv_sec = cur_rtt / 1000000;
80163953Srrs	tv.tv_usec = cur_rtt % 1000000;
81163953Srrs	min_wait = now;
82163953Srrs	timevalsub(&min_wait, &tv);
83163953Srrs	if (min_wait.tv_sec < 0 || min_wait.tv_usec < 0) {
84163953Srrs		/*
85163953Srrs		 * if we hit here, we don't have enough seconds on the clock
86163953Srrs		 * to account for the RTO. We just let the lower seconds be
87163953Srrs		 * the bounds and don't worry about it. This may mean we
88163953Srrs		 * will mark a lot more than we should.
89163953Srrs		 */
90163953Srrs		min_wait.tv_sec = min_wait.tv_usec = 0;
91163953Srrs	}
92163953Srrs	chk = TAILQ_LAST(&stcb->asoc.sent_queue, sctpchunk_listhead);
93163953Srrs	for (; chk != NULL; chk = tp2) {
94163953Srrs		tp2 = TAILQ_PREV(chk, sctpchunk_listhead, sctp_next);
95163953Srrs		if (chk->whoTo != net) {
96163953Srrs			continue;
97163953Srrs		}
98163953Srrs		if (chk->sent == SCTP_DATAGRAM_RESEND)
99163953Srrs			cnt_resend++;
100163953Srrs		else if ((chk->sent > SCTP_DATAGRAM_UNSENT) &&
101163953Srrs		    (chk->sent < SCTP_DATAGRAM_RESEND)) {
102163953Srrs			/* pending, may need retran */
103163953Srrs			if (chk->sent_rcv_time.tv_sec > min_wait.tv_sec) {
104163953Srrs				/*
105163953Srrs				 * we have reached a chunk that was sent
106163953Srrs				 * some seconds past our min.. forget it we
107163953Srrs				 * will find no more to send.
108163953Srrs				 */
109163953Srrs				continue;
110163953Srrs			} else if (chk->sent_rcv_time.tv_sec == min_wait.tv_sec) {
111163953Srrs				/*
112163953Srrs				 * we must look at the micro seconds to
113163953Srrs				 * know.
114163953Srrs				 */
115163953Srrs				if (chk->sent_rcv_time.tv_usec >= min_wait.tv_usec) {
116163953Srrs					/*
117163953Srrs					 * ok it was sent after our boundary
118163953Srrs					 * time.
119163953Srrs					 */
120163953Srrs					continue;
121163953Srrs				}
122163953Srrs			}
123170744Srrs			if (sctp_logging_level & SCTP_EARLYFR_LOGGING_ENABLE) {
124170744Srrs				sctp_log_fr(chk->rec.data.TSN_seq, chk->snd_count,
125170744Srrs				    4, SCTP_FR_MARKED_EARLY);
126170744Srrs			}
127163953Srrs			SCTP_STAT_INCR(sctps_earlyfrmrkretrans);
128163953Srrs			chk->sent = SCTP_DATAGRAM_RESEND;
129163953Srrs			sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
130163953Srrs			/* double book size since we are doing an early FR */
131163953Srrs			chk->book_size_scale++;
132163953Srrs			cnt += chk->send_size;
133163953Srrs			if ((cnt + net->flight_size) > net->cwnd) {
134163953Srrs				/* Mark all we could possibly resend */
135163953Srrs				break;
136163953Srrs			}
137163953Srrs		}
138163953Srrs	}
139163953Srrs	if (cnt) {
140163953Srrs		/*
141171440Srrs		 * JRS - Use the congestion control given in the congestion
142171440Srrs		 * control module
143163953Srrs		 */
144171440Srrs		stcb->asoc.cc_functions.sctp_cwnd_update_after_fr_timer(inp, stcb, net);
145163953Srrs	} else if (cnt_resend) {
146163953Srrs		sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_EARLY_FR_TMR);
147163953Srrs	}
148163953Srrs	/* Restart it? */
149163953Srrs	if (net->flight_size < net->cwnd) {
150163953Srrs		SCTP_STAT_INCR(sctps_earlyfrstrtmr);
151163953Srrs		sctp_timer_start(SCTP_TIMER_TYPE_EARLYFR, stcb->sctp_ep, stcb, net);
152163953Srrs	}
153163953Srrs}
154163953Srrs
155163953Srrsvoid
156163953Srrssctp_audit_retranmission_queue(struct sctp_association *asoc)
157163953Srrs{
158163953Srrs	struct sctp_tmit_chunk *chk;
159163953Srrs
160169420Srrs	SCTPDBG(SCTP_DEBUG_TIMER4, "Audit invoked on send queue cnt:%d onqueue:%d\n",
161169420Srrs	    asoc->sent_queue_retran_cnt,
162169420Srrs	    asoc->sent_queue_cnt);
163163953Srrs	asoc->sent_queue_retran_cnt = 0;
164163953Srrs	asoc->sent_queue_cnt = 0;
165163953Srrs	TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
166163953Srrs		if (chk->sent == SCTP_DATAGRAM_RESEND) {
167163953Srrs			sctp_ucount_incr(asoc->sent_queue_retran_cnt);
168163953Srrs		}
169163953Srrs		asoc->sent_queue_cnt++;
170163953Srrs	}
171163953Srrs	TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
172163953Srrs		if (chk->sent == SCTP_DATAGRAM_RESEND) {
173163953Srrs			sctp_ucount_incr(asoc->sent_queue_retran_cnt);
174163953Srrs		}
175163953Srrs	}
176169420Srrs	SCTPDBG(SCTP_DEBUG_TIMER4, "Audit completes retran:%d onqueue:%d\n",
177169420Srrs	    asoc->sent_queue_retran_cnt,
178169420Srrs	    asoc->sent_queue_cnt);
179163953Srrs}
180163953Srrs
181163953Srrsint
182163953Srrssctp_threshold_management(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
183163953Srrs    struct sctp_nets *net, uint16_t threshold)
184163953Srrs{
185163953Srrs	if (net) {
186163953Srrs		net->error_count++;
187169420Srrs		SCTPDBG(SCTP_DEBUG_TIMER4, "Error count for %p now %d thresh:%d\n",
188169420Srrs		    net, net->error_count,
189169420Srrs		    net->failure_threshold);
190163953Srrs		if (net->error_count > net->failure_threshold) {
191163953Srrs			/* We had a threshold failure */
192163953Srrs			if (net->dest_state & SCTP_ADDR_REACHABLE) {
193163953Srrs				net->dest_state &= ~SCTP_ADDR_REACHABLE;
194163953Srrs				net->dest_state |= SCTP_ADDR_NOT_REACHABLE;
195167598Srrs				net->dest_state &= ~SCTP_ADDR_REQ_PRIMARY;
196163953Srrs				if (net == stcb->asoc.primary_destination) {
197163953Srrs					net->dest_state |= SCTP_ADDR_WAS_PRIMARY;
198163953Srrs				}
199171440Srrs				/*
200171440Srrs				 * JRS 5/14/07 - If a destination is
201171440Srrs				 * unreachable, the PF bit is turned off.
202171440Srrs				 * This allows an unambiguous use of the PF
203171440Srrs				 * bit for destinations that are reachable
204171440Srrs				 * but potentially failed. If the
205171440Srrs				 * destination is set to the unreachable
206171440Srrs				 * state, also set the destination to the PF
207171440Srrs				 * state.
208171440Srrs				 */
209171440Srrs				/*
210171440Srrs				 * Add debug message here if destination is
211171440Srrs				 * not in PF state.
212171440Srrs				 */
213171440Srrs				/* Stop any running T3 timers here? */
214171477Srrs				if (sctp_cmt_on_off && sctp_cmt_pf) {
215171440Srrs					net->dest_state &= ~SCTP_ADDR_PF;
216171440Srrs					SCTPDBG(SCTP_DEBUG_TIMER4, "Destination %p moved from PF to unreachable.\n",
217171440Srrs					    net);
218171440Srrs				}
219163953Srrs				sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN,
220163953Srrs				    stcb,
221163953Srrs				    SCTP_FAILED_THRESHOLD,
222163953Srrs				    (void *)net);
223163953Srrs			}
224163953Srrs		}
225163953Srrs		/*********HOLD THIS COMMENT FOR PATCH OF ALTERNATE
226163953Srrs		 *********ROUTING CODE
227163953Srrs		 */
228163953Srrs		/*********HOLD THIS COMMENT FOR END OF PATCH OF ALTERNATE
229163953Srrs		 *********ROUTING CODE
230163953Srrs		 */
231163953Srrs	}
232163953Srrs	if (stcb == NULL)
233163953Srrs		return (0);
234163953Srrs
235163953Srrs	if (net) {
236163953Srrs		if ((net->dest_state & SCTP_ADDR_UNCONFIRMED) == 0) {
237163953Srrs			stcb->asoc.overall_error_count++;
238163953Srrs		}
239163953Srrs	} else {
240163953Srrs		stcb->asoc.overall_error_count++;
241163953Srrs	}
242169420Srrs	SCTPDBG(SCTP_DEBUG_TIMER4, "Overall error count for %p now %d thresh:%u state:%x\n",
243169420Srrs	    &stcb->asoc, stcb->asoc.overall_error_count,
244169420Srrs	    (uint32_t) threshold,
245169420Srrs	    ((net == NULL) ? (uint32_t) 0 : (uint32_t) net->dest_state));
246163953Srrs	/*
247163953Srrs	 * We specifically do not do >= to give the assoc one more change
248163953Srrs	 * before we fail it.
249163953Srrs	 */
250163953Srrs	if (stcb->asoc.overall_error_count > threshold) {
251163953Srrs		/* Abort notification sends a ULP notify */
252163953Srrs		struct mbuf *oper;
253163953Srrs
254163953Srrs		oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
255163953Srrs		    0, M_DONTWAIT, 1, MT_DATA);
256163953Srrs		if (oper) {
257163953Srrs			struct sctp_paramhdr *ph;
258163953Srrs			uint32_t *ippp;
259163953Srrs
260165647Srrs			SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
261163953Srrs			    sizeof(uint32_t);
262163953Srrs			ph = mtod(oper, struct sctp_paramhdr *);
263163953Srrs			ph->param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
264165647Srrs			ph->param_length = htons(SCTP_BUF_LEN(oper));
265163953Srrs			ippp = (uint32_t *) (ph + 1);
266165220Srrs			*ippp = htonl(SCTP_FROM_SCTP_TIMER + SCTP_LOC_1);
267163953Srrs		}
268165220Srrs		inp->last_abort_code = SCTP_FROM_SCTP_TIMER + SCTP_LOC_1;
269163953Srrs		sctp_abort_an_association(inp, stcb, SCTP_FAILED_THRESHOLD, oper);
270163953Srrs		return (1);
271163953Srrs	}
272163953Srrs	return (0);
273163953Srrs}
274163953Srrs
275163953Srrsstruct sctp_nets *
276163953Srrssctp_find_alternate_net(struct sctp_tcb *stcb,
277163953Srrs    struct sctp_nets *net,
278171440Srrs    int mode)
279163953Srrs{
280163953Srrs	/* Find and return an alternate network if possible */
281171440Srrs	struct sctp_nets *alt, *mnet, *min_errors_net = NULL, *max_cwnd_net = NULL;
282163953Srrs	int once;
283163953Srrs
284171440Srrs	/* JRS 5/14/07 - Initialize min_errors to an impossible value. */
285171440Srrs	int min_errors = -1;
286171440Srrs	uint32_t max_cwnd = 0;
287171440Srrs
288163953Srrs	if (stcb->asoc.numnets == 1) {
289163953Srrs		/* No others but net */
290163953Srrs		return (TAILQ_FIRST(&stcb->asoc.nets));
291163953Srrs	}
292171440Srrs	/*
293171440Srrs	 * JRS 5/14/07 - If mode is set to 2, use the CMT PF find alternate
294171440Srrs	 * net algorithm. This algorithm chooses the active destination (not
295171440Srrs	 * in PF state) with the largest cwnd value. If all destinations are
296171440Srrs	 * in PF state, unreachable, or unconfirmed, choose the desination
297171440Srrs	 * that is in PF state with the lowest error count. In case of a
298171440Srrs	 * tie, choose the destination that was most recently active.
299171440Srrs	 */
300171440Srrs	if (mode == 2) {
301163953Srrs		TAILQ_FOREACH(mnet, &stcb->asoc.nets, sctp_next) {
302171440Srrs			/*
303171440Srrs			 * JRS 5/14/07 - If the destination is unreachable
304171440Srrs			 * or unconfirmed, skip it.
305171440Srrs			 */
306163953Srrs			if (((mnet->dest_state & SCTP_ADDR_REACHABLE) != SCTP_ADDR_REACHABLE) ||
307171440Srrs			    (mnet->dest_state & SCTP_ADDR_UNCONFIRMED)) {
308171440Srrs				continue;
309171440Srrs			}
310171440Srrs			/*
311171440Srrs			 * JRS 5/14/07 -  If the destination is reachable
312171440Srrs			 * but in PF state, compare the error count of the
313171440Srrs			 * destination to the minimum error count seen thus
314171440Srrs			 * far. Store the destination with the lower error
315171440Srrs			 * count.  If the error counts are equal, store the
316171440Srrs			 * destination that was most recently active.
317171440Srrs			 */
318171440Srrs			if (mnet->dest_state & SCTP_ADDR_PF) {
319171440Srrs				/*
320171440Srrs				 * JRS 5/14/07 - If the destination under
321171440Srrs				 * consideration is the current destination,
322171440Srrs				 * work as if the error count is one higher.
323171440Srrs				 * The actual error count will not be
324171440Srrs				 * incremented until later in the t3
325171440Srrs				 * handler.
326171440Srrs				 */
327171440Srrs				if (mnet == net) {
328171440Srrs					if (min_errors == -1) {
329171440Srrs						min_errors = mnet->error_count + 1;
330171440Srrs						min_errors_net = mnet;
331171440Srrs					} else if (mnet->error_count + 1 < min_errors) {
332171440Srrs						min_errors = mnet->error_count + 1;
333171440Srrs						min_errors_net = mnet;
334171440Srrs					} else if (mnet->error_count + 1 == min_errors
335171440Srrs					    && mnet->last_active > min_errors_net->last_active) {
336171440Srrs						min_errors_net = mnet;
337171440Srrs						min_errors = mnet->error_count + 1;
338171440Srrs					}
339171440Srrs					continue;
340171440Srrs				} else {
341171440Srrs					if (min_errors == -1) {
342171440Srrs						min_errors = mnet->error_count;
343171440Srrs						min_errors_net = mnet;
344171440Srrs					} else if (mnet->error_count < min_errors) {
345171440Srrs						min_errors = mnet->error_count;
346171440Srrs						min_errors_net = mnet;
347171440Srrs					} else if (mnet->error_count == min_errors
348171440Srrs					    && mnet->last_active > min_errors_net->last_active) {
349171440Srrs						min_errors_net = mnet;
350171440Srrs						min_errors = mnet->error_count;
351171440Srrs					}
352171440Srrs					continue;
353171440Srrs				}
354171440Srrs			}
355171440Srrs			/*
356171440Srrs			 * JRS 5/14/07 - If the destination is reachable and
357171440Srrs			 * not in PF state, compare the cwnd of the
358171440Srrs			 * destination to the highest cwnd seen thus far.
359171440Srrs			 * Store the destination with the higher cwnd value.
360171440Srrs			 * If the cwnd values are equal, randomly choose one
361171440Srrs			 * of the two destinations.
362171440Srrs			 */
363171440Srrs			if (max_cwnd < mnet->cwnd) {
364171440Srrs				max_cwnd_net = mnet;
365171440Srrs				max_cwnd = mnet->cwnd;
366171440Srrs			} else if (max_cwnd == mnet->cwnd) {
367171440Srrs				uint32_t rndval;
368171440Srrs				uint8_t this_random;
369171440Srrs
370171440Srrs				if (stcb->asoc.hb_random_idx > 3) {
371171440Srrs					rndval = sctp_select_initial_TSN(&stcb->sctp_ep->sctp_ep);
372171440Srrs					memcpy(stcb->asoc.hb_random_values, &rndval, sizeof(stcb->asoc.hb_random_values));
373171440Srrs					this_random = stcb->asoc.hb_random_values[0];
374171440Srrs					stcb->asoc.hb_random_idx++;
375171440Srrs					stcb->asoc.hb_ect_randombit = 0;
376171440Srrs				} else {
377171440Srrs					this_random = stcb->asoc.hb_random_values[stcb->asoc.hb_random_idx];
378171440Srrs					stcb->asoc.hb_random_idx++;
379171440Srrs					stcb->asoc.hb_ect_randombit = 0;
380171440Srrs				}
381171440Srrs				if (this_random % 2 == 1) {
382171440Srrs					max_cwnd_net = mnet;
383171440Srrs					max_cwnd = mnet->cwnd;
384171440Srrs					//Useless ?
385171440Srrs				}
386171440Srrs			}
387171440Srrs		}
388171440Srrs		/*
389171440Srrs		 * JRS 5/14/07 - After all destination have been considered
390171440Srrs		 * as alternates, check to see if there was some active
391171440Srrs		 * destination (not in PF state).  If not, check to see if
392171440Srrs		 * there was some PF destination with the minimum number of
393171440Srrs		 * errors.  If not, return the original destination.  If
394171440Srrs		 * there is a min_errors_net, remove the PF flag from that
395171440Srrs		 * destination, set the cwnd to one or two MTUs, and return
396171440Srrs		 * the destination as an alt. If there was some active
397171440Srrs		 * destination with a highest cwnd, return the destination
398171440Srrs		 * as an alt.
399171440Srrs		 */
400171440Srrs		if (max_cwnd_net == NULL) {
401171440Srrs			if (min_errors_net == NULL) {
402171440Srrs				return (net);
403171440Srrs			}
404171440Srrs			min_errors_net->dest_state &= ~SCTP_ADDR_PF;
405171440Srrs			min_errors_net->cwnd = min_errors_net->mtu * sctp_cmt_pf;
406171440Srrs			if (SCTP_OS_TIMER_PENDING(&min_errors_net->rxt_timer.timer)) {
407171440Srrs				sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
408171440Srrs				    stcb, min_errors_net,
409171440Srrs				    SCTP_FROM_SCTP_TIMER + SCTP_LOC_2);
410171440Srrs			}
411171440Srrs			SCTPDBG(SCTP_DEBUG_TIMER4, "Destination %p moved from PF to active with %d errors.\n",
412171440Srrs			    min_errors_net, min_errors_net->error_count);
413171440Srrs			return (min_errors_net);
414171440Srrs		} else {
415171440Srrs			return (max_cwnd_net);
416171440Srrs		}
417171440Srrs	}
418171440Srrs	/*
419171440Srrs	 * JRS 5/14/07 - If mode is set to 1, use the CMT policy for
420171440Srrs	 * choosing an alternate net.
421171440Srrs	 */
422171440Srrs	else if (mode == 1) {
423171440Srrs		TAILQ_FOREACH(mnet, &stcb->asoc.nets, sctp_next) {
424171440Srrs			if (((mnet->dest_state & SCTP_ADDR_REACHABLE) != SCTP_ADDR_REACHABLE) ||
425163953Srrs			    (mnet->dest_state & SCTP_ADDR_UNCONFIRMED)
426163953Srrs			    ) {
427163953Srrs				/*
428163953Srrs				 * will skip ones that are not-reachable or
429163953Srrs				 * unconfirmed
430163953Srrs				 */
431163953Srrs				continue;
432163953Srrs			}
433171440Srrs			if (max_cwnd < mnet->cwnd) {
434171440Srrs				max_cwnd_net = mnet;
435171440Srrs				max_cwnd = mnet->cwnd;
436171440Srrs			} else if (max_cwnd == mnet->cwnd) {
437163953Srrs				uint32_t rndval;
438163953Srrs				uint8_t this_random;
439163953Srrs
440163953Srrs				if (stcb->asoc.hb_random_idx > 3) {
441163953Srrs					rndval = sctp_select_initial_TSN(&stcb->sctp_ep->sctp_ep);
442163953Srrs					memcpy(stcb->asoc.hb_random_values, &rndval,
443163953Srrs					    sizeof(stcb->asoc.hb_random_values));
444163953Srrs					this_random = stcb->asoc.hb_random_values[0];
445163953Srrs					stcb->asoc.hb_random_idx = 0;
446163953Srrs					stcb->asoc.hb_ect_randombit = 0;
447163953Srrs				} else {
448163953Srrs					this_random = stcb->asoc.hb_random_values[stcb->asoc.hb_random_idx];
449163953Srrs					stcb->asoc.hb_random_idx++;
450163953Srrs					stcb->asoc.hb_ect_randombit = 0;
451163953Srrs				}
452163953Srrs				if (this_random % 2) {
453171440Srrs					max_cwnd_net = mnet;
454171440Srrs					max_cwnd = mnet->cwnd;
455163953Srrs				}
456163953Srrs			}
457163953Srrs		}
458171440Srrs		if (max_cwnd_net) {
459171440Srrs			return (max_cwnd_net);
460163953Srrs		}
461163953Srrs	}
462163953Srrs	mnet = net;
463163953Srrs	once = 0;
464163953Srrs
465163953Srrs	if (mnet == NULL) {
466163953Srrs		mnet = TAILQ_FIRST(&stcb->asoc.nets);
467163953Srrs	}
468163953Srrs	do {
469163953Srrs		alt = TAILQ_NEXT(mnet, sctp_next);
470163953Srrs		if (alt == NULL) {
471163953Srrs			once++;
472163953Srrs			if (once > 1) {
473163953Srrs				break;
474163953Srrs			}
475163953Srrs			alt = TAILQ_FIRST(&stcb->asoc.nets);
476163953Srrs		}
477163953Srrs		if (alt->ro.ro_rt == NULL) {
478167598Srrs			if (alt->ro._s_addr) {
479167598Srrs				sctp_free_ifa(alt->ro._s_addr);
480167598Srrs				alt->ro._s_addr = NULL;
481169352Srrs
482167598Srrs			}
483163953Srrs			alt->src_addr_selected = 0;
484163953Srrs		}
485163953Srrs		if (
486163953Srrs		    ((alt->dest_state & SCTP_ADDR_REACHABLE) == SCTP_ADDR_REACHABLE) &&
487163953Srrs		    (alt->ro.ro_rt != NULL) &&
488169655Srrs		/* sa_ignore NO_NULL_CHK */
489163953Srrs		    (!(alt->dest_state & SCTP_ADDR_UNCONFIRMED))
490163953Srrs		    ) {
491163953Srrs			/* Found a reachable address */
492163953Srrs			break;
493163953Srrs		}
494163953Srrs		mnet = alt;
495163953Srrs	} while (alt != NULL);
496163953Srrs
497163953Srrs	if (alt == NULL) {
498163953Srrs		/* Case where NO insv network exists (dormant state) */
499163953Srrs		/* we rotate destinations */
500163953Srrs		once = 0;
501163953Srrs		mnet = net;
502163953Srrs		do {
503163953Srrs			alt = TAILQ_NEXT(mnet, sctp_next);
504163953Srrs			if (alt == NULL) {
505163953Srrs				once++;
506163953Srrs				if (once > 1) {
507163953Srrs					break;
508163953Srrs				}
509163953Srrs				alt = TAILQ_FIRST(&stcb->asoc.nets);
510163953Srrs			}
511169655Srrs			/* sa_ignore NO_NULL_CHK */
512163953Srrs			if ((!(alt->dest_state & SCTP_ADDR_UNCONFIRMED)) &&
513163953Srrs			    (alt != net)) {
514163953Srrs				/* Found an alternate address */
515163953Srrs				break;
516163953Srrs			}
517163953Srrs			mnet = alt;
518163953Srrs		} while (alt != NULL);
519163953Srrs	}
520163953Srrs	if (alt == NULL) {
521163953Srrs		return (net);
522163953Srrs	}
523163953Srrs	return (alt);
524163953Srrs}
525163953Srrs
526171440Srrs
527171440Srrs
528163953Srrsstatic void
529163953Srrssctp_backoff_on_timeout(struct sctp_tcb *stcb,
530163953Srrs    struct sctp_nets *net,
531163953Srrs    int win_probe,
532163953Srrs    int num_marked)
533163953Srrs{
534170642Srrs	if (net->RTO == 0) {
535170642Srrs		net->RTO = stcb->asoc.minrto;
536170642Srrs	}
537163953Srrs	net->RTO <<= 1;
538163953Srrs	if (net->RTO > stcb->asoc.maxrto) {
539163953Srrs		net->RTO = stcb->asoc.maxrto;
540163953Srrs	}
541163953Srrs	if ((win_probe == 0) && num_marked) {
542163953Srrs		/* We don't apply penalty to window probe scenarios */
543171440Srrs		/* JRS - Use the congestion control given in the CC module */
544171440Srrs		stcb->asoc.cc_functions.sctp_cwnd_update_after_timeout(stcb, net);
545163953Srrs	}
546163953Srrs}
547163953Srrs
548163953Srrsstatic int
549163953Srrssctp_mark_all_for_resend(struct sctp_tcb *stcb,
550163953Srrs    struct sctp_nets *net,
551163953Srrs    struct sctp_nets *alt,
552163953Srrs    int window_probe,
553163953Srrs    int *num_marked)
554163953Srrs{
555163953Srrs
556163953Srrs	/*
557163953Srrs	 * Mark all chunks (well not all) that were sent to *net for
558163953Srrs	 * retransmission. Move them to alt for there destination as well...
559163953Srrs	 * We only mark chunks that have been outstanding long enough to
560163953Srrs	 * have received feed-back.
561163953Srrs	 */
562163953Srrs	struct sctp_tmit_chunk *chk, *tp2, *could_be_sent = NULL;
563163953Srrs	struct sctp_nets *lnets;
564163953Srrs	struct timeval now, min_wait, tv;
565163953Srrs	int cur_rtt;
566168709Srrs	int audit_tf, num_mk, fir;
567163953Srrs	unsigned int cnt_mk;
568168709Srrs	uint32_t orig_flight, orig_tf;
569163953Srrs	uint32_t tsnlast, tsnfirst;
570163953Srrs
571171440Srrs
572163953Srrs	/* none in flight now */
573163953Srrs	audit_tf = 0;
574163953Srrs	fir = 0;
575163953Srrs	/*
576163953Srrs	 * figure out how long a data chunk must be pending before we can
577163953Srrs	 * mark it ..
578163953Srrs	 */
579169378Srrs	(void)SCTP_GETTIME_TIMEVAL(&now);
580163953Srrs	/* get cur rto in micro-seconds */
581163953Srrs	cur_rtt = (((net->lastsa >> 2) + net->lastsv) >> 1);
582163953Srrs	cur_rtt *= 1000;
583170744Srrs	if (sctp_logging_level & (SCTP_EARLYFR_LOGGING_ENABLE | SCTP_FR_LOGGING_ENABLE)) {
584170744Srrs		sctp_log_fr(cur_rtt,
585170744Srrs		    stcb->asoc.peers_rwnd,
586170744Srrs		    window_probe,
587170744Srrs		    SCTP_FR_T3_MARK_TIME);
588170744Srrs		sctp_log_fr(net->flight_size,
589170744Srrs		    SCTP_OS_TIMER_PENDING(&net->fr_timer.timer),
590170744Srrs		    SCTP_OS_TIMER_ACTIVE(&net->fr_timer.timer),
591170744Srrs		    SCTP_FR_CWND_REPORT);
592170744Srrs		sctp_log_fr(net->flight_size, net->cwnd, stcb->asoc.total_flight, SCTP_FR_CWND_REPORT);
593170744Srrs	}
594163953Srrs	tv.tv_sec = cur_rtt / 1000000;
595163953Srrs	tv.tv_usec = cur_rtt % 1000000;
596163953Srrs	min_wait = now;
597163953Srrs	timevalsub(&min_wait, &tv);
598163953Srrs	if (min_wait.tv_sec < 0 || min_wait.tv_usec < 0) {
599163953Srrs		/*
600163953Srrs		 * if we hit here, we don't have enough seconds on the clock
601163953Srrs		 * to account for the RTO. We just let the lower seconds be
602163953Srrs		 * the bounds and don't worry about it. This may mean we
603163953Srrs		 * will mark a lot more than we should.
604163953Srrs		 */
605163953Srrs		min_wait.tv_sec = min_wait.tv_usec = 0;
606163953Srrs	}
607170744Srrs	if (sctp_logging_level & (SCTP_EARLYFR_LOGGING_ENABLE | SCTP_FR_LOGGING_ENABLE)) {
608170744Srrs		sctp_log_fr(cur_rtt, now.tv_sec, now.tv_usec, SCTP_FR_T3_MARK_TIME);
609170744Srrs		sctp_log_fr(0, min_wait.tv_sec, min_wait.tv_usec, SCTP_FR_T3_MARK_TIME);
610170744Srrs	}
611163953Srrs	/*
612163953Srrs	 * Our rwnd will be incorrect here since we are not adding back the
613163953Srrs	 * cnt * mbuf but we will fix that down below.
614163953Srrs	 */
615163953Srrs	orig_flight = net->flight_size;
616168709Srrs	orig_tf = stcb->asoc.total_flight;
617168709Srrs
618163953Srrs	net->fast_retran_ip = 0;
619163953Srrs	/* Now on to each chunk */
620163953Srrs	num_mk = cnt_mk = 0;
621163953Srrs	tsnfirst = tsnlast = 0;
622163953Srrs	chk = TAILQ_FIRST(&stcb->asoc.sent_queue);
623163953Srrs	for (; chk != NULL; chk = tp2) {
624163953Srrs		tp2 = TAILQ_NEXT(chk, sctp_next);
625163953Srrs		if ((compare_with_wrap(stcb->asoc.last_acked_seq,
626163953Srrs		    chk->rec.data.TSN_seq,
627163953Srrs		    MAX_TSN)) ||
628163953Srrs		    (stcb->asoc.last_acked_seq == chk->rec.data.TSN_seq)) {
629163953Srrs			/* Strange case our list got out of order? */
630169420Srrs			SCTP_PRINTF("Our list is out of order?\n");
631163953Srrs			panic("Out of order list");
632163953Srrs		}
633163953Srrs		if ((chk->whoTo == net) && (chk->sent < SCTP_DATAGRAM_ACKED)) {
634163953Srrs			/*
635163953Srrs			 * found one to mark: If it is less than
636163953Srrs			 * DATAGRAM_ACKED it MUST not be a skipped or marked
637163953Srrs			 * TSN but instead one that is either already set
638163953Srrs			 * for retransmission OR one that needs
639163953Srrs			 * retransmission.
640163953Srrs			 */
641163953Srrs
642163953Srrs			/* validate its been outstanding long enough */
643170744Srrs			if (sctp_logging_level & (SCTP_EARLYFR_LOGGING_ENABLE | SCTP_FR_LOGGING_ENABLE)) {
644170744Srrs				sctp_log_fr(chk->rec.data.TSN_seq,
645170744Srrs				    chk->sent_rcv_time.tv_sec,
646170744Srrs				    chk->sent_rcv_time.tv_usec,
647170744Srrs				    SCTP_FR_T3_MARK_TIME);
648170744Srrs			}
649163953Srrs			if ((chk->sent_rcv_time.tv_sec > min_wait.tv_sec) && (window_probe == 0)) {
650163953Srrs				/*
651163953Srrs				 * we have reached a chunk that was sent
652163953Srrs				 * some seconds past our min.. forget it we
653163953Srrs				 * will find no more to send.
654163953Srrs				 */
655170744Srrs				if (sctp_logging_level & (SCTP_EARLYFR_LOGGING_ENABLE | SCTP_FR_LOGGING_ENABLE)) {
656170744Srrs					sctp_log_fr(0,
657170744Srrs					    chk->sent_rcv_time.tv_sec,
658170744Srrs					    chk->sent_rcv_time.tv_usec,
659170744Srrs					    SCTP_FR_T3_STOPPED);
660170744Srrs				}
661163953Srrs				continue;
662163953Srrs			} else if ((chk->sent_rcv_time.tv_sec == min_wait.tv_sec) &&
663163953Srrs			    (window_probe == 0)) {
664163953Srrs				/*
665163953Srrs				 * we must look at the micro seconds to
666163953Srrs				 * know.
667163953Srrs				 */
668163953Srrs				if (chk->sent_rcv_time.tv_usec >= min_wait.tv_usec) {
669163953Srrs					/*
670163953Srrs					 * ok it was sent after our boundary
671163953Srrs					 * time.
672163953Srrs					 */
673170744Srrs					if (sctp_logging_level & (SCTP_EARLYFR_LOGGING_ENABLE | SCTP_FR_LOGGING_ENABLE)) {
674170744Srrs						sctp_log_fr(0,
675170744Srrs						    chk->sent_rcv_time.tv_sec,
676170744Srrs						    chk->sent_rcv_time.tv_usec,
677170744Srrs						    SCTP_FR_T3_STOPPED);
678170744Srrs					}
679163953Srrs					continue;
680163953Srrs				}
681163953Srrs			}
682163953Srrs			if (PR_SCTP_TTL_ENABLED(chk->flags)) {
683163953Srrs				/* Is it expired? */
684163953Srrs				if ((now.tv_sec > chk->rec.data.timetodrop.tv_sec) ||
685163953Srrs				    ((chk->rec.data.timetodrop.tv_sec == now.tv_sec) &&
686163953Srrs				    (now.tv_usec > chk->rec.data.timetodrop.tv_usec))) {
687163953Srrs					/* Yes so drop it */
688163953Srrs					if (chk->data) {
689169420Srrs						(void)sctp_release_pr_sctp_chunk(stcb,
690163953Srrs						    chk,
691163953Srrs						    (SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_SENT),
692163953Srrs						    &stcb->asoc.sent_queue);
693163953Srrs					}
694163953Srrs				}
695163953Srrs				continue;
696163953Srrs			}
697163953Srrs			if (PR_SCTP_RTX_ENABLED(chk->flags)) {
698163953Srrs				/* Has it been retransmitted tv_sec times? */
699163953Srrs				if (chk->snd_count > chk->rec.data.timetodrop.tv_sec) {
700163953Srrs					if (chk->data) {
701169420Srrs						(void)sctp_release_pr_sctp_chunk(stcb,
702163953Srrs						    chk,
703163953Srrs						    (SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_SENT),
704163953Srrs						    &stcb->asoc.sent_queue);
705163953Srrs					}
706163953Srrs				}
707163953Srrs				continue;
708163953Srrs			}
709168709Srrs			if (chk->sent < SCTP_DATAGRAM_RESEND) {
710163953Srrs				sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
711163953Srrs				num_mk++;
712163953Srrs				if (fir == 0) {
713163953Srrs					fir = 1;
714163953Srrs					tsnfirst = chk->rec.data.TSN_seq;
715163953Srrs				}
716163953Srrs				tsnlast = chk->rec.data.TSN_seq;
717170744Srrs				if (sctp_logging_level & (SCTP_EARLYFR_LOGGING_ENABLE | SCTP_FR_LOGGING_ENABLE)) {
718170744Srrs					sctp_log_fr(chk->rec.data.TSN_seq, chk->snd_count,
719170744Srrs					    0, SCTP_FR_T3_MARKED);
720170744Srrs				}
721168709Srrs				if (chk->rec.data.chunk_was_revoked) {
722168709Srrs					/* deflate the cwnd */
723168709Srrs					chk->whoTo->cwnd -= chk->book_size;
724168709Srrs					chk->rec.data.chunk_was_revoked = 0;
725168709Srrs				}
726168709Srrs				net->marked_retrans++;
727168709Srrs				stcb->asoc.marked_retrans++;
728170744Srrs				if (sctp_logging_level & SCTP_FLIGHT_LOGGING_ENABLE) {
729170744Srrs					sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_RSND_TO,
730170744Srrs					    chk->whoTo->flight_size,
731170744Srrs					    chk->book_size,
732170744Srrs					    (uintptr_t) chk->whoTo,
733170744Srrs					    chk->rec.data.TSN_seq);
734170744Srrs				}
735168709Srrs				sctp_flight_size_decrease(chk);
736168709Srrs				sctp_total_flight_decrease(stcb, chk);
737168709Srrs				stcb->asoc.peers_rwnd += chk->send_size;
738168709Srrs				stcb->asoc.peers_rwnd += sctp_peer_chunk_oh;
739163953Srrs			}
740163953Srrs			chk->sent = SCTP_DATAGRAM_RESEND;
741163953Srrs			SCTP_STAT_INCR(sctps_markedretrans);
742165220Srrs
743163953Srrs			/* reset the TSN for striking and other FR stuff */
744163953Srrs			chk->rec.data.doing_fast_retransmit = 0;
745163953Srrs			/* Clear any time so NO RTT is being done */
746163953Srrs			chk->do_rtt = 0;
747163953Srrs			if (alt != net) {
748163953Srrs				sctp_free_remote_addr(chk->whoTo);
749163953Srrs				chk->no_fr_allowed = 1;
750163953Srrs				chk->whoTo = alt;
751163953Srrs				atomic_add_int(&alt->ref_count, 1);
752163953Srrs			} else {
753163953Srrs				chk->no_fr_allowed = 0;
754163953Srrs				if (TAILQ_EMPTY(&stcb->asoc.send_queue)) {
755163953Srrs					chk->rec.data.fast_retran_tsn = stcb->asoc.sending_seq;
756163953Srrs				} else {
757163953Srrs					chk->rec.data.fast_retran_tsn = (TAILQ_FIRST(&stcb->asoc.send_queue))->rec.data.TSN_seq;
758163953Srrs				}
759163953Srrs			}
760170181Srrs			/*
761170181Srrs			 * CMT: Do not allow FRs on retransmitted TSNs.
762170181Srrs			 */
763163953Srrs			if (sctp_cmt_on_off == 1) {
764163953Srrs				chk->no_fr_allowed = 1;
765163953Srrs			}
766163953Srrs		} else if (chk->sent == SCTP_DATAGRAM_ACKED) {
767163953Srrs			/* remember highest acked one */
768163953Srrs			could_be_sent = chk;
769163953Srrs		}
770163953Srrs		if (chk->sent == SCTP_DATAGRAM_RESEND) {
771163953Srrs			cnt_mk++;
772163953Srrs		}
773163953Srrs	}
774168709Srrs	if ((orig_flight - net->flight_size) != (orig_tf - stcb->asoc.total_flight)) {
775168709Srrs		/* we did not subtract the same things? */
776168709Srrs		audit_tf = 1;
777168709Srrs	}
778170744Srrs	if (sctp_logging_level & (SCTP_EARLYFR_LOGGING_ENABLE | SCTP_FR_LOGGING_ENABLE)) {
779170744Srrs		sctp_log_fr(tsnfirst, tsnlast, num_mk, SCTP_FR_T3_TIMEOUT);
780170744Srrs	}
781163953Srrs#ifdef SCTP_DEBUG
782169420Srrs	if (num_mk) {
783169420Srrs		SCTPDBG(SCTP_DEBUG_TIMER1, "LAST TSN marked was %x\n",
784169420Srrs		    tsnlast);
785169420Srrs		SCTPDBG(SCTP_DEBUG_TIMER1, "Num marked for retransmission was %d peer-rwd:%ld\n",
786169420Srrs		    num_mk, (u_long)stcb->asoc.peers_rwnd);
787169420Srrs		SCTPDBG(SCTP_DEBUG_TIMER1, "LAST TSN marked was %x\n",
788169420Srrs		    tsnlast);
789169420Srrs		SCTPDBG(SCTP_DEBUG_TIMER1, "Num marked for retransmission was %d peer-rwd:%d\n",
790169420Srrs		    num_mk,
791169420Srrs		    (int)stcb->asoc.peers_rwnd);
792163953Srrs	}
793163953Srrs#endif
794163953Srrs	*num_marked = num_mk;
795163953Srrs	if ((stcb->asoc.sent_queue_retran_cnt == 0) && (could_be_sent)) {
796163953Srrs		/* fix it so we retransmit the highest acked anyway */
797163953Srrs		sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
798163953Srrs		cnt_mk++;
799163953Srrs		could_be_sent->sent = SCTP_DATAGRAM_RESEND;
800163953Srrs	}
801163953Srrs	if (stcb->asoc.sent_queue_retran_cnt != cnt_mk) {
802165220Srrs#ifdef INVARIANTS
803171477Srrs		SCTP_PRINTF("Local Audit says there are %d for retran asoc cnt:%d we marked:%d this time\n",
804171477Srrs		    cnt_mk, stcb->asoc.sent_queue_retran_cnt, num_mk);
805163953Srrs#endif
806163953Srrs#ifndef SCTP_AUDITING_ENABLED
807163953Srrs		stcb->asoc.sent_queue_retran_cnt = cnt_mk;
808163953Srrs#endif
809163953Srrs	}
810163953Srrs	/* Now check for a ECN Echo that may be stranded */
811163953Srrs	TAILQ_FOREACH(chk, &stcb->asoc.control_send_queue, sctp_next) {
812163953Srrs		if ((chk->whoTo == net) &&
813163953Srrs		    (chk->rec.chunk_id.id == SCTP_ECN_ECHO)) {
814163953Srrs			sctp_free_remote_addr(chk->whoTo);
815163953Srrs			chk->whoTo = alt;
816163953Srrs			if (chk->sent != SCTP_DATAGRAM_RESEND) {
817163953Srrs				chk->sent = SCTP_DATAGRAM_RESEND;
818163953Srrs				sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
819163953Srrs			}
820163953Srrs			atomic_add_int(&alt->ref_count, 1);
821163953Srrs		}
822163953Srrs	}
823163953Srrs	if (audit_tf) {
824169420Srrs		SCTPDBG(SCTP_DEBUG_TIMER4,
825169420Srrs		    "Audit total flight due to negative value net:%p\n",
826169420Srrs		    net);
827163953Srrs		stcb->asoc.total_flight = 0;
828163953Srrs		stcb->asoc.total_flight_count = 0;
829163953Srrs		/* Clear all networks flight size */
830163953Srrs		TAILQ_FOREACH(lnets, &stcb->asoc.nets, sctp_next) {
831163953Srrs			lnets->flight_size = 0;
832169420Srrs			SCTPDBG(SCTP_DEBUG_TIMER4,
833169420Srrs			    "Net:%p c-f cwnd:%d ssthresh:%d\n",
834169420Srrs			    lnets, lnets->cwnd, lnets->ssthresh);
835163953Srrs		}
836163953Srrs		TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
837163953Srrs			if (chk->sent < SCTP_DATAGRAM_RESEND) {
838170744Srrs				if (sctp_logging_level & SCTP_FLIGHT_LOGGING_ENABLE) {
839170744Srrs					sctp_misc_ints(SCTP_FLIGHT_LOG_UP,
840170744Srrs					    chk->whoTo->flight_size,
841170744Srrs					    chk->book_size,
842170744Srrs					    (uintptr_t) chk->whoTo,
843170744Srrs					    chk->rec.data.TSN_seq);
844170744Srrs				}
845168709Srrs				sctp_flight_size_increase(chk);
846168709Srrs				sctp_total_flight_increase(stcb, chk);
847163953Srrs			}
848163953Srrs		}
849163953Srrs	}
850163953Srrs	/*
851163953Srrs	 * Setup the ecn nonce re-sync point. We do this since
852163953Srrs	 * retranmissions are NOT setup for ECN. This means that do to
853163953Srrs	 * Karn's rule, we don't know the total of the peers ecn bits.
854163953Srrs	 */
855163953Srrs	chk = TAILQ_FIRST(&stcb->asoc.send_queue);
856163953Srrs	if (chk == NULL) {
857163953Srrs		stcb->asoc.nonce_resync_tsn = stcb->asoc.sending_seq;
858163953Srrs	} else {
859163953Srrs		stcb->asoc.nonce_resync_tsn = chk->rec.data.TSN_seq;
860163953Srrs	}
861163953Srrs	stcb->asoc.nonce_wait_for_ecne = 0;
862163953Srrs	stcb->asoc.nonce_sum_check = 0;
863163953Srrs	/* We return 1 if we only have a window probe outstanding */
864163953Srrs	return (0);
865163953Srrs}
866163953Srrs
867163953Srrsstatic void
868163953Srrssctp_move_all_chunks_to_alt(struct sctp_tcb *stcb,
869163953Srrs    struct sctp_nets *net,
870163953Srrs    struct sctp_nets *alt)
871163953Srrs{
872163953Srrs	struct sctp_association *asoc;
873163953Srrs	struct sctp_stream_out *outs;
874163953Srrs	struct sctp_tmit_chunk *chk;
875163953Srrs	struct sctp_stream_queue_pending *sp;
876163953Srrs
877163953Srrs	if (net == alt)
878163953Srrs		/* nothing to do */
879163953Srrs		return;
880163953Srrs
881163953Srrs	asoc = &stcb->asoc;
882163953Srrs
883163953Srrs	/*
884163953Srrs	 * now through all the streams checking for chunks sent to our bad
885163953Srrs	 * network.
886163953Srrs	 */
887163953Srrs	TAILQ_FOREACH(outs, &asoc->out_wheel, next_spoke) {
888163953Srrs		/* now clean up any chunks here */
889163953Srrs		TAILQ_FOREACH(sp, &outs->outqueue, next) {
890163953Srrs			if (sp->net == net) {
891163953Srrs				sctp_free_remote_addr(sp->net);
892163953Srrs				sp->net = alt;
893163953Srrs				atomic_add_int(&alt->ref_count, 1);
894163953Srrs			}
895163953Srrs		}
896163953Srrs	}
897163953Srrs	/* Now check the pending queue */
898163953Srrs	TAILQ_FOREACH(chk, &asoc->send_queue, sctp_next) {
899163953Srrs		if (chk->whoTo == net) {
900163953Srrs			sctp_free_remote_addr(chk->whoTo);
901163953Srrs			chk->whoTo = alt;
902163953Srrs			atomic_add_int(&alt->ref_count, 1);
903163953Srrs		}
904163953Srrs	}
905163953Srrs
906163953Srrs}
907163953Srrs
908163953Srrsint
909163953Srrssctp_t3rxt_timer(struct sctp_inpcb *inp,
910163953Srrs    struct sctp_tcb *stcb,
911163953Srrs    struct sctp_nets *net)
912163953Srrs{
913163953Srrs	struct sctp_nets *alt;
914163953Srrs	int win_probe, num_mk;
915163953Srrs
916170744Srrs	if (sctp_logging_level & SCTP_FR_LOGGING_ENABLE) {
917170744Srrs		sctp_log_fr(0, 0, 0, SCTP_FR_T3_TIMEOUT);
918170744Srrs	}
919170744Srrs	if (sctp_logging_level & SCTP_CWND_LOGGING_ENABLE) {
920163953Srrs		struct sctp_nets *lnet;
921163953Srrs
922163953Srrs		TAILQ_FOREACH(lnet, &stcb->asoc.nets, sctp_next) {
923163953Srrs			if (net == lnet) {
924163953Srrs				sctp_log_cwnd(stcb, lnet, 1, SCTP_CWND_LOG_FROM_T3);
925163953Srrs			} else {
926163953Srrs				sctp_log_cwnd(stcb, lnet, 0, SCTP_CWND_LOG_FROM_T3);
927163953Srrs			}
928163953Srrs		}
929163953Srrs	}
930163953Srrs	/* Find an alternate and mark those for retransmission */
931163953Srrs	if ((stcb->asoc.peers_rwnd == 0) &&
932163953Srrs	    (stcb->asoc.total_flight < net->mtu)) {
933163953Srrs		SCTP_STAT_INCR(sctps_timowindowprobe);
934163953Srrs		win_probe = 1;
935163953Srrs	} else {
936163953Srrs		win_probe = 0;
937163953Srrs	}
938168709Srrs
939171440Srrs	/*
940171440Srrs	 * JRS 5/14/07 - If CMT PF is on and the destination if not already
941171440Srrs	 * in PF state, set the destination to PF state and store the
942171440Srrs	 * current time as the time that the destination was last active. In
943171440Srrs	 * addition, find an alternate destination with PF-based
944171440Srrs	 * find_alt_net().
945171440Srrs	 */
946171477Srrs	if (sctp_cmt_on_off && sctp_cmt_pf) {
947171440Srrs		if ((net->dest_state & SCTP_ADDR_PF) != SCTP_ADDR_PF) {
948171440Srrs			net->dest_state |= SCTP_ADDR_PF;
949171477Srrs			net->last_active = sctp_get_tick_count();
950171440Srrs			SCTPDBG(SCTP_DEBUG_TIMER4, "Destination %p moved from active to PF.\n",
951171440Srrs			    net);
952171440Srrs		}
953171440Srrs		alt = sctp_find_alternate_net(stcb, net, 2);
954171440Srrs	} else if (sctp_cmt_on_off) {
955168709Srrs		/*
956168709Srrs		 * CMT: Using RTX_SSTHRESH policy for CMT. If CMT is being
957168709Srrs		 * used, then pick dest with largest ssthresh for any
958168709Srrs		 * retransmission.
959168709Srrs		 */
960168709Srrs		alt = net;
961168709Srrs		alt = sctp_find_alternate_net(stcb, alt, 1);
962168709Srrs		/*
963168709Srrs		 * CUCv2: If a different dest is picked for the
964168709Srrs		 * retransmission, then new (rtx-)pseudo_cumack needs to be
965168709Srrs		 * tracked for orig dest. Let CUCv2 track new (rtx-)
966168709Srrs		 * pseudo-cumack always.
967168709Srrs		 */
968168709Srrs		net->find_pseudo_cumack = 1;
969168709Srrs		net->find_rtx_pseudo_cumack = 1;
970168709Srrs
971168709Srrs	} else {		/* CMT is OFF */
972168709Srrs		alt = sctp_find_alternate_net(stcb, net, 0);
973168709Srrs	}
974168709Srrs
975169420Srrs	(void)sctp_mark_all_for_resend(stcb, net, alt, win_probe, &num_mk);
976163953Srrs	/* FR Loss recovery just ended with the T3. */
977163953Srrs	stcb->asoc.fast_retran_loss_recovery = 0;
978163953Srrs
979163953Srrs	/* CMT FR loss recovery ended with the T3 */
980163953Srrs	net->fast_retran_loss_recovery = 0;
981163953Srrs
982163953Srrs	/*
983163953Srrs	 * setup the sat loss recovery that prevents satellite cwnd advance.
984163953Srrs	 */
985163953Srrs	stcb->asoc.sat_t3_loss_recovery = 1;
986163953Srrs	stcb->asoc.sat_t3_recovery_tsn = stcb->asoc.sending_seq;
987163953Srrs
988163953Srrs	/* Backoff the timer and cwnd */
989163953Srrs	sctp_backoff_on_timeout(stcb, net, win_probe, num_mk);
990163953Srrs	if (win_probe == 0) {
991163953Srrs		/* We don't do normal threshold management on window probes */
992163953Srrs		if (sctp_threshold_management(inp, stcb, net,
993163953Srrs		    stcb->asoc.max_send_times)) {
994163953Srrs			/* Association was destroyed */
995163953Srrs			return (1);
996163953Srrs		} else {
997163953Srrs			if (net != stcb->asoc.primary_destination) {
998163953Srrs				/* send a immediate HB if our RTO is stale */
999163953Srrs				struct timeval now;
1000163953Srrs				unsigned int ms_goneby;
1001163953Srrs
1002169378Srrs				(void)SCTP_GETTIME_TIMEVAL(&now);
1003163953Srrs				if (net->last_sent_time.tv_sec) {
1004163953Srrs					ms_goneby = (now.tv_sec - net->last_sent_time.tv_sec) * 1000;
1005163953Srrs				} else {
1006163953Srrs					ms_goneby = 0;
1007163953Srrs				}
1008163953Srrs				if ((ms_goneby > net->RTO) || (net->RTO == 0)) {
1009163953Srrs					/*
1010163953Srrs					 * no recent feed back in an RTO or
1011163953Srrs					 * more, request a RTT update
1012163953Srrs					 */
1013171440Srrs					if (sctp_send_hb(stcb, 1, net) < 0)
1014171440Srrs						return 1;
1015163953Srrs				}
1016163953Srrs			}
1017163953Srrs		}
1018163953Srrs	} else {
1019163953Srrs		/*
1020163953Srrs		 * For a window probe we don't penalize the net's but only
1021163953Srrs		 * the association. This may fail it if SACKs are not coming
1022163953Srrs		 * back. If sack's are coming with rwnd locked at 0, we will
1023163953Srrs		 * continue to hold things waiting for rwnd to raise
1024163953Srrs		 */
1025163953Srrs		if (sctp_threshold_management(inp, stcb, NULL,
1026163953Srrs		    stcb->asoc.max_send_times)) {
1027163953Srrs			/* Association was destroyed */
1028163953Srrs			return (1);
1029163953Srrs		}
1030163953Srrs	}
1031163953Srrs	if (net->dest_state & SCTP_ADDR_NOT_REACHABLE) {
1032163953Srrs		/* Move all pending over too */
1033163953Srrs		sctp_move_all_chunks_to_alt(stcb, net, alt);
1034169352Srrs
1035169352Srrs		/*
1036169352Srrs		 * Get the address that failed, to force a new src address
1037169352Srrs		 * selecton and a route allocation.
1038169352Srrs		 */
1039169352Srrs		if (net->ro._s_addr) {
1040169352Srrs			sctp_free_ifa(net->ro._s_addr);
1041169352Srrs			net->ro._s_addr = NULL;
1042169352Srrs		}
1043169352Srrs		net->src_addr_selected = 0;
1044169352Srrs
1045169352Srrs		/* Force a route allocation too */
1046169352Srrs		if (net->ro.ro_rt) {
1047169352Srrs			RTFREE(net->ro.ro_rt);
1048169352Srrs			net->ro.ro_rt = NULL;
1049169352Srrs		}
1050163953Srrs		/* Was it our primary? */
1051163953Srrs		if ((stcb->asoc.primary_destination == net) && (alt != net)) {
1052163953Srrs			/*
1053163953Srrs			 * Yes, note it as such and find an alternate note:
1054163953Srrs			 * this means HB code must use this to resent the
1055163953Srrs			 * primary if it goes active AND if someone does a
1056163953Srrs			 * change-primary then this flag must be cleared
1057163953Srrs			 * from any net structures.
1058163953Srrs			 */
1059163953Srrs			if (sctp_set_primary_addr(stcb,
1060163953Srrs			    (struct sockaddr *)NULL,
1061163953Srrs			    alt) == 0) {
1062163953Srrs				net->dest_state |= SCTP_ADDR_WAS_PRIMARY;
1063163953Srrs			}
1064163953Srrs		}
1065171477Srrs	} else if (sctp_cmt_on_off && sctp_cmt_pf && (net->dest_state & SCTP_ADDR_PF) == SCTP_ADDR_PF) {
1066171440Srrs		/*
1067171440Srrs		 * JRS 5/14/07 - If the destination hasn't failed completely
1068171440Srrs		 * but is in PF state, a PF-heartbeat needs to be sent
1069171440Srrs		 * manually.
1070171440Srrs		 */
1071171440Srrs		if (sctp_send_hb(stcb, 1, net) < 0)
1072171440Srrs			return 1;
1073163953Srrs	}
1074163953Srrs	/*
1075163953Srrs	 * Special case for cookie-echo'ed case, we don't do output but must
1076163953Srrs	 * await the COOKIE-ACK before retransmission
1077163953Srrs	 */
1078163953Srrs	if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_ECHOED) {
1079163953Srrs		/*
1080163953Srrs		 * Here we just reset the timer and start again since we
1081163953Srrs		 * have not established the asoc
1082163953Srrs		 */
1083163953Srrs		sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
1084163953Srrs		return (0);
1085163953Srrs	}
1086163953Srrs	if (stcb->asoc.peer_supports_prsctp) {
1087163953Srrs		struct sctp_tmit_chunk *lchk;
1088163953Srrs
1089163953Srrs		lchk = sctp_try_advance_peer_ack_point(stcb, &stcb->asoc);
1090163953Srrs		/* C3. See if we need to send a Fwd-TSN */
1091163953Srrs		if (compare_with_wrap(stcb->asoc.advanced_peer_ack_point,
1092163953Srrs		    stcb->asoc.last_acked_seq, MAX_TSN)) {
1093163953Srrs			/*
1094163953Srrs			 * ISSUE with ECN, see FWD-TSN processing for notes
1095163953Srrs			 * on issues that will occur when the ECN NONCE
1096163953Srrs			 * stuff is put into SCTP for cross checking.
1097163953Srrs			 */
1098163953Srrs			send_forward_tsn(stcb, &stcb->asoc);
1099163953Srrs			if (lchk) {
1100163953Srrs				/* Assure a timer is up */
1101163953Srrs				sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, stcb, lchk->whoTo);
1102163953Srrs			}
1103163953Srrs		}
1104163953Srrs	}
1105170744Srrs	if (sctp_logging_level & SCTP_CWND_MONITOR_ENABLE) {
1106170744Srrs		sctp_log_cwnd(stcb, net, net->cwnd, SCTP_CWND_LOG_FROM_RTX);
1107170744Srrs	}
1108163953Srrs	return (0);
1109163953Srrs}
1110163953Srrs
1111163953Srrsint
1112163953Srrssctp_t1init_timer(struct sctp_inpcb *inp,
1113163953Srrs    struct sctp_tcb *stcb,
1114163953Srrs    struct sctp_nets *net)
1115163953Srrs{
1116163953Srrs	/* bump the thresholds */
1117163953Srrs	if (stcb->asoc.delayed_connection) {
1118163953Srrs		/*
1119163953Srrs		 * special hook for delayed connection. The library did NOT
1120163953Srrs		 * complete the rest of its sends.
1121163953Srrs		 */
1122163953Srrs		stcb->asoc.delayed_connection = 0;
1123163953Srrs		sctp_send_initiate(inp, stcb);
1124163953Srrs		return (0);
1125163953Srrs	}
1126163953Srrs	if (SCTP_GET_STATE((&stcb->asoc)) != SCTP_STATE_COOKIE_WAIT) {
1127163953Srrs		return (0);
1128163953Srrs	}
1129163953Srrs	if (sctp_threshold_management(inp, stcb, net,
1130163953Srrs	    stcb->asoc.max_init_times)) {
1131163953Srrs		/* Association was destroyed */
1132163953Srrs		return (1);
1133163953Srrs	}
1134163953Srrs	stcb->asoc.dropped_special_cnt = 0;
1135163953Srrs	sctp_backoff_on_timeout(stcb, stcb->asoc.primary_destination, 1, 0);
1136163953Srrs	if (stcb->asoc.initial_init_rto_max < net->RTO) {
1137163953Srrs		net->RTO = stcb->asoc.initial_init_rto_max;
1138163953Srrs	}
1139163953Srrs	if (stcb->asoc.numnets > 1) {
1140163953Srrs		/* If we have more than one addr use it */
1141163953Srrs		struct sctp_nets *alt;
1142163953Srrs
1143163953Srrs		alt = sctp_find_alternate_net(stcb, stcb->asoc.primary_destination, 0);
1144163953Srrs		if ((alt != NULL) && (alt != stcb->asoc.primary_destination)) {
1145163953Srrs			sctp_move_all_chunks_to_alt(stcb, stcb->asoc.primary_destination, alt);
1146163953Srrs			stcb->asoc.primary_destination = alt;
1147163953Srrs		}
1148163953Srrs	}
1149163953Srrs	/* Send out a new init */
1150163953Srrs	sctp_send_initiate(inp, stcb);
1151163953Srrs	return (0);
1152163953Srrs}
1153163953Srrs
1154163953Srrs/*
1155163953Srrs * For cookie and asconf we actually need to find and mark for resend, then
1156163953Srrs * increment the resend counter (after all the threshold management stuff of
1157163953Srrs * course).
1158163953Srrs */
1159163953Srrsint
1160163953Srrssctp_cookie_timer(struct sctp_inpcb *inp,
1161163953Srrs    struct sctp_tcb *stcb,
1162163953Srrs    struct sctp_nets *net)
1163163953Srrs{
1164163953Srrs	struct sctp_nets *alt;
1165163953Srrs	struct sctp_tmit_chunk *cookie;
1166163953Srrs
1167163953Srrs	/* first before all else we must find the cookie */
1168163953Srrs	TAILQ_FOREACH(cookie, &stcb->asoc.control_send_queue, sctp_next) {
1169163953Srrs		if (cookie->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
1170163953Srrs			break;
1171163953Srrs		}
1172163953Srrs	}
1173163953Srrs	if (cookie == NULL) {
1174163953Srrs		if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_ECHOED) {
1175163953Srrs			/* FOOBAR! */
1176163953Srrs			struct mbuf *oper;
1177163953Srrs
1178163953Srrs			oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
1179163953Srrs			    0, M_DONTWAIT, 1, MT_DATA);
1180163953Srrs			if (oper) {
1181163953Srrs				struct sctp_paramhdr *ph;
1182163953Srrs				uint32_t *ippp;
1183163953Srrs
1184165647Srrs				SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
1185163953Srrs				    sizeof(uint32_t);
1186163953Srrs				ph = mtod(oper, struct sctp_paramhdr *);
1187163953Srrs				ph->param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1188165647Srrs				ph->param_length = htons(SCTP_BUF_LEN(oper));
1189163953Srrs				ippp = (uint32_t *) (ph + 1);
1190171440Srrs				*ippp = htonl(SCTP_FROM_SCTP_TIMER + SCTP_LOC_3);
1191163953Srrs			}
1192171440Srrs			inp->last_abort_code = SCTP_FROM_SCTP_TIMER + SCTP_LOC_4;
1193163953Srrs			sctp_abort_an_association(inp, stcb, SCTP_INTERNAL_ERROR,
1194163953Srrs			    oper);
1195163953Srrs		} else {
1196165220Srrs#ifdef INVARIANTS
1197163953Srrs			panic("Cookie timer expires in wrong state?");
1198163953Srrs#else
1199169420Srrs			SCTP_PRINTF("Strange in state %d not cookie-echoed yet c-e timer expires?\n", SCTP_GET_STATE(&stcb->asoc));
1200163953Srrs			return (0);
1201163953Srrs#endif
1202163953Srrs		}
1203163953Srrs		return (0);
1204163953Srrs	}
1205163953Srrs	/* Ok we found the cookie, threshold management next */
1206163953Srrs	if (sctp_threshold_management(inp, stcb, cookie->whoTo,
1207163953Srrs	    stcb->asoc.max_init_times)) {
1208163953Srrs		/* Assoc is over */
1209163953Srrs		return (1);
1210163953Srrs	}
1211163953Srrs	/*
1212163953Srrs	 * cleared theshold management now lets backoff the address & select
1213163953Srrs	 * an alternate
1214163953Srrs	 */
1215163953Srrs	stcb->asoc.dropped_special_cnt = 0;
1216163953Srrs	sctp_backoff_on_timeout(stcb, cookie->whoTo, 1, 0);
1217163953Srrs	alt = sctp_find_alternate_net(stcb, cookie->whoTo, 0);
1218163953Srrs	if (alt != cookie->whoTo) {
1219163953Srrs		sctp_free_remote_addr(cookie->whoTo);
1220163953Srrs		cookie->whoTo = alt;
1221163953Srrs		atomic_add_int(&alt->ref_count, 1);
1222163953Srrs	}
1223163953Srrs	/* Now mark the retran info */
1224163953Srrs	if (cookie->sent != SCTP_DATAGRAM_RESEND) {
1225163953Srrs		sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
1226163953Srrs	}
1227163953Srrs	cookie->sent = SCTP_DATAGRAM_RESEND;
1228163953Srrs	/*
1229163953Srrs	 * Now call the output routine to kick out the cookie again, Note we
1230163953Srrs	 * don't mark any chunks for retran so that FR will need to kick in
1231163953Srrs	 * to move these (or a send timer).
1232163953Srrs	 */
1233163953Srrs	return (0);
1234163953Srrs}
1235163953Srrs
1236163953Srrsint
1237163953Srrssctp_strreset_timer(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
1238163953Srrs    struct sctp_nets *net)
1239163953Srrs{
1240163953Srrs	struct sctp_nets *alt;
1241163953Srrs	struct sctp_tmit_chunk *strrst = NULL, *chk = NULL;
1242163953Srrs
1243163953Srrs	if (stcb->asoc.stream_reset_outstanding == 0) {
1244163953Srrs		return (0);
1245163953Srrs	}
1246163953Srrs	/* find the existing STRRESET, we use the seq number we sent out on */
1247169420Srrs	(void)sctp_find_stream_reset(stcb, stcb->asoc.str_reset_seq_out, &strrst);
1248163953Srrs	if (strrst == NULL) {
1249163953Srrs		return (0);
1250163953Srrs	}
1251163953Srrs	/* do threshold management */
1252163953Srrs	if (sctp_threshold_management(inp, stcb, strrst->whoTo,
1253163953Srrs	    stcb->asoc.max_send_times)) {
1254163953Srrs		/* Assoc is over */
1255163953Srrs		return (1);
1256163953Srrs	}
1257163953Srrs	/*
1258163953Srrs	 * cleared theshold management now lets backoff the address & select
1259163953Srrs	 * an alternate
1260163953Srrs	 */
1261163953Srrs	sctp_backoff_on_timeout(stcb, strrst->whoTo, 1, 0);
1262163953Srrs	alt = sctp_find_alternate_net(stcb, strrst->whoTo, 0);
1263163953Srrs	sctp_free_remote_addr(strrst->whoTo);
1264163953Srrs	strrst->whoTo = alt;
1265163953Srrs	atomic_add_int(&alt->ref_count, 1);
1266163953Srrs
1267163953Srrs	/* See if a ECN Echo is also stranded */
1268163953Srrs	TAILQ_FOREACH(chk, &stcb->asoc.control_send_queue, sctp_next) {
1269163953Srrs		if ((chk->whoTo == net) &&
1270163953Srrs		    (chk->rec.chunk_id.id == SCTP_ECN_ECHO)) {
1271163953Srrs			sctp_free_remote_addr(chk->whoTo);
1272163953Srrs			if (chk->sent != SCTP_DATAGRAM_RESEND) {
1273163953Srrs				chk->sent = SCTP_DATAGRAM_RESEND;
1274163953Srrs				sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
1275163953Srrs			}
1276163953Srrs			chk->whoTo = alt;
1277163953Srrs			atomic_add_int(&alt->ref_count, 1);
1278163953Srrs		}
1279163953Srrs	}
1280163953Srrs	if (net->dest_state & SCTP_ADDR_NOT_REACHABLE) {
1281163953Srrs		/*
1282163953Srrs		 * If the address went un-reachable, we need to move to
1283163953Srrs		 * alternates for ALL chk's in queue
1284163953Srrs		 */
1285163953Srrs		sctp_move_all_chunks_to_alt(stcb, net, alt);
1286163953Srrs	}
1287163953Srrs	/* mark the retran info */
1288163953Srrs	if (strrst->sent != SCTP_DATAGRAM_RESEND)
1289163953Srrs		sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
1290163953Srrs	strrst->sent = SCTP_DATAGRAM_RESEND;
1291163953Srrs
1292163953Srrs	/* restart the timer */
1293163953Srrs	sctp_timer_start(SCTP_TIMER_TYPE_STRRESET, inp, stcb, strrst->whoTo);
1294163953Srrs	return (0);
1295163953Srrs}
1296163953Srrs
1297163953Srrsint
1298163953Srrssctp_asconf_timer(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
1299163953Srrs    struct sctp_nets *net)
1300163953Srrs{
1301163953Srrs	struct sctp_nets *alt;
1302163953Srrs	struct sctp_tmit_chunk *asconf, *chk;
1303163953Srrs
1304171572Srrs	/* is this a first send, or a retransmission? */
1305163953Srrs	if (stcb->asoc.asconf_sent == 0) {
1306163953Srrs		/* compose a new ASCONF chunk and send it */
1307163953Srrs		sctp_send_asconf(stcb, net);
1308163953Srrs	} else {
1309171572Srrs		/*
1310171572Srrs		 * Retransmission of the existing ASCONF is needed
1311171572Srrs		 */
1312163953Srrs
1313163953Srrs		/* find the existing ASCONF */
1314163953Srrs		TAILQ_FOREACH(asconf, &stcb->asoc.control_send_queue,
1315163953Srrs		    sctp_next) {
1316163953Srrs			if (asconf->rec.chunk_id.id == SCTP_ASCONF) {
1317163953Srrs				break;
1318163953Srrs			}
1319163953Srrs		}
1320163953Srrs		if (asconf == NULL) {
1321163953Srrs			return (0);
1322163953Srrs		}
1323163953Srrs		/* do threshold management */
1324163953Srrs		if (sctp_threshold_management(inp, stcb, asconf->whoTo,
1325163953Srrs		    stcb->asoc.max_send_times)) {
1326163953Srrs			/* Assoc is over */
1327163953Srrs			return (1);
1328163953Srrs		}
1329163953Srrs		if (asconf->snd_count > stcb->asoc.max_send_times) {
1330163953Srrs			/*
1331171572Srrs			 * Something is rotten: our peer is not responding
1332171572Srrs			 * to ASCONFs but apparently is to other chunks.
1333171572Srrs			 * i.e. it is not properly handling the chunk type
1334171572Srrs			 * upper bits. Mark this peer as ASCONF incapable
1335171572Srrs			 * and cleanup.
1336163953Srrs			 */
1337169420Srrs			SCTPDBG(SCTP_DEBUG_TIMER1, "asconf_timer: Peer has not responded to our repeated ASCONFs\n");
1338163953Srrs			sctp_asconf_cleanup(stcb, net);
1339163953Srrs			return (0);
1340163953Srrs		}
1341163953Srrs		/*
1342171572Srrs		 * cleared threshold management, so now backoff the net and
1343171572Srrs		 * select an alternate
1344163953Srrs		 */
1345163953Srrs		sctp_backoff_on_timeout(stcb, asconf->whoTo, 1, 0);
1346163953Srrs		alt = sctp_find_alternate_net(stcb, asconf->whoTo, 0);
1347163953Srrs		sctp_free_remote_addr(asconf->whoTo);
1348163953Srrs		asconf->whoTo = alt;
1349163953Srrs		atomic_add_int(&alt->ref_count, 1);
1350163953Srrs
1351171572Srrs		/* See if an ECN Echo is also stranded */
1352163953Srrs		TAILQ_FOREACH(chk, &stcb->asoc.control_send_queue, sctp_next) {
1353163953Srrs			if ((chk->whoTo == net) &&
1354163953Srrs			    (chk->rec.chunk_id.id == SCTP_ECN_ECHO)) {
1355163953Srrs				sctp_free_remote_addr(chk->whoTo);
1356163953Srrs				chk->whoTo = alt;
1357163953Srrs				if (chk->sent != SCTP_DATAGRAM_RESEND) {
1358163953Srrs					chk->sent = SCTP_DATAGRAM_RESEND;
1359163953Srrs					sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
1360163953Srrs				}
1361163953Srrs				atomic_add_int(&alt->ref_count, 1);
1362163953Srrs			}
1363163953Srrs		}
1364163953Srrs		if (net->dest_state & SCTP_ADDR_NOT_REACHABLE) {
1365163953Srrs			/*
1366163953Srrs			 * If the address went un-reachable, we need to move
1367171572Srrs			 * to the alternate for ALL chunks in queue
1368163953Srrs			 */
1369163953Srrs			sctp_move_all_chunks_to_alt(stcb, net, alt);
1370163953Srrs		}
1371163953Srrs		/* mark the retran info */
1372163953Srrs		if (asconf->sent != SCTP_DATAGRAM_RESEND)
1373163953Srrs			sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
1374163953Srrs		asconf->sent = SCTP_DATAGRAM_RESEND;
1375163953Srrs	}
1376163953Srrs	return (0);
1377163953Srrs}
1378163953Srrs
1379163953Srrs/*
1380163953Srrs * For the shutdown and shutdown-ack, we do not keep one around on the
1381163953Srrs * control queue. This means we must generate a new one and call the general
1382163953Srrs * chunk output routine, AFTER having done threshold management.
1383163953Srrs */
1384163953Srrsint
1385163953Srrssctp_shutdown_timer(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
1386163953Srrs    struct sctp_nets *net)
1387163953Srrs{
1388163953Srrs	struct sctp_nets *alt;
1389163953Srrs
1390163953Srrs	/* first threshold managment */
1391163953Srrs	if (sctp_threshold_management(inp, stcb, net, stcb->asoc.max_send_times)) {
1392163953Srrs		/* Assoc is over */
1393163953Srrs		return (1);
1394163953Srrs	}
1395163953Srrs	/* second select an alternative */
1396163953Srrs	alt = sctp_find_alternate_net(stcb, net, 0);
1397163953Srrs
1398163953Srrs	/* third generate a shutdown into the queue for out net */
1399163953Srrs	if (alt) {
1400163953Srrs		sctp_send_shutdown(stcb, alt);
1401163953Srrs	} else {
1402163953Srrs		/*
1403163953Srrs		 * if alt is NULL, there is no dest to send to??
1404163953Srrs		 */
1405163953Srrs		return (0);
1406163953Srrs	}
1407163953Srrs	/* fourth restart timer */
1408163953Srrs	sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, inp, stcb, alt);
1409163953Srrs	return (0);
1410163953Srrs}
1411163953Srrs
1412163953Srrsint
1413163953Srrssctp_shutdownack_timer(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
1414163953Srrs    struct sctp_nets *net)
1415163953Srrs{
1416163953Srrs	struct sctp_nets *alt;
1417163953Srrs
1418163953Srrs	/* first threshold managment */
1419163953Srrs	if (sctp_threshold_management(inp, stcb, net, stcb->asoc.max_send_times)) {
1420163953Srrs		/* Assoc is over */
1421163953Srrs		return (1);
1422163953Srrs	}
1423163953Srrs	/* second select an alternative */
1424163953Srrs	alt = sctp_find_alternate_net(stcb, net, 0);
1425163953Srrs
1426163953Srrs	/* third generate a shutdown into the queue for out net */
1427163953Srrs	sctp_send_shutdown_ack(stcb, alt);
1428163953Srrs
1429163953Srrs	/* fourth restart timer */
1430163953Srrs	sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNACK, inp, stcb, alt);
1431163953Srrs	return (0);
1432163953Srrs}
1433163953Srrs
1434163953Srrsstatic void
1435163953Srrssctp_audit_stream_queues_for_size(struct sctp_inpcb *inp,
1436163953Srrs    struct sctp_tcb *stcb)
1437163953Srrs{
1438163953Srrs	struct sctp_stream_out *outs;
1439163953Srrs	struct sctp_stream_queue_pending *sp;
1440163953Srrs	unsigned int chks_in_queue = 0;
1441163953Srrs	int being_filled = 0;
1442163953Srrs
1443163953Srrs	/*
1444163953Srrs	 * This function is ONLY called when the send/sent queues are empty.
1445163953Srrs	 */
1446163953Srrs	if ((stcb == NULL) || (inp == NULL))
1447163953Srrs		return;
1448163953Srrs
1449163953Srrs	if (stcb->asoc.sent_queue_retran_cnt) {
1450169420Srrs		SCTP_PRINTF("Hmm, sent_queue_retran_cnt is non-zero %d\n",
1451163953Srrs		    stcb->asoc.sent_queue_retran_cnt);
1452163953Srrs		stcb->asoc.sent_queue_retran_cnt = 0;
1453163953Srrs	}
1454163953Srrs	SCTP_TCB_SEND_LOCK(stcb);
1455163953Srrs	if (TAILQ_EMPTY(&stcb->asoc.out_wheel)) {
1456163953Srrs		int i, cnt = 0;
1457163953Srrs
1458163953Srrs		/* Check to see if a spoke fell off the wheel */
1459163953Srrs		for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
1460163953Srrs			if (!TAILQ_EMPTY(&stcb->asoc.strmout[i].outqueue)) {
1461163953Srrs				sctp_insert_on_wheel(stcb, &stcb->asoc, &stcb->asoc.strmout[i], 1);
1462163953Srrs				cnt++;
1463163953Srrs			}
1464163953Srrs		}
1465163953Srrs		if (cnt) {
1466163953Srrs			/* yep, we lost a spoke or two */
1467169420Srrs			SCTP_PRINTF("Found an additional %d streams NOT on outwheel, corrected\n", cnt);
1468163953Srrs		} else {
1469163953Srrs			/* no spokes lost, */
1470163953Srrs			stcb->asoc.total_output_queue_size = 0;
1471163953Srrs		}
1472163953Srrs		SCTP_TCB_SEND_UNLOCK(stcb);
1473163953Srrs		return;
1474163953Srrs	}
1475163953Srrs	SCTP_TCB_SEND_UNLOCK(stcb);
1476163953Srrs	/* Check to see if some data queued, if so report it */
1477163953Srrs	TAILQ_FOREACH(outs, &stcb->asoc.out_wheel, next_spoke) {
1478163953Srrs		if (!TAILQ_EMPTY(&outs->outqueue)) {
1479163953Srrs			TAILQ_FOREACH(sp, &outs->outqueue, next) {
1480163953Srrs				if (sp->msg_is_complete)
1481163953Srrs					being_filled++;
1482163953Srrs				chks_in_queue++;
1483163953Srrs			}
1484163953Srrs		}
1485163953Srrs	}
1486163953Srrs	if (chks_in_queue != stcb->asoc.stream_queue_cnt) {
1487169420Srrs		SCTP_PRINTF("Hmm, stream queue cnt at %d I counted %d in stream out wheel\n",
1488163953Srrs		    stcb->asoc.stream_queue_cnt, chks_in_queue);
1489163953Srrs	}
1490163953Srrs	if (chks_in_queue) {
1491163953Srrs		/* call the output queue function */
1492163953Srrs		sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_T3);
1493163953Srrs		if ((TAILQ_EMPTY(&stcb->asoc.send_queue)) &&
1494163953Srrs		    (TAILQ_EMPTY(&stcb->asoc.sent_queue))) {
1495163953Srrs			/*
1496163953Srrs			 * Probably should go in and make it go back through
1497163953Srrs			 * and add fragments allowed
1498163953Srrs			 */
1499163953Srrs			if (being_filled == 0) {
1500169420Srrs				SCTP_PRINTF("Still nothing moved %d chunks are stuck\n",
1501163953Srrs				    chks_in_queue);
1502163953Srrs			}
1503163953Srrs		}
1504163953Srrs	} else {
1505169420Srrs		SCTP_PRINTF("Found no chunks on any queue tot:%lu\n",
1506163953Srrs		    (u_long)stcb->asoc.total_output_queue_size);
1507163953Srrs		stcb->asoc.total_output_queue_size = 0;
1508163953Srrs	}
1509163953Srrs}
1510163953Srrs
1511163953Srrsint
1512163953Srrssctp_heartbeat_timer(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
1513163953Srrs    struct sctp_nets *net, int cnt_of_unconf)
1514163953Srrs{
1515171440Srrs	int ret;
1516171440Srrs
1517163953Srrs	if (net) {
1518163953Srrs		if (net->hb_responded == 0) {
1519167598Srrs			if (net->ro._s_addr) {
1520167598Srrs				/*
1521167598Srrs				 * Invalidate the src address if we did not
1522167598Srrs				 * get a response last time.
1523167598Srrs				 */
1524167598Srrs				sctp_free_ifa(net->ro._s_addr);
1525167598Srrs				net->ro._s_addr = NULL;
1526167598Srrs				net->src_addr_selected = 0;
1527167598Srrs			}
1528163953Srrs			sctp_backoff_on_timeout(stcb, net, 1, 0);
1529163953Srrs		}
1530163953Srrs		/* Zero PBA, if it needs it */
1531163953Srrs		if (net->partial_bytes_acked) {
1532163953Srrs			net->partial_bytes_acked = 0;
1533163953Srrs		}
1534163953Srrs	}
1535163953Srrs	if ((stcb->asoc.total_output_queue_size > 0) &&
1536163953Srrs	    (TAILQ_EMPTY(&stcb->asoc.send_queue)) &&
1537163953Srrs	    (TAILQ_EMPTY(&stcb->asoc.sent_queue))) {
1538163953Srrs		sctp_audit_stream_queues_for_size(inp, stcb);
1539163953Srrs	}
1540163953Srrs	/* Send a new HB, this will do threshold managment, pick a new dest */
1541163953Srrs	if (cnt_of_unconf == 0) {
1542163953Srrs		if (sctp_send_hb(stcb, 0, NULL) < 0) {
1543163953Srrs			return (1);
1544163953Srrs		}
1545163953Srrs	} else {
1546163953Srrs		/*
1547163953Srrs		 * this will send out extra hb's up to maxburst if there are
1548163953Srrs		 * any unconfirmed addresses.
1549163953Srrs		 */
1550170056Srrs		uint32_t cnt_sent = 0;
1551163953Srrs
1552163953Srrs		TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1553163953Srrs			if ((net->dest_state & SCTP_ADDR_UNCONFIRMED) &&
1554163953Srrs			    (net->dest_state & SCTP_ADDR_REACHABLE)) {
1555163953Srrs				cnt_sent++;
1556167598Srrs				if (net->hb_responded == 0) {
1557167598Srrs					/* Did we respond last time? */
1558167598Srrs					if (net->ro._s_addr) {
1559167598Srrs						sctp_free_ifa(net->ro._s_addr);
1560167598Srrs						net->ro._s_addr = NULL;
1561167598Srrs						net->src_addr_selected = 0;
1562167598Srrs					}
1563167598Srrs				}
1564171440Srrs				ret = sctp_send_hb(stcb, 1, net);
1565171440Srrs				if (ret < 0)
1566171440Srrs					return 1;
1567171440Srrs				else if (ret == 0) {
1568163953Srrs					break;
1569163953Srrs				}
1570167598Srrs				if (cnt_sent >= sctp_hb_maxburst)
1571163953Srrs					break;
1572163953Srrs			}
1573163953Srrs		}
1574163953Srrs	}
1575163953Srrs	return (0);
1576163953Srrs}
1577163953Srrs
1578163953Srrsint
1579163953Srrssctp_is_hb_timer_running(struct sctp_tcb *stcb)
1580163953Srrs{
1581165647Srrs	if (SCTP_OS_TIMER_PENDING(&stcb->asoc.hb_timer.timer)) {
1582163953Srrs		/* its running */
1583163953Srrs		return (1);
1584163953Srrs	} else {
1585163953Srrs		/* nope */
1586163953Srrs		return (0);
1587163953Srrs	}
1588163953Srrs}
1589163953Srrs
1590163953Srrsint
1591163953Srrssctp_is_sack_timer_running(struct sctp_tcb *stcb)
1592163953Srrs{
1593165647Srrs	if (SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
1594163953Srrs		/* its running */
1595163953Srrs		return (1);
1596163953Srrs	} else {
1597163953Srrs		/* nope */
1598163953Srrs		return (0);
1599163953Srrs	}
1600163953Srrs}
1601163953Srrs
1602163953Srrs#define SCTP_NUMBER_OF_MTU_SIZES 18
1603163953Srrsstatic uint32_t mtu_sizes[] = {
1604163953Srrs	68,
1605163953Srrs	296,
1606163953Srrs	508,
1607163953Srrs	512,
1608163953Srrs	544,
1609163953Srrs	576,
1610163953Srrs	1006,
1611163953Srrs	1492,
1612163953Srrs	1500,
1613163953Srrs	1536,
1614163953Srrs	2002,
1615163953Srrs	2048,
1616163953Srrs	4352,
1617163953Srrs	4464,
1618163953Srrs	8166,
1619163953Srrs	17914,
1620163953Srrs	32000,
1621163953Srrs	65535
1622163953Srrs};
1623163953Srrs
1624163953Srrs
1625163953Srrsstatic uint32_t
1626163953Srrssctp_getnext_mtu(struct sctp_inpcb *inp, uint32_t cur_mtu)
1627163953Srrs{
1628163953Srrs	/* select another MTU that is just bigger than this one */
1629163953Srrs	int i;
1630163953Srrs
1631163953Srrs	for (i = 0; i < SCTP_NUMBER_OF_MTU_SIZES; i++) {
1632163953Srrs		if (cur_mtu < mtu_sizes[i]) {
1633163953Srrs			/* no max_mtu is bigger than this one */
1634163953Srrs			return (mtu_sizes[i]);
1635163953Srrs		}
1636163953Srrs	}
1637163953Srrs	/* here return the highest allowable */
1638163953Srrs	return (cur_mtu);
1639163953Srrs}
1640163953Srrs
1641163953Srrs
1642163953Srrsvoid
1643163953Srrssctp_pathmtu_timer(struct sctp_inpcb *inp,
1644163953Srrs    struct sctp_tcb *stcb,
1645163953Srrs    struct sctp_nets *net)
1646163953Srrs{
1647163953Srrs	uint32_t next_mtu;
1648163953Srrs
1649163953Srrs	/* restart the timer in any case */
1650163953Srrs	next_mtu = sctp_getnext_mtu(inp, net->mtu);
1651163953Srrs	if (next_mtu <= net->mtu) {
1652163953Srrs		/* nothing to do */
1653163953Srrs		return;
1654169352Srrs	} {
1655169352Srrs		uint32_t mtu;
1656169352Srrs
1657169352Srrs		if ((net->src_addr_selected == 0) ||
1658169352Srrs		    (net->ro._s_addr == NULL) ||
1659169352Srrs		    (net->ro._s_addr->localifa_flags & SCTP_BEING_DELETED)) {
1660169420Srrs			if ((net->ro._s_addr != NULL) && (net->ro._s_addr->localifa_flags & SCTP_BEING_DELETED)) {
1661169352Srrs				sctp_free_ifa(net->ro._s_addr);
1662169352Srrs				net->ro._s_addr = NULL;
1663169352Srrs				net->src_addr_selected = 0;
1664169420Srrs			} else if (net->ro._s_addr == NULL) {
1665169420Srrs				net->ro._s_addr = sctp_source_address_selection(inp,
1666169420Srrs				    stcb,
1667169420Srrs				    (sctp_route_t *) & net->ro,
1668169420Srrs				    net, 0, stcb->asoc.vrf_id);
1669169352Srrs			}
1670169352Srrs			if (net->ro._s_addr)
1671169352Srrs				net->src_addr_selected = 1;
1672169352Srrs		}
1673169352Srrs		if (net->ro._s_addr) {
1674169352Srrs			mtu = SCTP_GATHER_MTU_FROM_ROUTE(net->ro._s_addr, &net->ro._s_addr.sa, net->ro.ro_rt);
1675169352Srrs			if (mtu > next_mtu) {
1676163953Srrs				net->mtu = next_mtu;
1677163953Srrs			}
1678163953Srrs		}
1679163953Srrs	}
1680163953Srrs	/* restart the timer */
1681163953Srrs	sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net);
1682163953Srrs}
1683163953Srrs
1684163953Srrsvoid
1685163953Srrssctp_autoclose_timer(struct sctp_inpcb *inp,
1686163953Srrs    struct sctp_tcb *stcb,
1687163953Srrs    struct sctp_nets *net)
1688163953Srrs{
1689163953Srrs	struct timeval tn, *tim_touse;
1690163953Srrs	struct sctp_association *asoc;
1691163953Srrs	int ticks_gone_by;
1692163953Srrs
1693169378Srrs	(void)SCTP_GETTIME_TIMEVAL(&tn);
1694163953Srrs	if (stcb->asoc.sctp_autoclose_ticks &&
1695163953Srrs	    sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE)) {
1696163953Srrs		/* Auto close is on */
1697163953Srrs		asoc = &stcb->asoc;
1698163953Srrs		/* pick the time to use */
1699163953Srrs		if (asoc->time_last_rcvd.tv_sec >
1700163953Srrs		    asoc->time_last_sent.tv_sec) {
1701163953Srrs			tim_touse = &asoc->time_last_rcvd;
1702163953Srrs		} else {
1703163953Srrs			tim_touse = &asoc->time_last_sent;
1704163953Srrs		}
1705163953Srrs		/* Now has long enough transpired to autoclose? */
1706163953Srrs		ticks_gone_by = SEC_TO_TICKS(tn.tv_sec - tim_touse->tv_sec);
1707163953Srrs		if ((ticks_gone_by > 0) &&
1708163953Srrs		    (ticks_gone_by >= (int)asoc->sctp_autoclose_ticks)) {
1709163953Srrs			/*
1710163953Srrs			 * autoclose time has hit, call the output routine,
1711163953Srrs			 * which should do nothing just to be SURE we don't
1712163953Srrs			 * have hanging data. We can then safely check the
1713163953Srrs			 * queues and know that we are clear to send
1714163953Srrs			 * shutdown
1715163953Srrs			 */
1716163953Srrs			sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_AUTOCLOSE_TMR);
1717163953Srrs			/* Are we clean? */
1718163953Srrs			if (TAILQ_EMPTY(&asoc->send_queue) &&
1719163953Srrs			    TAILQ_EMPTY(&asoc->sent_queue)) {
1720163953Srrs				/*
1721163953Srrs				 * there is nothing queued to send, so I'm
1722163953Srrs				 * done...
1723163953Srrs				 */
1724166675Srrs				if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) {
1725163953Srrs					/* only send SHUTDOWN 1st time thru */
1726163953Srrs					sctp_send_shutdown(stcb, stcb->asoc.primary_destination);
1727166675Srrs					if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
1728166675Srrs					    (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
1729166675Srrs						SCTP_STAT_DECR_GAUGE32(sctps_currestab);
1730166675Srrs					}
1731163953Srrs					asoc->state = SCTP_STATE_SHUTDOWN_SENT;
1732163953Srrs					sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
1733163953Srrs					    stcb->sctp_ep, stcb,
1734163953Srrs					    asoc->primary_destination);
1735163953Srrs					sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
1736163953Srrs					    stcb->sctp_ep, stcb,
1737163953Srrs					    asoc->primary_destination);
1738163953Srrs				}
1739163953Srrs			}
1740163953Srrs		} else {
1741163953Srrs			/*
1742163953Srrs			 * No auto close at this time, reset t-o to check
1743163953Srrs			 * later
1744163953Srrs			 */
1745163953Srrs			int tmp;
1746163953Srrs
1747163953Srrs			/* fool the timer startup to use the time left */
1748163953Srrs			tmp = asoc->sctp_autoclose_ticks;
1749163953Srrs			asoc->sctp_autoclose_ticks -= ticks_gone_by;
1750163953Srrs			sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb,
1751163953Srrs			    net);
1752163953Srrs			/* restore the real tick value */
1753163953Srrs			asoc->sctp_autoclose_ticks = tmp;
1754163953Srrs		}
1755163953Srrs	}
1756163953Srrs}
1757163953Srrs
1758163953Srrsvoid
1759163953Srrssctp_iterator_timer(struct sctp_iterator *it)
1760163953Srrs{
1761163953Srrs	int iteration_count = 0;
1762167598Srrs	int inp_skip = 0;
1763163953Srrs
1764163953Srrs	/*
1765163953Srrs	 * only one iterator can run at a time. This is the only way we can
1766163953Srrs	 * cleanly pull ep's from underneath all the running interators when
1767163953Srrs	 * a ep is freed.
1768163953Srrs	 */
1769163953Srrs	SCTP_ITERATOR_LOCK();
1770163953Srrs	if (it->inp == NULL) {
1771163953Srrs		/* iterator is complete */
1772163953Srrsdone_with_iterator:
1773163953Srrs		SCTP_ITERATOR_UNLOCK();
1774163953Srrs		SCTP_INP_INFO_WLOCK();
1775167598Srrs		TAILQ_REMOVE(&sctppcbinfo.iteratorhead, it, sctp_nxt_itr);
1776163953Srrs		/* stopping the callout is not needed, in theory */
1777163953Srrs		SCTP_INP_INFO_WUNLOCK();
1778169378Srrs		(void)SCTP_OS_TIMER_STOP(&it->tmr.timer);
1779163953Srrs		if (it->function_atend != NULL) {
1780163953Srrs			(*it->function_atend) (it->pointer, it->val);
1781163953Srrs		}
1782170091Srrs		SCTP_FREE(it, SCTP_M_ITER);
1783163953Srrs		return;
1784163953Srrs	}
1785163953Srrsselect_a_new_ep:
1786163953Srrs	SCTP_INP_WLOCK(it->inp);
1787163953Srrs	while (((it->pcb_flags) &&
1788163953Srrs	    ((it->inp->sctp_flags & it->pcb_flags) != it->pcb_flags)) ||
1789163953Srrs	    ((it->pcb_features) &&
1790163953Srrs	    ((it->inp->sctp_features & it->pcb_features) != it->pcb_features))) {
1791163953Srrs		/* endpoint flags or features don't match, so keep looking */
1792163953Srrs		if (it->iterator_flags & SCTP_ITERATOR_DO_SINGLE_INP) {
1793163953Srrs			SCTP_INP_WUNLOCK(it->inp);
1794163953Srrs			goto done_with_iterator;
1795163953Srrs		}
1796163953Srrs		SCTP_INP_WUNLOCK(it->inp);
1797163953Srrs		it->inp = LIST_NEXT(it->inp, sctp_list);
1798163953Srrs		if (it->inp == NULL) {
1799163953Srrs			goto done_with_iterator;
1800163953Srrs		}
1801163953Srrs		SCTP_INP_WLOCK(it->inp);
1802163953Srrs	}
1803163953Srrs	if ((it->inp->inp_starting_point_for_iterator != NULL) &&
1804163953Srrs	    (it->inp->inp_starting_point_for_iterator != it)) {
1805169420Srrs		SCTP_PRINTF("Iterator collision, waiting for one at %p\n",
1806163979Sru		    it->inp);
1807163953Srrs		SCTP_INP_WUNLOCK(it->inp);
1808163953Srrs		goto start_timer_return;
1809163953Srrs	}
1810163953Srrs	/* mark the current iterator on the endpoint */
1811163953Srrs	it->inp->inp_starting_point_for_iterator = it;
1812163953Srrs	SCTP_INP_WUNLOCK(it->inp);
1813163953Srrs	SCTP_INP_RLOCK(it->inp);
1814163953Srrs	/* now go through each assoc which is in the desired state */
1815167598Srrs	if (it->done_current_ep == 0) {
1816167598Srrs		if (it->function_inp != NULL)
1817167598Srrs			inp_skip = (*it->function_inp) (it->inp, it->pointer, it->val);
1818167598Srrs		it->done_current_ep = 1;
1819167598Srrs	}
1820163953Srrs	if (it->stcb == NULL) {
1821163953Srrs		/* run the per instance function */
1822163953Srrs		it->stcb = LIST_FIRST(&it->inp->sctp_asoc_list);
1823163953Srrs	}
1824163953Srrs	SCTP_INP_RUNLOCK(it->inp);
1825167598Srrs	if ((inp_skip) || it->stcb == NULL) {
1826167598Srrs		if (it->function_inp_end != NULL) {
1827167598Srrs			inp_skip = (*it->function_inp_end) (it->inp,
1828167598Srrs			    it->pointer,
1829167598Srrs			    it->val);
1830167598Srrs		}
1831167598Srrs		goto no_stcb;
1832167598Srrs	}
1833163953Srrs	if ((it->stcb) &&
1834163953Srrs	    (it->stcb->asoc.stcb_starting_point_for_iterator == it)) {
1835163953Srrs		it->stcb->asoc.stcb_starting_point_for_iterator = NULL;
1836163953Srrs	}
1837163953Srrs	while (it->stcb) {
1838163953Srrs		SCTP_TCB_LOCK(it->stcb);
1839163953Srrs		if (it->asoc_state && ((it->stcb->asoc.state & it->asoc_state) != it->asoc_state)) {
1840163953Srrs			/* not in the right state... keep looking */
1841163953Srrs			SCTP_TCB_UNLOCK(it->stcb);
1842163953Srrs			goto next_assoc;
1843163953Srrs		}
1844163953Srrs		/* mark the current iterator on the assoc */
1845163953Srrs		it->stcb->asoc.stcb_starting_point_for_iterator = it;
1846163953Srrs		/* see if we have limited out the iterator loop */
1847163953Srrs		iteration_count++;
1848163953Srrs		if (iteration_count > SCTP_ITERATOR_MAX_AT_ONCE) {
1849163953Srrs	start_timer_return:
1850163953Srrs			/* set a timer to continue this later */
1851170943Srrs			if (it->stcb)
1852170943Srrs				SCTP_TCB_UNLOCK(it->stcb);
1853163953Srrs			sctp_timer_start(SCTP_TIMER_TYPE_ITERATOR,
1854163953Srrs			    (struct sctp_inpcb *)it, NULL, NULL);
1855163953Srrs			SCTP_ITERATOR_UNLOCK();
1856163953Srrs			return;
1857163953Srrs		}
1858163953Srrs		/* run function on this one */
1859163953Srrs		(*it->function_assoc) (it->inp, it->stcb, it->pointer, it->val);
1860163953Srrs
1861163953Srrs		/*
1862163953Srrs		 * we lie here, it really needs to have its own type but
1863163953Srrs		 * first I must verify that this won't effect things :-0
1864163953Srrs		 */
1865163953Srrs		if (it->no_chunk_output == 0)
1866163953Srrs			sctp_chunk_output(it->inp, it->stcb, SCTP_OUTPUT_FROM_T3);
1867163953Srrs
1868163953Srrs		SCTP_TCB_UNLOCK(it->stcb);
1869163953Srrsnext_assoc:
1870163953Srrs		it->stcb = LIST_NEXT(it->stcb, sctp_tcblist);
1871167598Srrs		if (it->stcb == NULL) {
1872167598Srrs			if (it->function_inp_end != NULL) {
1873167598Srrs				inp_skip = (*it->function_inp_end) (it->inp,
1874167598Srrs				    it->pointer,
1875167598Srrs				    it->val);
1876167598Srrs			}
1877167598Srrs		}
1878163953Srrs	}
1879167598Srrsno_stcb:
1880163953Srrs	/* done with all assocs on this endpoint, move on to next endpoint */
1881167598Srrs	it->done_current_ep = 0;
1882163953Srrs	SCTP_INP_WLOCK(it->inp);
1883163953Srrs	it->inp->inp_starting_point_for_iterator = NULL;
1884163953Srrs	SCTP_INP_WUNLOCK(it->inp);
1885163953Srrs	if (it->iterator_flags & SCTP_ITERATOR_DO_SINGLE_INP) {
1886163953Srrs		it->inp = NULL;
1887163953Srrs	} else {
1888163953Srrs		SCTP_INP_INFO_RLOCK();
1889163953Srrs		it->inp = LIST_NEXT(it->inp, sctp_list);
1890163953Srrs		SCTP_INP_INFO_RUNLOCK();
1891163953Srrs	}
1892163953Srrs	if (it->inp == NULL) {
1893163953Srrs		goto done_with_iterator;
1894163953Srrs	}
1895163953Srrs	goto select_a_new_ep;
1896163953Srrs}
1897