sctp_timer.c revision 182367
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 182367 2008-08-28 09:44:07Z rrs $");
35163953Srrs
36163953Srrs#define _IP_VHL
37166086Srrs#include <netinet/sctp_os.h>
38163953Srrs#include <netinet/sctp_pcb.h>
39163953Srrs#ifdef INET6
40163953Srrs#endif
41163953Srrs#include <netinet/sctp_var.h>
42167598Srrs#include <netinet/sctp_sysctl.h>
43163953Srrs#include <netinet/sctp_timer.h>
44163953Srrs#include <netinet/sctputil.h>
45163953Srrs#include <netinet/sctp_output.h>
46163953Srrs#include <netinet/sctp_header.h>
47163953Srrs#include <netinet/sctp_indata.h>
48163953Srrs#include <netinet/sctp_asconf.h>
49163953Srrs#include <netinet/sctp_input.h>
50163953Srrs#include <netinet/sctp.h>
51163953Srrs#include <netinet/sctp_uio.h>
52163953Srrs
53163953Srrs
54163953Srrs
55163953Srrsvoid
56163953Srrssctp_early_fr_timer(struct sctp_inpcb *inp,
57163953Srrs    struct sctp_tcb *stcb,
58163953Srrs    struct sctp_nets *net)
59163953Srrs{
60163953Srrs	struct sctp_tmit_chunk *chk, *tp2;
61163953Srrs	struct timeval now, min_wait, tv;
62163953Srrs	unsigned int cur_rtt, cnt = 0, cnt_resend = 0;
63163953Srrs
64163953Srrs	/* an early FR is occuring. */
65169378Srrs	(void)SCTP_GETTIME_TIMEVAL(&now);
66163953Srrs	/* get cur rto in micro-seconds */
67163953Srrs	if (net->lastsa == 0) {
68163953Srrs		/* Hmm no rtt estimate yet? */
69163953Srrs		cur_rtt = stcb->asoc.initial_rto >> 2;
70163953Srrs	} else {
71163953Srrs
72163953Srrs		cur_rtt = ((net->lastsa >> 2) + net->lastsv) >> 1;
73163953Srrs	}
74179783Srrs	if (cur_rtt < SCTP_BASE_SYSCTL(sctp_early_fr_msec)) {
75179783Srrs		cur_rtt = SCTP_BASE_SYSCTL(sctp_early_fr_msec);
76163953Srrs	}
77163953Srrs	cur_rtt *= 1000;
78163953Srrs	tv.tv_sec = cur_rtt / 1000000;
79163953Srrs	tv.tv_usec = cur_rtt % 1000000;
80163953Srrs	min_wait = now;
81163953Srrs	timevalsub(&min_wait, &tv);
82163953Srrs	if (min_wait.tv_sec < 0 || min_wait.tv_usec < 0) {
83163953Srrs		/*
84163953Srrs		 * if we hit here, we don't have enough seconds on the clock
85163953Srrs		 * to account for the RTO. We just let the lower seconds be
86163953Srrs		 * the bounds and don't worry about it. This may mean we
87163953Srrs		 * will mark a lot more than we should.
88163953Srrs		 */
89163953Srrs		min_wait.tv_sec = min_wait.tv_usec = 0;
90163953Srrs	}
91163953Srrs	chk = TAILQ_LAST(&stcb->asoc.sent_queue, sctpchunk_listhead);
92163953Srrs	for (; chk != NULL; chk = tp2) {
93163953Srrs		tp2 = TAILQ_PREV(chk, sctpchunk_listhead, sctp_next);
94163953Srrs		if (chk->whoTo != net) {
95163953Srrs			continue;
96163953Srrs		}
97163953Srrs		if (chk->sent == SCTP_DATAGRAM_RESEND)
98163953Srrs			cnt_resend++;
99163953Srrs		else if ((chk->sent > SCTP_DATAGRAM_UNSENT) &&
100163953Srrs		    (chk->sent < SCTP_DATAGRAM_RESEND)) {
101163953Srrs			/* pending, may need retran */
102163953Srrs			if (chk->sent_rcv_time.tv_sec > min_wait.tv_sec) {
103163953Srrs				/*
104163953Srrs				 * we have reached a chunk that was sent
105163953Srrs				 * some seconds past our min.. forget it we
106163953Srrs				 * will find no more to send.
107163953Srrs				 */
108163953Srrs				continue;
109163953Srrs			} else if (chk->sent_rcv_time.tv_sec == min_wait.tv_sec) {
110163953Srrs				/*
111163953Srrs				 * we must look at the micro seconds to
112163953Srrs				 * know.
113163953Srrs				 */
114163953Srrs				if (chk->sent_rcv_time.tv_usec >= min_wait.tv_usec) {
115163953Srrs					/*
116163953Srrs					 * ok it was sent after our boundary
117163953Srrs					 * time.
118163953Srrs					 */
119163953Srrs					continue;
120163953Srrs				}
121163953Srrs			}
122179783Srrs			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_EARLYFR_LOGGING_ENABLE) {
123170744Srrs				sctp_log_fr(chk->rec.data.TSN_seq, chk->snd_count,
124170744Srrs				    4, SCTP_FR_MARKED_EARLY);
125170744Srrs			}
126163953Srrs			SCTP_STAT_INCR(sctps_earlyfrmrkretrans);
127163953Srrs			chk->sent = SCTP_DATAGRAM_RESEND;
128163953Srrs			sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
129163953Srrs			/* double book size since we are doing an early FR */
130163953Srrs			chk->book_size_scale++;
131163953Srrs			cnt += chk->send_size;
132163953Srrs			if ((cnt + net->flight_size) > net->cwnd) {
133163953Srrs				/* Mark all we could possibly resend */
134163953Srrs				break;
135163953Srrs			}
136163953Srrs		}
137163953Srrs	}
138163953Srrs	if (cnt) {
139163953Srrs		/*
140171440Srrs		 * JRS - Use the congestion control given in the congestion
141171440Srrs		 * control module
142163953Srrs		 */
143171440Srrs		stcb->asoc.cc_functions.sctp_cwnd_update_after_fr_timer(inp, stcb, net);
144163953Srrs	} else if (cnt_resend) {
145172090Srrs		sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_EARLY_FR_TMR, SCTP_SO_NOT_LOCKED);
146163953Srrs	}
147163953Srrs	/* Restart it? */
148163953Srrs	if (net->flight_size < net->cwnd) {
149163953Srrs		SCTP_STAT_INCR(sctps_earlyfrstrtmr);
150163953Srrs		sctp_timer_start(SCTP_TIMER_TYPE_EARLYFR, stcb->sctp_ep, stcb, net);
151163953Srrs	}
152163953Srrs}
153163953Srrs
154163953Srrsvoid
155163953Srrssctp_audit_retranmission_queue(struct sctp_association *asoc)
156163953Srrs{
157163953Srrs	struct sctp_tmit_chunk *chk;
158163953Srrs
159169420Srrs	SCTPDBG(SCTP_DEBUG_TIMER4, "Audit invoked on send queue cnt:%d onqueue:%d\n",
160169420Srrs	    asoc->sent_queue_retran_cnt,
161169420Srrs	    asoc->sent_queue_cnt);
162163953Srrs	asoc->sent_queue_retran_cnt = 0;
163163953Srrs	asoc->sent_queue_cnt = 0;
164163953Srrs	TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
165163953Srrs		if (chk->sent == SCTP_DATAGRAM_RESEND) {
166163953Srrs			sctp_ucount_incr(asoc->sent_queue_retran_cnt);
167163953Srrs		}
168163953Srrs		asoc->sent_queue_cnt++;
169163953Srrs	}
170163953Srrs	TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
171163953Srrs		if (chk->sent == SCTP_DATAGRAM_RESEND) {
172163953Srrs			sctp_ucount_incr(asoc->sent_queue_retran_cnt);
173163953Srrs		}
174163953Srrs	}
175179157Srrs	TAILQ_FOREACH(chk, &asoc->asconf_send_queue, sctp_next) {
176179157Srrs		if (chk->sent == SCTP_DATAGRAM_RESEND) {
177179157Srrs			sctp_ucount_incr(asoc->sent_queue_retran_cnt);
178179157Srrs		}
179179157Srrs	}
180169420Srrs	SCTPDBG(SCTP_DEBUG_TIMER4, "Audit completes retran:%d onqueue:%d\n",
181169420Srrs	    asoc->sent_queue_retran_cnt,
182169420Srrs	    asoc->sent_queue_cnt);
183163953Srrs}
184163953Srrs
185163953Srrsint
186163953Srrssctp_threshold_management(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
187163953Srrs    struct sctp_nets *net, uint16_t threshold)
188163953Srrs{
189163953Srrs	if (net) {
190163953Srrs		net->error_count++;
191169420Srrs		SCTPDBG(SCTP_DEBUG_TIMER4, "Error count for %p now %d thresh:%d\n",
192169420Srrs		    net, net->error_count,
193169420Srrs		    net->failure_threshold);
194163953Srrs		if (net->error_count > net->failure_threshold) {
195163953Srrs			/* We had a threshold failure */
196163953Srrs			if (net->dest_state & SCTP_ADDR_REACHABLE) {
197163953Srrs				net->dest_state &= ~SCTP_ADDR_REACHABLE;
198163953Srrs				net->dest_state |= SCTP_ADDR_NOT_REACHABLE;
199167598Srrs				net->dest_state &= ~SCTP_ADDR_REQ_PRIMARY;
200163953Srrs				if (net == stcb->asoc.primary_destination) {
201163953Srrs					net->dest_state |= SCTP_ADDR_WAS_PRIMARY;
202163953Srrs				}
203171440Srrs				/*
204171440Srrs				 * JRS 5/14/07 - If a destination is
205171440Srrs				 * unreachable, the PF bit is turned off.
206171440Srrs				 * This allows an unambiguous use of the PF
207171440Srrs				 * bit for destinations that are reachable
208171440Srrs				 * but potentially failed. If the
209171440Srrs				 * destination is set to the unreachable
210171440Srrs				 * state, also set the destination to the PF
211171440Srrs				 * state.
212171440Srrs				 */
213171440Srrs				/*
214171440Srrs				 * Add debug message here if destination is
215171440Srrs				 * not in PF state.
216171440Srrs				 */
217171440Srrs				/* Stop any running T3 timers here? */
218179783Srrs				if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) && SCTP_BASE_SYSCTL(sctp_cmt_pf)) {
219171440Srrs					net->dest_state &= ~SCTP_ADDR_PF;
220171440Srrs					SCTPDBG(SCTP_DEBUG_TIMER4, "Destination %p moved from PF to unreachable.\n",
221171440Srrs					    net);
222171440Srrs				}
223163953Srrs				sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN,
224163953Srrs				    stcb,
225163953Srrs				    SCTP_FAILED_THRESHOLD,
226172090Srrs				    (void *)net, SCTP_SO_NOT_LOCKED);
227163953Srrs			}
228163953Srrs		}
229163953Srrs		/*********HOLD THIS COMMENT FOR PATCH OF ALTERNATE
230163953Srrs		 *********ROUTING CODE
231163953Srrs		 */
232163953Srrs		/*********HOLD THIS COMMENT FOR END OF PATCH OF ALTERNATE
233163953Srrs		 *********ROUTING CODE
234163953Srrs		 */
235163953Srrs	}
236163953Srrs	if (stcb == NULL)
237163953Srrs		return (0);
238163953Srrs
239163953Srrs	if (net) {
240163953Srrs		if ((net->dest_state & SCTP_ADDR_UNCONFIRMED) == 0) {
241179783Srrs			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
242171943Srrs				sctp_misc_ints(SCTP_THRESHOLD_INCR,
243171943Srrs				    stcb->asoc.overall_error_count,
244171943Srrs				    (stcb->asoc.overall_error_count + 1),
245171943Srrs				    SCTP_FROM_SCTP_TIMER,
246171943Srrs				    __LINE__);
247171943Srrs			}
248163953Srrs			stcb->asoc.overall_error_count++;
249163953Srrs		}
250163953Srrs	} else {
251179783Srrs		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
252171943Srrs			sctp_misc_ints(SCTP_THRESHOLD_INCR,
253171943Srrs			    stcb->asoc.overall_error_count,
254171943Srrs			    (stcb->asoc.overall_error_count + 1),
255171943Srrs			    SCTP_FROM_SCTP_TIMER,
256171943Srrs			    __LINE__);
257171943Srrs		}
258163953Srrs		stcb->asoc.overall_error_count++;
259163953Srrs	}
260169420Srrs	SCTPDBG(SCTP_DEBUG_TIMER4, "Overall error count for %p now %d thresh:%u state:%x\n",
261169420Srrs	    &stcb->asoc, stcb->asoc.overall_error_count,
262169420Srrs	    (uint32_t) threshold,
263169420Srrs	    ((net == NULL) ? (uint32_t) 0 : (uint32_t) net->dest_state));
264163953Srrs	/*
265163953Srrs	 * We specifically do not do >= to give the assoc one more change
266163953Srrs	 * before we fail it.
267163953Srrs	 */
268163953Srrs	if (stcb->asoc.overall_error_count > threshold) {
269163953Srrs		/* Abort notification sends a ULP notify */
270163953Srrs		struct mbuf *oper;
271163953Srrs
272163953Srrs		oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
273163953Srrs		    0, M_DONTWAIT, 1, MT_DATA);
274163953Srrs		if (oper) {
275163953Srrs			struct sctp_paramhdr *ph;
276163953Srrs			uint32_t *ippp;
277163953Srrs
278165647Srrs			SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
279163953Srrs			    sizeof(uint32_t);
280163953Srrs			ph = mtod(oper, struct sctp_paramhdr *);
281163953Srrs			ph->param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
282165647Srrs			ph->param_length = htons(SCTP_BUF_LEN(oper));
283163953Srrs			ippp = (uint32_t *) (ph + 1);
284165220Srrs			*ippp = htonl(SCTP_FROM_SCTP_TIMER + SCTP_LOC_1);
285163953Srrs		}
286165220Srrs		inp->last_abort_code = SCTP_FROM_SCTP_TIMER + SCTP_LOC_1;
287172090Srrs		sctp_abort_an_association(inp, stcb, SCTP_FAILED_THRESHOLD, oper, SCTP_SO_NOT_LOCKED);
288163953Srrs		return (1);
289163953Srrs	}
290163953Srrs	return (0);
291163953Srrs}
292163953Srrs
293163953Srrsstruct sctp_nets *
294163953Srrssctp_find_alternate_net(struct sctp_tcb *stcb,
295163953Srrs    struct sctp_nets *net,
296171440Srrs    int mode)
297163953Srrs{
298163953Srrs	/* Find and return an alternate network if possible */
299171440Srrs	struct sctp_nets *alt, *mnet, *min_errors_net = NULL, *max_cwnd_net = NULL;
300163953Srrs	int once;
301163953Srrs
302171440Srrs	/* JRS 5/14/07 - Initialize min_errors to an impossible value. */
303171440Srrs	int min_errors = -1;
304171440Srrs	uint32_t max_cwnd = 0;
305171440Srrs
306163953Srrs	if (stcb->asoc.numnets == 1) {
307163953Srrs		/* No others but net */
308163953Srrs		return (TAILQ_FIRST(&stcb->asoc.nets));
309163953Srrs	}
310171440Srrs	/*
311171440Srrs	 * JRS 5/14/07 - If mode is set to 2, use the CMT PF find alternate
312171440Srrs	 * net algorithm. This algorithm chooses the active destination (not
313171440Srrs	 * in PF state) with the largest cwnd value. If all destinations are
314171440Srrs	 * in PF state, unreachable, or unconfirmed, choose the desination
315171440Srrs	 * that is in PF state with the lowest error count. In case of a
316171440Srrs	 * tie, choose the destination that was most recently active.
317171440Srrs	 */
318171440Srrs	if (mode == 2) {
319163953Srrs		TAILQ_FOREACH(mnet, &stcb->asoc.nets, sctp_next) {
320171440Srrs			/*
321171440Srrs			 * JRS 5/14/07 - If the destination is unreachable
322171440Srrs			 * or unconfirmed, skip it.
323171440Srrs			 */
324163953Srrs			if (((mnet->dest_state & SCTP_ADDR_REACHABLE) != SCTP_ADDR_REACHABLE) ||
325171440Srrs			    (mnet->dest_state & SCTP_ADDR_UNCONFIRMED)) {
326171440Srrs				continue;
327171440Srrs			}
328171440Srrs			/*
329171440Srrs			 * JRS 5/14/07 -  If the destination is reachable
330171440Srrs			 * but in PF state, compare the error count of the
331171440Srrs			 * destination to the minimum error count seen thus
332171440Srrs			 * far. Store the destination with the lower error
333171440Srrs			 * count.  If the error counts are equal, store the
334171440Srrs			 * destination that was most recently active.
335171440Srrs			 */
336171440Srrs			if (mnet->dest_state & SCTP_ADDR_PF) {
337171440Srrs				/*
338171440Srrs				 * JRS 5/14/07 - If the destination under
339171440Srrs				 * consideration is the current destination,
340171440Srrs				 * work as if the error count is one higher.
341171440Srrs				 * The actual error count will not be
342171440Srrs				 * incremented until later in the t3
343171440Srrs				 * handler.
344171440Srrs				 */
345171440Srrs				if (mnet == net) {
346171440Srrs					if (min_errors == -1) {
347171440Srrs						min_errors = mnet->error_count + 1;
348171440Srrs						min_errors_net = mnet;
349171440Srrs					} else if (mnet->error_count + 1 < min_errors) {
350171440Srrs						min_errors = mnet->error_count + 1;
351171440Srrs						min_errors_net = mnet;
352171440Srrs					} else if (mnet->error_count + 1 == min_errors
353171440Srrs					    && mnet->last_active > min_errors_net->last_active) {
354171440Srrs						min_errors_net = mnet;
355171440Srrs						min_errors = mnet->error_count + 1;
356171440Srrs					}
357171440Srrs					continue;
358171440Srrs				} else {
359171440Srrs					if (min_errors == -1) {
360171440Srrs						min_errors = mnet->error_count;
361171440Srrs						min_errors_net = mnet;
362171440Srrs					} else if (mnet->error_count < min_errors) {
363171440Srrs						min_errors = mnet->error_count;
364171440Srrs						min_errors_net = mnet;
365171440Srrs					} else if (mnet->error_count == min_errors
366171440Srrs					    && mnet->last_active > min_errors_net->last_active) {
367171440Srrs						min_errors_net = mnet;
368171440Srrs						min_errors = mnet->error_count;
369171440Srrs					}
370171440Srrs					continue;
371171440Srrs				}
372171440Srrs			}
373171440Srrs			/*
374171440Srrs			 * JRS 5/14/07 - If the destination is reachable and
375171440Srrs			 * not in PF state, compare the cwnd of the
376171440Srrs			 * destination to the highest cwnd seen thus far.
377171440Srrs			 * Store the destination with the higher cwnd value.
378171440Srrs			 * If the cwnd values are equal, randomly choose one
379171440Srrs			 * of the two destinations.
380171440Srrs			 */
381171440Srrs			if (max_cwnd < mnet->cwnd) {
382171440Srrs				max_cwnd_net = mnet;
383171440Srrs				max_cwnd = mnet->cwnd;
384171440Srrs			} else if (max_cwnd == mnet->cwnd) {
385171440Srrs				uint32_t rndval;
386171440Srrs				uint8_t this_random;
387171440Srrs
388171440Srrs				if (stcb->asoc.hb_random_idx > 3) {
389171440Srrs					rndval = sctp_select_initial_TSN(&stcb->sctp_ep->sctp_ep);
390171440Srrs					memcpy(stcb->asoc.hb_random_values, &rndval, sizeof(stcb->asoc.hb_random_values));
391171440Srrs					this_random = stcb->asoc.hb_random_values[0];
392171440Srrs					stcb->asoc.hb_random_idx++;
393171440Srrs					stcb->asoc.hb_ect_randombit = 0;
394171440Srrs				} else {
395171440Srrs					this_random = stcb->asoc.hb_random_values[stcb->asoc.hb_random_idx];
396171440Srrs					stcb->asoc.hb_random_idx++;
397171440Srrs					stcb->asoc.hb_ect_randombit = 0;
398171440Srrs				}
399171440Srrs				if (this_random % 2 == 1) {
400171440Srrs					max_cwnd_net = mnet;
401180387Srrs					max_cwnd = mnet->cwnd;	/* Useless? */
402171440Srrs				}
403171440Srrs			}
404171440Srrs		}
405171440Srrs		/*
406171440Srrs		 * JRS 5/14/07 - After all destination have been considered
407171440Srrs		 * as alternates, check to see if there was some active
408171440Srrs		 * destination (not in PF state).  If not, check to see if
409171440Srrs		 * there was some PF destination with the minimum number of
410171440Srrs		 * errors.  If not, return the original destination.  If
411171440Srrs		 * there is a min_errors_net, remove the PF flag from that
412171440Srrs		 * destination, set the cwnd to one or two MTUs, and return
413171440Srrs		 * the destination as an alt. If there was some active
414171440Srrs		 * destination with a highest cwnd, return the destination
415171440Srrs		 * as an alt.
416171440Srrs		 */
417171440Srrs		if (max_cwnd_net == NULL) {
418171440Srrs			if (min_errors_net == NULL) {
419171440Srrs				return (net);
420171440Srrs			}
421171440Srrs			min_errors_net->dest_state &= ~SCTP_ADDR_PF;
422179783Srrs			min_errors_net->cwnd = min_errors_net->mtu * SCTP_BASE_SYSCTL(sctp_cmt_pf);
423171440Srrs			if (SCTP_OS_TIMER_PENDING(&min_errors_net->rxt_timer.timer)) {
424171440Srrs				sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
425171440Srrs				    stcb, min_errors_net,
426171440Srrs				    SCTP_FROM_SCTP_TIMER + SCTP_LOC_2);
427171440Srrs			}
428171440Srrs			SCTPDBG(SCTP_DEBUG_TIMER4, "Destination %p moved from PF to active with %d errors.\n",
429171440Srrs			    min_errors_net, min_errors_net->error_count);
430171440Srrs			return (min_errors_net);
431171440Srrs		} else {
432171440Srrs			return (max_cwnd_net);
433171440Srrs		}
434171440Srrs	}
435171440Srrs	/*
436171440Srrs	 * JRS 5/14/07 - If mode is set to 1, use the CMT policy for
437171440Srrs	 * choosing an alternate net.
438171440Srrs	 */
439171440Srrs	else if (mode == 1) {
440171440Srrs		TAILQ_FOREACH(mnet, &stcb->asoc.nets, sctp_next) {
441171440Srrs			if (((mnet->dest_state & SCTP_ADDR_REACHABLE) != SCTP_ADDR_REACHABLE) ||
442163953Srrs			    (mnet->dest_state & SCTP_ADDR_UNCONFIRMED)
443163953Srrs			    ) {
444163953Srrs				/*
445163953Srrs				 * will skip ones that are not-reachable or
446163953Srrs				 * unconfirmed
447163953Srrs				 */
448163953Srrs				continue;
449163953Srrs			}
450171440Srrs			if (max_cwnd < mnet->cwnd) {
451171440Srrs				max_cwnd_net = mnet;
452171440Srrs				max_cwnd = mnet->cwnd;
453171440Srrs			} else if (max_cwnd == mnet->cwnd) {
454163953Srrs				uint32_t rndval;
455163953Srrs				uint8_t this_random;
456163953Srrs
457163953Srrs				if (stcb->asoc.hb_random_idx > 3) {
458163953Srrs					rndval = sctp_select_initial_TSN(&stcb->sctp_ep->sctp_ep);
459163953Srrs					memcpy(stcb->asoc.hb_random_values, &rndval,
460163953Srrs					    sizeof(stcb->asoc.hb_random_values));
461163953Srrs					this_random = stcb->asoc.hb_random_values[0];
462163953Srrs					stcb->asoc.hb_random_idx = 0;
463163953Srrs					stcb->asoc.hb_ect_randombit = 0;
464163953Srrs				} else {
465163953Srrs					this_random = stcb->asoc.hb_random_values[stcb->asoc.hb_random_idx];
466163953Srrs					stcb->asoc.hb_random_idx++;
467163953Srrs					stcb->asoc.hb_ect_randombit = 0;
468163953Srrs				}
469163953Srrs				if (this_random % 2) {
470171440Srrs					max_cwnd_net = mnet;
471171440Srrs					max_cwnd = mnet->cwnd;
472163953Srrs				}
473163953Srrs			}
474163953Srrs		}
475171440Srrs		if (max_cwnd_net) {
476171440Srrs			return (max_cwnd_net);
477163953Srrs		}
478163953Srrs	}
479163953Srrs	mnet = net;
480163953Srrs	once = 0;
481163953Srrs
482163953Srrs	if (mnet == NULL) {
483163953Srrs		mnet = TAILQ_FIRST(&stcb->asoc.nets);
484163953Srrs	}
485163953Srrs	do {
486163953Srrs		alt = TAILQ_NEXT(mnet, sctp_next);
487163953Srrs		if (alt == NULL) {
488163953Srrs			once++;
489163953Srrs			if (once > 1) {
490163953Srrs				break;
491163953Srrs			}
492163953Srrs			alt = TAILQ_FIRST(&stcb->asoc.nets);
493163953Srrs		}
494163953Srrs		if (alt->ro.ro_rt == NULL) {
495167598Srrs			if (alt->ro._s_addr) {
496167598Srrs				sctp_free_ifa(alt->ro._s_addr);
497167598Srrs				alt->ro._s_addr = NULL;
498167598Srrs			}
499163953Srrs			alt->src_addr_selected = 0;
500163953Srrs		}
501163953Srrs		if (
502163953Srrs		    ((alt->dest_state & SCTP_ADDR_REACHABLE) == SCTP_ADDR_REACHABLE) &&
503163953Srrs		    (alt->ro.ro_rt != NULL) &&
504169655Srrs		/* sa_ignore NO_NULL_CHK */
505163953Srrs		    (!(alt->dest_state & SCTP_ADDR_UNCONFIRMED))
506163953Srrs		    ) {
507163953Srrs			/* Found a reachable address */
508163953Srrs			break;
509163953Srrs		}
510163953Srrs		mnet = alt;
511163953Srrs	} while (alt != NULL);
512163953Srrs
513163953Srrs	if (alt == NULL) {
514163953Srrs		/* Case where NO insv network exists (dormant state) */
515163953Srrs		/* we rotate destinations */
516163953Srrs		once = 0;
517163953Srrs		mnet = net;
518163953Srrs		do {
519163953Srrs			alt = TAILQ_NEXT(mnet, sctp_next);
520163953Srrs			if (alt == NULL) {
521163953Srrs				once++;
522163953Srrs				if (once > 1) {
523163953Srrs					break;
524163953Srrs				}
525163953Srrs				alt = TAILQ_FIRST(&stcb->asoc.nets);
526163953Srrs			}
527169655Srrs			/* sa_ignore NO_NULL_CHK */
528163953Srrs			if ((!(alt->dest_state & SCTP_ADDR_UNCONFIRMED)) &&
529163953Srrs			    (alt != net)) {
530163953Srrs				/* Found an alternate address */
531163953Srrs				break;
532163953Srrs			}
533163953Srrs			mnet = alt;
534163953Srrs		} while (alt != NULL);
535163953Srrs	}
536163953Srrs	if (alt == NULL) {
537163953Srrs		return (net);
538163953Srrs	}
539163953Srrs	return (alt);
540163953Srrs}
541163953Srrs
542171440Srrs
543171440Srrs
544163953Srrsstatic void
545163953Srrssctp_backoff_on_timeout(struct sctp_tcb *stcb,
546163953Srrs    struct sctp_nets *net,
547163953Srrs    int win_probe,
548163953Srrs    int num_marked)
549163953Srrs{
550170642Srrs	if (net->RTO == 0) {
551170642Srrs		net->RTO = stcb->asoc.minrto;
552170642Srrs	}
553163953Srrs	net->RTO <<= 1;
554163953Srrs	if (net->RTO > stcb->asoc.maxrto) {
555163953Srrs		net->RTO = stcb->asoc.maxrto;
556163953Srrs	}
557163953Srrs	if ((win_probe == 0) && num_marked) {
558163953Srrs		/* We don't apply penalty to window probe scenarios */
559171440Srrs		/* JRS - Use the congestion control given in the CC module */
560171440Srrs		stcb->asoc.cc_functions.sctp_cwnd_update_after_timeout(stcb, net);
561163953Srrs	}
562163953Srrs}
563163953Srrs
564182367Srrsstatic void
565182367Srrssctp_recover_sent_list(struct sctp_tcb *stcb)
566182367Srrs{
567182367Srrs	struct sctp_tmit_chunk *chk, *tp2;
568182367Srrs	struct sctp_association *asoc;
569182367Srrs
570182367Srrs	asoc = &stcb->asoc;
571182367Srrs	chk = TAILQ_FIRST(&stcb->asoc.sent_queue);
572182367Srrs	for (; chk != NULL; chk = tp2) {
573182367Srrs		tp2 = TAILQ_NEXT(chk, sctp_next);
574182367Srrs		if ((compare_with_wrap(stcb->asoc.last_acked_seq,
575182367Srrs		    chk->rec.data.TSN_seq,
576182367Srrs		    MAX_TSN)) ||
577182367Srrs		    (stcb->asoc.last_acked_seq == chk->rec.data.TSN_seq)) {
578182367Srrs
579182367Srrs			SCTP_PRINTF("Found chk:%p tsn:%x <= last_acked_seq:%x\n",
580182367Srrs			    chk, chk->rec.data.TSN_seq, stcb->asoc.last_acked_seq);
581182367Srrs			TAILQ_REMOVE(&asoc->sent_queue, chk, sctp_next);
582182367Srrs			if (chk->pr_sctp_on) {
583182367Srrs				if (asoc->pr_sctp_cnt != 0)
584182367Srrs					asoc->pr_sctp_cnt--;
585182367Srrs			}
586182367Srrs			if (chk->data) {
587182367Srrs				/* sa_ignore NO_NULL_CHK */
588182367Srrs				sctp_free_bufspace(stcb, asoc, chk, 1);
589182367Srrs				sctp_m_freem(chk->data);
590182367Srrs				if (PR_SCTP_BUF_ENABLED(chk->flags)) {
591182367Srrs					asoc->sent_queue_cnt_removeable--;
592182367Srrs				}
593182367Srrs			}
594182367Srrs			chk->data = NULL;
595182367Srrs			asoc->sent_queue_cnt--;
596182367Srrs			sctp_free_a_chunk(stcb, chk);
597182367Srrs		}
598182367Srrs	}
599182367Srrs	SCTP_PRINTF("after recover order is as follows\n");
600182367Srrs	chk = TAILQ_FIRST(&stcb->asoc.sent_queue);
601182367Srrs	for (; chk != NULL; chk = tp2) {
602182367Srrs		tp2 = TAILQ_NEXT(chk, sctp_next);
603182367Srrs		SCTP_PRINTF("chk:%p TSN:%x\n", chk, chk->rec.data.TSN_seq);
604182367Srrs	}
605182367Srrs}
606182367Srrs
607163953Srrsstatic int
608163953Srrssctp_mark_all_for_resend(struct sctp_tcb *stcb,
609163953Srrs    struct sctp_nets *net,
610163953Srrs    struct sctp_nets *alt,
611163953Srrs    int window_probe,
612163953Srrs    int *num_marked)
613163953Srrs{
614163953Srrs
615163953Srrs	/*
616163953Srrs	 * Mark all chunks (well not all) that were sent to *net for
617163953Srrs	 * retransmission. Move them to alt for there destination as well...
618163953Srrs	 * We only mark chunks that have been outstanding long enough to
619163953Srrs	 * have received feed-back.
620163953Srrs	 */
621163953Srrs	struct sctp_tmit_chunk *chk, *tp2, *could_be_sent = NULL;
622163953Srrs	struct sctp_nets *lnets;
623163953Srrs	struct timeval now, min_wait, tv;
624163953Srrs	int cur_rtt;
625168709Srrs	int audit_tf, num_mk, fir;
626163953Srrs	unsigned int cnt_mk;
627168709Srrs	uint32_t orig_flight, orig_tf;
628163953Srrs	uint32_t tsnlast, tsnfirst;
629182367Srrs	int recovery_cnt = 0;
630163953Srrs
631171440Srrs
632163953Srrs	/* none in flight now */
633163953Srrs	audit_tf = 0;
634163953Srrs	fir = 0;
635163953Srrs	/*
636163953Srrs	 * figure out how long a data chunk must be pending before we can
637163953Srrs	 * mark it ..
638163953Srrs	 */
639169378Srrs	(void)SCTP_GETTIME_TIMEVAL(&now);
640163953Srrs	/* get cur rto in micro-seconds */
641163953Srrs	cur_rtt = (((net->lastsa >> 2) + net->lastsv) >> 1);
642163953Srrs	cur_rtt *= 1000;
643179783Srrs	if (SCTP_BASE_SYSCTL(sctp_logging_level) & (SCTP_EARLYFR_LOGGING_ENABLE | SCTP_FR_LOGGING_ENABLE)) {
644170744Srrs		sctp_log_fr(cur_rtt,
645170744Srrs		    stcb->asoc.peers_rwnd,
646170744Srrs		    window_probe,
647170744Srrs		    SCTP_FR_T3_MARK_TIME);
648170744Srrs		sctp_log_fr(net->flight_size,
649170744Srrs		    SCTP_OS_TIMER_PENDING(&net->fr_timer.timer),
650170744Srrs		    SCTP_OS_TIMER_ACTIVE(&net->fr_timer.timer),
651170744Srrs		    SCTP_FR_CWND_REPORT);
652170744Srrs		sctp_log_fr(net->flight_size, net->cwnd, stcb->asoc.total_flight, SCTP_FR_CWND_REPORT);
653170744Srrs	}
654163953Srrs	tv.tv_sec = cur_rtt / 1000000;
655163953Srrs	tv.tv_usec = cur_rtt % 1000000;
656163953Srrs	min_wait = now;
657163953Srrs	timevalsub(&min_wait, &tv);
658163953Srrs	if (min_wait.tv_sec < 0 || min_wait.tv_usec < 0) {
659163953Srrs		/*
660163953Srrs		 * if we hit here, we don't have enough seconds on the clock
661163953Srrs		 * to account for the RTO. We just let the lower seconds be
662163953Srrs		 * the bounds and don't worry about it. This may mean we
663163953Srrs		 * will mark a lot more than we should.
664163953Srrs		 */
665163953Srrs		min_wait.tv_sec = min_wait.tv_usec = 0;
666163953Srrs	}
667179783Srrs	if (SCTP_BASE_SYSCTL(sctp_logging_level) & (SCTP_EARLYFR_LOGGING_ENABLE | SCTP_FR_LOGGING_ENABLE)) {
668170744Srrs		sctp_log_fr(cur_rtt, now.tv_sec, now.tv_usec, SCTP_FR_T3_MARK_TIME);
669170744Srrs		sctp_log_fr(0, min_wait.tv_sec, min_wait.tv_usec, SCTP_FR_T3_MARK_TIME);
670170744Srrs	}
671163953Srrs	/*
672163953Srrs	 * Our rwnd will be incorrect here since we are not adding back the
673163953Srrs	 * cnt * mbuf but we will fix that down below.
674163953Srrs	 */
675163953Srrs	orig_flight = net->flight_size;
676168709Srrs	orig_tf = stcb->asoc.total_flight;
677168709Srrs
678163953Srrs	net->fast_retran_ip = 0;
679163953Srrs	/* Now on to each chunk */
680163953Srrs	num_mk = cnt_mk = 0;
681163953Srrs	tsnfirst = tsnlast = 0;
682182367Srrsstart_again:
683163953Srrs	chk = TAILQ_FIRST(&stcb->asoc.sent_queue);
684163953Srrs	for (; chk != NULL; chk = tp2) {
685163953Srrs		tp2 = TAILQ_NEXT(chk, sctp_next);
686163953Srrs		if ((compare_with_wrap(stcb->asoc.last_acked_seq,
687163953Srrs		    chk->rec.data.TSN_seq,
688163953Srrs		    MAX_TSN)) ||
689163953Srrs		    (stcb->asoc.last_acked_seq == chk->rec.data.TSN_seq)) {
690163953Srrs			/* Strange case our list got out of order? */
691182367Srrs			SCTP_PRINTF("Our list is out of order? last_acked:%x chk:%x",
692182367Srrs			    (unsigned int)stcb->asoc.last_acked_seq, (unsigned int)chk->rec.data.TSN_seq);
693182367Srrs			recovery_cnt++;
694182367Srrs#ifdef INVARIANTS
695182367Srrs			panic("last acked >= chk on sent-Q");
696182367Srrs#else
697182367Srrs			SCTP_PRINTF("Recover attempts a restart cnt:%d\n", recovery_cnt);
698182367Srrs			sctp_recover_sent_list(stcb);
699182367Srrs			if (recovery_cnt < 10) {
700182367Srrs				goto start_again;
701182367Srrs			} else {
702182367Srrs				SCTP_PRINTF("Recovery fails %d times??\n", recovery_cnt);
703182367Srrs			}
704182367Srrs#endif
705163953Srrs		}
706163953Srrs		if ((chk->whoTo == net) && (chk->sent < SCTP_DATAGRAM_ACKED)) {
707163953Srrs			/*
708163953Srrs			 * found one to mark: If it is less than
709163953Srrs			 * DATAGRAM_ACKED it MUST not be a skipped or marked
710163953Srrs			 * TSN but instead one that is either already set
711163953Srrs			 * for retransmission OR one that needs
712163953Srrs			 * retransmission.
713163953Srrs			 */
714163953Srrs
715163953Srrs			/* validate its been outstanding long enough */
716179783Srrs			if (SCTP_BASE_SYSCTL(sctp_logging_level) & (SCTP_EARLYFR_LOGGING_ENABLE | SCTP_FR_LOGGING_ENABLE)) {
717170744Srrs				sctp_log_fr(chk->rec.data.TSN_seq,
718170744Srrs				    chk->sent_rcv_time.tv_sec,
719170744Srrs				    chk->sent_rcv_time.tv_usec,
720170744Srrs				    SCTP_FR_T3_MARK_TIME);
721170744Srrs			}
722163953Srrs			if ((chk->sent_rcv_time.tv_sec > min_wait.tv_sec) && (window_probe == 0)) {
723163953Srrs				/*
724163953Srrs				 * we have reached a chunk that was sent
725163953Srrs				 * some seconds past our min.. forget it we
726163953Srrs				 * will find no more to send.
727163953Srrs				 */
728179783Srrs				if (SCTP_BASE_SYSCTL(sctp_logging_level) & (SCTP_EARLYFR_LOGGING_ENABLE | SCTP_FR_LOGGING_ENABLE)) {
729170744Srrs					sctp_log_fr(0,
730170744Srrs					    chk->sent_rcv_time.tv_sec,
731170744Srrs					    chk->sent_rcv_time.tv_usec,
732170744Srrs					    SCTP_FR_T3_STOPPED);
733170744Srrs				}
734163953Srrs				continue;
735163953Srrs			} else if ((chk->sent_rcv_time.tv_sec == min_wait.tv_sec) &&
736163953Srrs			    (window_probe == 0)) {
737163953Srrs				/*
738163953Srrs				 * we must look at the micro seconds to
739163953Srrs				 * know.
740163953Srrs				 */
741163953Srrs				if (chk->sent_rcv_time.tv_usec >= min_wait.tv_usec) {
742163953Srrs					/*
743163953Srrs					 * ok it was sent after our boundary
744163953Srrs					 * time.
745163953Srrs					 */
746179783Srrs					if (SCTP_BASE_SYSCTL(sctp_logging_level) & (SCTP_EARLYFR_LOGGING_ENABLE | SCTP_FR_LOGGING_ENABLE)) {
747170744Srrs						sctp_log_fr(0,
748170744Srrs						    chk->sent_rcv_time.tv_sec,
749170744Srrs						    chk->sent_rcv_time.tv_usec,
750170744Srrs						    SCTP_FR_T3_STOPPED);
751170744Srrs					}
752163953Srrs					continue;
753163953Srrs				}
754163953Srrs			}
755163953Srrs			if (PR_SCTP_TTL_ENABLED(chk->flags)) {
756163953Srrs				/* Is it expired? */
757163953Srrs				if ((now.tv_sec > chk->rec.data.timetodrop.tv_sec) ||
758163953Srrs				    ((chk->rec.data.timetodrop.tv_sec == now.tv_sec) &&
759163953Srrs				    (now.tv_usec > chk->rec.data.timetodrop.tv_usec))) {
760163953Srrs					/* Yes so drop it */
761163953Srrs					if (chk->data) {
762169420Srrs						(void)sctp_release_pr_sctp_chunk(stcb,
763163953Srrs						    chk,
764163953Srrs						    (SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_SENT),
765172090Srrs						    &stcb->asoc.sent_queue, SCTP_SO_NOT_LOCKED);
766163953Srrs					}
767163953Srrs				}
768163953Srrs				continue;
769163953Srrs			}
770163953Srrs			if (PR_SCTP_RTX_ENABLED(chk->flags)) {
771163953Srrs				/* Has it been retransmitted tv_sec times? */
772163953Srrs				if (chk->snd_count > chk->rec.data.timetodrop.tv_sec) {
773163953Srrs					if (chk->data) {
774169420Srrs						(void)sctp_release_pr_sctp_chunk(stcb,
775163953Srrs						    chk,
776163953Srrs						    (SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_SENT),
777172090Srrs						    &stcb->asoc.sent_queue, SCTP_SO_NOT_LOCKED);
778163953Srrs					}
779163953Srrs				}
780163953Srrs				continue;
781163953Srrs			}
782168709Srrs			if (chk->sent < SCTP_DATAGRAM_RESEND) {
783163953Srrs				sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
784163953Srrs				num_mk++;
785163953Srrs				if (fir == 0) {
786163953Srrs					fir = 1;
787163953Srrs					tsnfirst = chk->rec.data.TSN_seq;
788163953Srrs				}
789163953Srrs				tsnlast = chk->rec.data.TSN_seq;
790179783Srrs				if (SCTP_BASE_SYSCTL(sctp_logging_level) & (SCTP_EARLYFR_LOGGING_ENABLE | SCTP_FR_LOGGING_ENABLE)) {
791170744Srrs					sctp_log_fr(chk->rec.data.TSN_seq, chk->snd_count,
792170744Srrs					    0, SCTP_FR_T3_MARKED);
793170744Srrs				}
794168709Srrs				if (chk->rec.data.chunk_was_revoked) {
795168709Srrs					/* deflate the cwnd */
796168709Srrs					chk->whoTo->cwnd -= chk->book_size;
797168709Srrs					chk->rec.data.chunk_was_revoked = 0;
798168709Srrs				}
799168709Srrs				net->marked_retrans++;
800168709Srrs				stcb->asoc.marked_retrans++;
801179783Srrs				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
802170744Srrs					sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_RSND_TO,
803170744Srrs					    chk->whoTo->flight_size,
804170744Srrs					    chk->book_size,
805170744Srrs					    (uintptr_t) chk->whoTo,
806170744Srrs					    chk->rec.data.TSN_seq);
807170744Srrs				}
808168709Srrs				sctp_flight_size_decrease(chk);
809168709Srrs				sctp_total_flight_decrease(stcb, chk);
810168709Srrs				stcb->asoc.peers_rwnd += chk->send_size;
811179783Srrs				stcb->asoc.peers_rwnd += SCTP_BASE_SYSCTL(sctp_peer_chunk_oh);
812163953Srrs			}
813163953Srrs			chk->sent = SCTP_DATAGRAM_RESEND;
814163953Srrs			SCTP_STAT_INCR(sctps_markedretrans);
815165220Srrs
816163953Srrs			/* reset the TSN for striking and other FR stuff */
817163953Srrs			chk->rec.data.doing_fast_retransmit = 0;
818163953Srrs			/* Clear any time so NO RTT is being done */
819163953Srrs			chk->do_rtt = 0;
820163953Srrs			if (alt != net) {
821163953Srrs				sctp_free_remote_addr(chk->whoTo);
822163953Srrs				chk->no_fr_allowed = 1;
823163953Srrs				chk->whoTo = alt;
824163953Srrs				atomic_add_int(&alt->ref_count, 1);
825163953Srrs			} else {
826163953Srrs				chk->no_fr_allowed = 0;
827163953Srrs				if (TAILQ_EMPTY(&stcb->asoc.send_queue)) {
828163953Srrs					chk->rec.data.fast_retran_tsn = stcb->asoc.sending_seq;
829163953Srrs				} else {
830163953Srrs					chk->rec.data.fast_retran_tsn = (TAILQ_FIRST(&stcb->asoc.send_queue))->rec.data.TSN_seq;
831163953Srrs				}
832163953Srrs			}
833170181Srrs			/*
834170181Srrs			 * CMT: Do not allow FRs on retransmitted TSNs.
835170181Srrs			 */
836179783Srrs			if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) == 1) {
837163953Srrs				chk->no_fr_allowed = 1;
838163953Srrs			}
839163953Srrs		} else if (chk->sent == SCTP_DATAGRAM_ACKED) {
840163953Srrs			/* remember highest acked one */
841163953Srrs			could_be_sent = chk;
842163953Srrs		}
843163953Srrs		if (chk->sent == SCTP_DATAGRAM_RESEND) {
844163953Srrs			cnt_mk++;
845163953Srrs		}
846163953Srrs	}
847168709Srrs	if ((orig_flight - net->flight_size) != (orig_tf - stcb->asoc.total_flight)) {
848168709Srrs		/* we did not subtract the same things? */
849168709Srrs		audit_tf = 1;
850168709Srrs	}
851179783Srrs	if (SCTP_BASE_SYSCTL(sctp_logging_level) & (SCTP_EARLYFR_LOGGING_ENABLE | SCTP_FR_LOGGING_ENABLE)) {
852170744Srrs		sctp_log_fr(tsnfirst, tsnlast, num_mk, SCTP_FR_T3_TIMEOUT);
853170744Srrs	}
854163953Srrs#ifdef SCTP_DEBUG
855169420Srrs	if (num_mk) {
856169420Srrs		SCTPDBG(SCTP_DEBUG_TIMER1, "LAST TSN marked was %x\n",
857169420Srrs		    tsnlast);
858169420Srrs		SCTPDBG(SCTP_DEBUG_TIMER1, "Num marked for retransmission was %d peer-rwd:%ld\n",
859169420Srrs		    num_mk, (u_long)stcb->asoc.peers_rwnd);
860169420Srrs		SCTPDBG(SCTP_DEBUG_TIMER1, "LAST TSN marked was %x\n",
861169420Srrs		    tsnlast);
862169420Srrs		SCTPDBG(SCTP_DEBUG_TIMER1, "Num marked for retransmission was %d peer-rwd:%d\n",
863169420Srrs		    num_mk,
864169420Srrs		    (int)stcb->asoc.peers_rwnd);
865163953Srrs	}
866163953Srrs#endif
867163953Srrs	*num_marked = num_mk;
868163953Srrs	if ((stcb->asoc.sent_queue_retran_cnt == 0) && (could_be_sent)) {
869163953Srrs		/* fix it so we retransmit the highest acked anyway */
870163953Srrs		sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
871163953Srrs		cnt_mk++;
872163953Srrs		could_be_sent->sent = SCTP_DATAGRAM_RESEND;
873163953Srrs	}
874163953Srrs	if (stcb->asoc.sent_queue_retran_cnt != cnt_mk) {
875165220Srrs#ifdef INVARIANTS
876171477Srrs		SCTP_PRINTF("Local Audit says there are %d for retran asoc cnt:%d we marked:%d this time\n",
877171477Srrs		    cnt_mk, stcb->asoc.sent_queue_retran_cnt, num_mk);
878163953Srrs#endif
879163953Srrs#ifndef SCTP_AUDITING_ENABLED
880163953Srrs		stcb->asoc.sent_queue_retran_cnt = cnt_mk;
881163953Srrs#endif
882163953Srrs	}
883163953Srrs	/* Now check for a ECN Echo that may be stranded */
884163953Srrs	TAILQ_FOREACH(chk, &stcb->asoc.control_send_queue, sctp_next) {
885163953Srrs		if ((chk->whoTo == net) &&
886163953Srrs		    (chk->rec.chunk_id.id == SCTP_ECN_ECHO)) {
887163953Srrs			sctp_free_remote_addr(chk->whoTo);
888163953Srrs			chk->whoTo = alt;
889163953Srrs			if (chk->sent != SCTP_DATAGRAM_RESEND) {
890163953Srrs				chk->sent = SCTP_DATAGRAM_RESEND;
891163953Srrs				sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
892163953Srrs			}
893163953Srrs			atomic_add_int(&alt->ref_count, 1);
894163953Srrs		}
895163953Srrs	}
896163953Srrs	if (audit_tf) {
897169420Srrs		SCTPDBG(SCTP_DEBUG_TIMER4,
898169420Srrs		    "Audit total flight due to negative value net:%p\n",
899169420Srrs		    net);
900163953Srrs		stcb->asoc.total_flight = 0;
901163953Srrs		stcb->asoc.total_flight_count = 0;
902163953Srrs		/* Clear all networks flight size */
903163953Srrs		TAILQ_FOREACH(lnets, &stcb->asoc.nets, sctp_next) {
904163953Srrs			lnets->flight_size = 0;
905169420Srrs			SCTPDBG(SCTP_DEBUG_TIMER4,
906169420Srrs			    "Net:%p c-f cwnd:%d ssthresh:%d\n",
907169420Srrs			    lnets, lnets->cwnd, lnets->ssthresh);
908163953Srrs		}
909163953Srrs		TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
910163953Srrs			if (chk->sent < SCTP_DATAGRAM_RESEND) {
911179783Srrs				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
912170744Srrs					sctp_misc_ints(SCTP_FLIGHT_LOG_UP,
913170744Srrs					    chk->whoTo->flight_size,
914170744Srrs					    chk->book_size,
915170744Srrs					    (uintptr_t) chk->whoTo,
916170744Srrs					    chk->rec.data.TSN_seq);
917170744Srrs				}
918168709Srrs				sctp_flight_size_increase(chk);
919168709Srrs				sctp_total_flight_increase(stcb, chk);
920163953Srrs			}
921163953Srrs		}
922163953Srrs	}
923163953Srrs	/*
924163953Srrs	 * Setup the ecn nonce re-sync point. We do this since
925163953Srrs	 * retranmissions are NOT setup for ECN. This means that do to
926163953Srrs	 * Karn's rule, we don't know the total of the peers ecn bits.
927163953Srrs	 */
928163953Srrs	chk = TAILQ_FIRST(&stcb->asoc.send_queue);
929163953Srrs	if (chk == NULL) {
930163953Srrs		stcb->asoc.nonce_resync_tsn = stcb->asoc.sending_seq;
931163953Srrs	} else {
932163953Srrs		stcb->asoc.nonce_resync_tsn = chk->rec.data.TSN_seq;
933163953Srrs	}
934163953Srrs	stcb->asoc.nonce_wait_for_ecne = 0;
935163953Srrs	stcb->asoc.nonce_sum_check = 0;
936163953Srrs	/* We return 1 if we only have a window probe outstanding */
937163953Srrs	return (0);
938163953Srrs}
939163953Srrs
940163953Srrsstatic void
941163953Srrssctp_move_all_chunks_to_alt(struct sctp_tcb *stcb,
942163953Srrs    struct sctp_nets *net,
943163953Srrs    struct sctp_nets *alt)
944163953Srrs{
945163953Srrs	struct sctp_association *asoc;
946163953Srrs	struct sctp_stream_out *outs;
947163953Srrs	struct sctp_tmit_chunk *chk;
948163953Srrs	struct sctp_stream_queue_pending *sp;
949163953Srrs
950163953Srrs	if (net == alt)
951163953Srrs		/* nothing to do */
952163953Srrs		return;
953163953Srrs
954163953Srrs	asoc = &stcb->asoc;
955163953Srrs
956163953Srrs	/*
957163953Srrs	 * now through all the streams checking for chunks sent to our bad
958163953Srrs	 * network.
959163953Srrs	 */
960163953Srrs	TAILQ_FOREACH(outs, &asoc->out_wheel, next_spoke) {
961163953Srrs		/* now clean up any chunks here */
962163953Srrs		TAILQ_FOREACH(sp, &outs->outqueue, next) {
963163953Srrs			if (sp->net == net) {
964163953Srrs				sctp_free_remote_addr(sp->net);
965163953Srrs				sp->net = alt;
966163953Srrs				atomic_add_int(&alt->ref_count, 1);
967163953Srrs			}
968163953Srrs		}
969163953Srrs	}
970163953Srrs	/* Now check the pending queue */
971163953Srrs	TAILQ_FOREACH(chk, &asoc->send_queue, sctp_next) {
972163953Srrs		if (chk->whoTo == net) {
973163953Srrs			sctp_free_remote_addr(chk->whoTo);
974163953Srrs			chk->whoTo = alt;
975163953Srrs			atomic_add_int(&alt->ref_count, 1);
976163953Srrs		}
977163953Srrs	}
978163953Srrs
979163953Srrs}
980163953Srrs
981163953Srrsint
982163953Srrssctp_t3rxt_timer(struct sctp_inpcb *inp,
983163953Srrs    struct sctp_tcb *stcb,
984163953Srrs    struct sctp_nets *net)
985163953Srrs{
986163953Srrs	struct sctp_nets *alt;
987163953Srrs	int win_probe, num_mk;
988163953Srrs
989179783Srrs	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
990170744Srrs		sctp_log_fr(0, 0, 0, SCTP_FR_T3_TIMEOUT);
991170744Srrs	}
992179783Srrs	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
993163953Srrs		struct sctp_nets *lnet;
994163953Srrs
995163953Srrs		TAILQ_FOREACH(lnet, &stcb->asoc.nets, sctp_next) {
996163953Srrs			if (net == lnet) {
997163953Srrs				sctp_log_cwnd(stcb, lnet, 1, SCTP_CWND_LOG_FROM_T3);
998163953Srrs			} else {
999163953Srrs				sctp_log_cwnd(stcb, lnet, 0, SCTP_CWND_LOG_FROM_T3);
1000163953Srrs			}
1001163953Srrs		}
1002163953Srrs	}
1003163953Srrs	/* Find an alternate and mark those for retransmission */
1004163953Srrs	if ((stcb->asoc.peers_rwnd == 0) &&
1005163953Srrs	    (stcb->asoc.total_flight < net->mtu)) {
1006163953Srrs		SCTP_STAT_INCR(sctps_timowindowprobe);
1007163953Srrs		win_probe = 1;
1008163953Srrs	} else {
1009163953Srrs		win_probe = 0;
1010163953Srrs	}
1011168709Srrs
1012171440Srrs	/*
1013171440Srrs	 * JRS 5/14/07 - If CMT PF is on and the destination if not already
1014171440Srrs	 * in PF state, set the destination to PF state and store the
1015171440Srrs	 * current time as the time that the destination was last active. In
1016171440Srrs	 * addition, find an alternate destination with PF-based
1017171440Srrs	 * find_alt_net().
1018171440Srrs	 */
1019179783Srrs	if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) && SCTP_BASE_SYSCTL(sctp_cmt_pf)) {
1020171440Srrs		if ((net->dest_state & SCTP_ADDR_PF) != SCTP_ADDR_PF) {
1021171440Srrs			net->dest_state |= SCTP_ADDR_PF;
1022171477Srrs			net->last_active = sctp_get_tick_count();
1023171440Srrs			SCTPDBG(SCTP_DEBUG_TIMER4, "Destination %p moved from active to PF.\n",
1024171440Srrs			    net);
1025171440Srrs		}
1026171440Srrs		alt = sctp_find_alternate_net(stcb, net, 2);
1027179783Srrs	} else if (SCTP_BASE_SYSCTL(sctp_cmt_on_off)) {
1028168709Srrs		/*
1029168709Srrs		 * CMT: Using RTX_SSTHRESH policy for CMT. If CMT is being
1030168709Srrs		 * used, then pick dest with largest ssthresh for any
1031168709Srrs		 * retransmission.
1032168709Srrs		 */
1033168709Srrs		alt = net;
1034168709Srrs		alt = sctp_find_alternate_net(stcb, alt, 1);
1035168709Srrs		/*
1036168709Srrs		 * CUCv2: If a different dest is picked for the
1037168709Srrs		 * retransmission, then new (rtx-)pseudo_cumack needs to be
1038168709Srrs		 * tracked for orig dest. Let CUCv2 track new (rtx-)
1039168709Srrs		 * pseudo-cumack always.
1040168709Srrs		 */
1041168709Srrs		net->find_pseudo_cumack = 1;
1042168709Srrs		net->find_rtx_pseudo_cumack = 1;
1043168709Srrs	} else {		/* CMT is OFF */
1044168709Srrs		alt = sctp_find_alternate_net(stcb, net, 0);
1045168709Srrs	}
1046168709Srrs
1047169420Srrs	(void)sctp_mark_all_for_resend(stcb, net, alt, win_probe, &num_mk);
1048163953Srrs	/* FR Loss recovery just ended with the T3. */
1049163953Srrs	stcb->asoc.fast_retran_loss_recovery = 0;
1050163953Srrs
1051163953Srrs	/* CMT FR loss recovery ended with the T3 */
1052163953Srrs	net->fast_retran_loss_recovery = 0;
1053163953Srrs
1054163953Srrs	/*
1055163953Srrs	 * setup the sat loss recovery that prevents satellite cwnd advance.
1056163953Srrs	 */
1057163953Srrs	stcb->asoc.sat_t3_loss_recovery = 1;
1058163953Srrs	stcb->asoc.sat_t3_recovery_tsn = stcb->asoc.sending_seq;
1059163953Srrs
1060163953Srrs	/* Backoff the timer and cwnd */
1061163953Srrs	sctp_backoff_on_timeout(stcb, net, win_probe, num_mk);
1062163953Srrs	if (win_probe == 0) {
1063163953Srrs		/* We don't do normal threshold management on window probes */
1064163953Srrs		if (sctp_threshold_management(inp, stcb, net,
1065163953Srrs		    stcb->asoc.max_send_times)) {
1066163953Srrs			/* Association was destroyed */
1067163953Srrs			return (1);
1068163953Srrs		} else {
1069163953Srrs			if (net != stcb->asoc.primary_destination) {
1070163953Srrs				/* send a immediate HB if our RTO is stale */
1071163953Srrs				struct timeval now;
1072163953Srrs				unsigned int ms_goneby;
1073163953Srrs
1074169378Srrs				(void)SCTP_GETTIME_TIMEVAL(&now);
1075163953Srrs				if (net->last_sent_time.tv_sec) {
1076163953Srrs					ms_goneby = (now.tv_sec - net->last_sent_time.tv_sec) * 1000;
1077163953Srrs				} else {
1078163953Srrs					ms_goneby = 0;
1079163953Srrs				}
1080163953Srrs				if ((ms_goneby > net->RTO) || (net->RTO == 0)) {
1081163953Srrs					/*
1082163953Srrs					 * no recent feed back in an RTO or
1083163953Srrs					 * more, request a RTT update
1084163953Srrs					 */
1085171440Srrs					if (sctp_send_hb(stcb, 1, net) < 0)
1086171440Srrs						return 1;
1087163953Srrs				}
1088163953Srrs			}
1089163953Srrs		}
1090163953Srrs	} else {
1091163953Srrs		/*
1092163953Srrs		 * For a window probe we don't penalize the net's but only
1093163953Srrs		 * the association. This may fail it if SACKs are not coming
1094163953Srrs		 * back. If sack's are coming with rwnd locked at 0, we will
1095163953Srrs		 * continue to hold things waiting for rwnd to raise
1096163953Srrs		 */
1097163953Srrs		if (sctp_threshold_management(inp, stcb, NULL,
1098163953Srrs		    stcb->asoc.max_send_times)) {
1099163953Srrs			/* Association was destroyed */
1100163953Srrs			return (1);
1101163953Srrs		}
1102163953Srrs	}
1103163953Srrs	if (net->dest_state & SCTP_ADDR_NOT_REACHABLE) {
1104163953Srrs		/* Move all pending over too */
1105163953Srrs		sctp_move_all_chunks_to_alt(stcb, net, alt);
1106169352Srrs
1107169352Srrs		/*
1108169352Srrs		 * Get the address that failed, to force a new src address
1109169352Srrs		 * selecton and a route allocation.
1110169352Srrs		 */
1111169352Srrs		if (net->ro._s_addr) {
1112169352Srrs			sctp_free_ifa(net->ro._s_addr);
1113169352Srrs			net->ro._s_addr = NULL;
1114169352Srrs		}
1115169352Srrs		net->src_addr_selected = 0;
1116169352Srrs
1117169352Srrs		/* Force a route allocation too */
1118169352Srrs		if (net->ro.ro_rt) {
1119169352Srrs			RTFREE(net->ro.ro_rt);
1120169352Srrs			net->ro.ro_rt = NULL;
1121169352Srrs		}
1122163953Srrs		/* Was it our primary? */
1123163953Srrs		if ((stcb->asoc.primary_destination == net) && (alt != net)) {
1124163953Srrs			/*
1125163953Srrs			 * Yes, note it as such and find an alternate note:
1126163953Srrs			 * this means HB code must use this to resent the
1127163953Srrs			 * primary if it goes active AND if someone does a
1128163953Srrs			 * change-primary then this flag must be cleared
1129163953Srrs			 * from any net structures.
1130163953Srrs			 */
1131163953Srrs			if (sctp_set_primary_addr(stcb,
1132163953Srrs			    (struct sockaddr *)NULL,
1133163953Srrs			    alt) == 0) {
1134163953Srrs				net->dest_state |= SCTP_ADDR_WAS_PRIMARY;
1135163953Srrs			}
1136163953Srrs		}
1137179783Srrs	} else if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) && SCTP_BASE_SYSCTL(sctp_cmt_pf) && (net->dest_state & SCTP_ADDR_PF) == SCTP_ADDR_PF) {
1138171440Srrs		/*
1139171440Srrs		 * JRS 5/14/07 - If the destination hasn't failed completely
1140171440Srrs		 * but is in PF state, a PF-heartbeat needs to be sent
1141171440Srrs		 * manually.
1142171440Srrs		 */
1143171440Srrs		if (sctp_send_hb(stcb, 1, net) < 0)
1144171440Srrs			return 1;
1145163953Srrs	}
1146163953Srrs	/*
1147163953Srrs	 * Special case for cookie-echo'ed case, we don't do output but must
1148163953Srrs	 * await the COOKIE-ACK before retransmission
1149163953Srrs	 */
1150163953Srrs	if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_ECHOED) {
1151163953Srrs		/*
1152163953Srrs		 * Here we just reset the timer and start again since we
1153163953Srrs		 * have not established the asoc
1154163953Srrs		 */
1155163953Srrs		sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
1156163953Srrs		return (0);
1157163953Srrs	}
1158163953Srrs	if (stcb->asoc.peer_supports_prsctp) {
1159163953Srrs		struct sctp_tmit_chunk *lchk;
1160163953Srrs
1161163953Srrs		lchk = sctp_try_advance_peer_ack_point(stcb, &stcb->asoc);
1162163953Srrs		/* C3. See if we need to send a Fwd-TSN */
1163163953Srrs		if (compare_with_wrap(stcb->asoc.advanced_peer_ack_point,
1164163953Srrs		    stcb->asoc.last_acked_seq, MAX_TSN)) {
1165163953Srrs			/*
1166163953Srrs			 * ISSUE with ECN, see FWD-TSN processing for notes
1167163953Srrs			 * on issues that will occur when the ECN NONCE
1168163953Srrs			 * stuff is put into SCTP for cross checking.
1169163953Srrs			 */
1170163953Srrs			send_forward_tsn(stcb, &stcb->asoc);
1171163953Srrs			if (lchk) {
1172163953Srrs				/* Assure a timer is up */
1173163953Srrs				sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, stcb, lchk->whoTo);
1174163953Srrs			}
1175163953Srrs		}
1176163953Srrs	}
1177179783Srrs	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_MONITOR_ENABLE) {
1178170744Srrs		sctp_log_cwnd(stcb, net, net->cwnd, SCTP_CWND_LOG_FROM_RTX);
1179170744Srrs	}
1180163953Srrs	return (0);
1181163953Srrs}
1182163953Srrs
1183163953Srrsint
1184163953Srrssctp_t1init_timer(struct sctp_inpcb *inp,
1185163953Srrs    struct sctp_tcb *stcb,
1186163953Srrs    struct sctp_nets *net)
1187163953Srrs{
1188163953Srrs	/* bump the thresholds */
1189163953Srrs	if (stcb->asoc.delayed_connection) {
1190163953Srrs		/*
1191163953Srrs		 * special hook for delayed connection. The library did NOT
1192163953Srrs		 * complete the rest of its sends.
1193163953Srrs		 */
1194163953Srrs		stcb->asoc.delayed_connection = 0;
1195172090Srrs		sctp_send_initiate(inp, stcb, SCTP_SO_NOT_LOCKED);
1196163953Srrs		return (0);
1197163953Srrs	}
1198163953Srrs	if (SCTP_GET_STATE((&stcb->asoc)) != SCTP_STATE_COOKIE_WAIT) {
1199163953Srrs		return (0);
1200163953Srrs	}
1201163953Srrs	if (sctp_threshold_management(inp, stcb, net,
1202163953Srrs	    stcb->asoc.max_init_times)) {
1203163953Srrs		/* Association was destroyed */
1204163953Srrs		return (1);
1205163953Srrs	}
1206163953Srrs	stcb->asoc.dropped_special_cnt = 0;
1207163953Srrs	sctp_backoff_on_timeout(stcb, stcb->asoc.primary_destination, 1, 0);
1208163953Srrs	if (stcb->asoc.initial_init_rto_max < net->RTO) {
1209163953Srrs		net->RTO = stcb->asoc.initial_init_rto_max;
1210163953Srrs	}
1211163953Srrs	if (stcb->asoc.numnets > 1) {
1212163953Srrs		/* If we have more than one addr use it */
1213163953Srrs		struct sctp_nets *alt;
1214163953Srrs
1215163953Srrs		alt = sctp_find_alternate_net(stcb, stcb->asoc.primary_destination, 0);
1216163953Srrs		if ((alt != NULL) && (alt != stcb->asoc.primary_destination)) {
1217163953Srrs			sctp_move_all_chunks_to_alt(stcb, stcb->asoc.primary_destination, alt);
1218163953Srrs			stcb->asoc.primary_destination = alt;
1219163953Srrs		}
1220163953Srrs	}
1221163953Srrs	/* Send out a new init */
1222172090Srrs	sctp_send_initiate(inp, stcb, SCTP_SO_NOT_LOCKED);
1223163953Srrs	return (0);
1224163953Srrs}
1225163953Srrs
1226163953Srrs/*
1227163953Srrs * For cookie and asconf we actually need to find and mark for resend, then
1228163953Srrs * increment the resend counter (after all the threshold management stuff of
1229163953Srrs * course).
1230163953Srrs */
1231163953Srrsint
1232163953Srrssctp_cookie_timer(struct sctp_inpcb *inp,
1233163953Srrs    struct sctp_tcb *stcb,
1234163953Srrs    struct sctp_nets *net)
1235163953Srrs{
1236163953Srrs	struct sctp_nets *alt;
1237163953Srrs	struct sctp_tmit_chunk *cookie;
1238163953Srrs
1239163953Srrs	/* first before all else we must find the cookie */
1240163953Srrs	TAILQ_FOREACH(cookie, &stcb->asoc.control_send_queue, sctp_next) {
1241163953Srrs		if (cookie->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
1242163953Srrs			break;
1243163953Srrs		}
1244163953Srrs	}
1245163953Srrs	if (cookie == NULL) {
1246163953Srrs		if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_ECHOED) {
1247163953Srrs			/* FOOBAR! */
1248163953Srrs			struct mbuf *oper;
1249163953Srrs
1250163953Srrs			oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
1251163953Srrs			    0, M_DONTWAIT, 1, MT_DATA);
1252163953Srrs			if (oper) {
1253163953Srrs				struct sctp_paramhdr *ph;
1254163953Srrs				uint32_t *ippp;
1255163953Srrs
1256165647Srrs				SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
1257163953Srrs				    sizeof(uint32_t);
1258163953Srrs				ph = mtod(oper, struct sctp_paramhdr *);
1259163953Srrs				ph->param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1260165647Srrs				ph->param_length = htons(SCTP_BUF_LEN(oper));
1261163953Srrs				ippp = (uint32_t *) (ph + 1);
1262171440Srrs				*ippp = htonl(SCTP_FROM_SCTP_TIMER + SCTP_LOC_3);
1263163953Srrs			}
1264171440Srrs			inp->last_abort_code = SCTP_FROM_SCTP_TIMER + SCTP_LOC_4;
1265163953Srrs			sctp_abort_an_association(inp, stcb, SCTP_INTERNAL_ERROR,
1266172090Srrs			    oper, SCTP_SO_NOT_LOCKED);
1267163953Srrs		} else {
1268165220Srrs#ifdef INVARIANTS
1269163953Srrs			panic("Cookie timer expires in wrong state?");
1270163953Srrs#else
1271169420Srrs			SCTP_PRINTF("Strange in state %d not cookie-echoed yet c-e timer expires?\n", SCTP_GET_STATE(&stcb->asoc));
1272163953Srrs			return (0);
1273163953Srrs#endif
1274163953Srrs		}
1275163953Srrs		return (0);
1276163953Srrs	}
1277163953Srrs	/* Ok we found the cookie, threshold management next */
1278163953Srrs	if (sctp_threshold_management(inp, stcb, cookie->whoTo,
1279163953Srrs	    stcb->asoc.max_init_times)) {
1280163953Srrs		/* Assoc is over */
1281163953Srrs		return (1);
1282163953Srrs	}
1283163953Srrs	/*
1284163953Srrs	 * cleared theshold management now lets backoff the address & select
1285163953Srrs	 * an alternate
1286163953Srrs	 */
1287163953Srrs	stcb->asoc.dropped_special_cnt = 0;
1288163953Srrs	sctp_backoff_on_timeout(stcb, cookie->whoTo, 1, 0);
1289163953Srrs	alt = sctp_find_alternate_net(stcb, cookie->whoTo, 0);
1290163953Srrs	if (alt != cookie->whoTo) {
1291163953Srrs		sctp_free_remote_addr(cookie->whoTo);
1292163953Srrs		cookie->whoTo = alt;
1293163953Srrs		atomic_add_int(&alt->ref_count, 1);
1294163953Srrs	}
1295163953Srrs	/* Now mark the retran info */
1296163953Srrs	if (cookie->sent != SCTP_DATAGRAM_RESEND) {
1297163953Srrs		sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
1298163953Srrs	}
1299163953Srrs	cookie->sent = SCTP_DATAGRAM_RESEND;
1300163953Srrs	/*
1301163953Srrs	 * Now call the output routine to kick out the cookie again, Note we
1302163953Srrs	 * don't mark any chunks for retran so that FR will need to kick in
1303163953Srrs	 * to move these (or a send timer).
1304163953Srrs	 */
1305163953Srrs	return (0);
1306163953Srrs}
1307163953Srrs
1308163953Srrsint
1309163953Srrssctp_strreset_timer(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
1310163953Srrs    struct sctp_nets *net)
1311163953Srrs{
1312163953Srrs	struct sctp_nets *alt;
1313163953Srrs	struct sctp_tmit_chunk *strrst = NULL, *chk = NULL;
1314163953Srrs
1315163953Srrs	if (stcb->asoc.stream_reset_outstanding == 0) {
1316163953Srrs		return (0);
1317163953Srrs	}
1318163953Srrs	/* find the existing STRRESET, we use the seq number we sent out on */
1319169420Srrs	(void)sctp_find_stream_reset(stcb, stcb->asoc.str_reset_seq_out, &strrst);
1320163953Srrs	if (strrst == NULL) {
1321163953Srrs		return (0);
1322163953Srrs	}
1323163953Srrs	/* do threshold management */
1324163953Srrs	if (sctp_threshold_management(inp, stcb, strrst->whoTo,
1325163953Srrs	    stcb->asoc.max_send_times)) {
1326163953Srrs		/* Assoc is over */
1327163953Srrs		return (1);
1328163953Srrs	}
1329163953Srrs	/*
1330163953Srrs	 * cleared theshold management now lets backoff the address & select
1331163953Srrs	 * an alternate
1332163953Srrs	 */
1333163953Srrs	sctp_backoff_on_timeout(stcb, strrst->whoTo, 1, 0);
1334163953Srrs	alt = sctp_find_alternate_net(stcb, strrst->whoTo, 0);
1335163953Srrs	sctp_free_remote_addr(strrst->whoTo);
1336163953Srrs	strrst->whoTo = alt;
1337163953Srrs	atomic_add_int(&alt->ref_count, 1);
1338163953Srrs
1339163953Srrs	/* See if a ECN Echo is also stranded */
1340163953Srrs	TAILQ_FOREACH(chk, &stcb->asoc.control_send_queue, sctp_next) {
1341163953Srrs		if ((chk->whoTo == net) &&
1342163953Srrs		    (chk->rec.chunk_id.id == SCTP_ECN_ECHO)) {
1343163953Srrs			sctp_free_remote_addr(chk->whoTo);
1344163953Srrs			if (chk->sent != SCTP_DATAGRAM_RESEND) {
1345163953Srrs				chk->sent = SCTP_DATAGRAM_RESEND;
1346163953Srrs				sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
1347163953Srrs			}
1348163953Srrs			chk->whoTo = alt;
1349163953Srrs			atomic_add_int(&alt->ref_count, 1);
1350163953Srrs		}
1351163953Srrs	}
1352163953Srrs	if (net->dest_state & SCTP_ADDR_NOT_REACHABLE) {
1353163953Srrs		/*
1354163953Srrs		 * If the address went un-reachable, we need to move to
1355163953Srrs		 * alternates for ALL chk's in queue
1356163953Srrs		 */
1357163953Srrs		sctp_move_all_chunks_to_alt(stcb, net, alt);
1358163953Srrs	}
1359163953Srrs	/* mark the retran info */
1360163953Srrs	if (strrst->sent != SCTP_DATAGRAM_RESEND)
1361163953Srrs		sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
1362163953Srrs	strrst->sent = SCTP_DATAGRAM_RESEND;
1363163953Srrs
1364163953Srrs	/* restart the timer */
1365163953Srrs	sctp_timer_start(SCTP_TIMER_TYPE_STRRESET, inp, stcb, strrst->whoTo);
1366163953Srrs	return (0);
1367163953Srrs}
1368163953Srrs
1369163953Srrsint
1370163953Srrssctp_asconf_timer(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
1371163953Srrs    struct sctp_nets *net)
1372163953Srrs{
1373163953Srrs	struct sctp_nets *alt;
1374179157Srrs	struct sctp_tmit_chunk *asconf, *chk, *nchk;
1375163953Srrs
1376171572Srrs	/* is this a first send, or a retransmission? */
1377179157Srrs	if (TAILQ_EMPTY(&stcb->asoc.asconf_send_queue)) {
1378163953Srrs		/* compose a new ASCONF chunk and send it */
1379172190Srrs		sctp_send_asconf(stcb, net, SCTP_ADDR_NOT_LOCKED);
1380163953Srrs	} else {
1381171572Srrs		/*
1382171572Srrs		 * Retransmission of the existing ASCONF is needed
1383171572Srrs		 */
1384163953Srrs
1385163953Srrs		/* find the existing ASCONF */
1386179157Srrs		asconf = TAILQ_FIRST(&stcb->asoc.asconf_send_queue);
1387163953Srrs		if (asconf == NULL) {
1388163953Srrs			return (0);
1389163953Srrs		}
1390163953Srrs		/* do threshold management */
1391163953Srrs		if (sctp_threshold_management(inp, stcb, asconf->whoTo,
1392163953Srrs		    stcb->asoc.max_send_times)) {
1393163953Srrs			/* Assoc is over */
1394163953Srrs			return (1);
1395163953Srrs		}
1396163953Srrs		if (asconf->snd_count > stcb->asoc.max_send_times) {
1397163953Srrs			/*
1398171572Srrs			 * Something is rotten: our peer is not responding
1399171572Srrs			 * to ASCONFs but apparently is to other chunks.
1400171572Srrs			 * i.e. it is not properly handling the chunk type
1401171572Srrs			 * upper bits. Mark this peer as ASCONF incapable
1402171572Srrs			 * and cleanup.
1403163953Srrs			 */
1404169420Srrs			SCTPDBG(SCTP_DEBUG_TIMER1, "asconf_timer: Peer has not responded to our repeated ASCONFs\n");
1405163953Srrs			sctp_asconf_cleanup(stcb, net);
1406163953Srrs			return (0);
1407163953Srrs		}
1408163953Srrs		/*
1409171572Srrs		 * cleared threshold management, so now backoff the net and
1410171572Srrs		 * select an alternate
1411163953Srrs		 */
1412163953Srrs		sctp_backoff_on_timeout(stcb, asconf->whoTo, 1, 0);
1413163953Srrs		alt = sctp_find_alternate_net(stcb, asconf->whoTo, 0);
1414179157Srrs		if (asconf->whoTo != alt) {
1415179157Srrs			sctp_free_remote_addr(asconf->whoTo);
1416179157Srrs			asconf->whoTo = alt;
1417179157Srrs			atomic_add_int(&alt->ref_count, 1);
1418179157Srrs		}
1419171572Srrs		/* See if an ECN Echo is also stranded */
1420163953Srrs		TAILQ_FOREACH(chk, &stcb->asoc.control_send_queue, sctp_next) {
1421163953Srrs			if ((chk->whoTo == net) &&
1422163953Srrs			    (chk->rec.chunk_id.id == SCTP_ECN_ECHO)) {
1423163953Srrs				sctp_free_remote_addr(chk->whoTo);
1424163953Srrs				chk->whoTo = alt;
1425163953Srrs				if (chk->sent != SCTP_DATAGRAM_RESEND) {
1426163953Srrs					chk->sent = SCTP_DATAGRAM_RESEND;
1427163953Srrs					sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
1428163953Srrs				}
1429163953Srrs				atomic_add_int(&alt->ref_count, 1);
1430163953Srrs			}
1431163953Srrs		}
1432179157Srrs		for (chk = asconf; chk; chk = nchk) {
1433179157Srrs			nchk = TAILQ_NEXT(chk, sctp_next);
1434179157Srrs			if (chk->whoTo != alt) {
1435179157Srrs				sctp_free_remote_addr(chk->whoTo);
1436179157Srrs				chk->whoTo = alt;
1437179157Srrs				atomic_add_int(&alt->ref_count, 1);
1438179157Srrs			}
1439179157Srrs			if (asconf->sent != SCTP_DATAGRAM_RESEND && chk->sent != SCTP_DATAGRAM_UNSENT)
1440179157Srrs				sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
1441179157Srrs			chk->sent = SCTP_DATAGRAM_RESEND;
1442179157Srrs		}
1443163953Srrs		if (net->dest_state & SCTP_ADDR_NOT_REACHABLE) {
1444163953Srrs			/*
1445163953Srrs			 * If the address went un-reachable, we need to move
1446171572Srrs			 * to the alternate for ALL chunks in queue
1447163953Srrs			 */
1448163953Srrs			sctp_move_all_chunks_to_alt(stcb, net, alt);
1449179157Srrs			net = alt;
1450163953Srrs		}
1451163953Srrs		/* mark the retran info */
1452163953Srrs		if (asconf->sent != SCTP_DATAGRAM_RESEND)
1453163953Srrs			sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
1454163953Srrs		asconf->sent = SCTP_DATAGRAM_RESEND;
1455179157Srrs
1456179157Srrs		/* send another ASCONF if any and we can do */
1457179157Srrs		sctp_send_asconf(stcb, alt, SCTP_ADDR_NOT_LOCKED);
1458163953Srrs	}
1459163953Srrs	return (0);
1460163953Srrs}
1461163953Srrs
1462172091Srrs/* Mobility adaptation */
1463172156Srrsvoid
1464172091Srrssctp_delete_prim_timer(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
1465172091Srrs    struct sctp_nets *net)
1466172091Srrs{
1467172091Srrs	if (stcb->asoc.deleted_primary == NULL) {
1468172091Srrs		SCTPDBG(SCTP_DEBUG_ASCONF1, "delete_prim_timer: deleted_primary is not stored...\n");
1469172091Srrs		sctp_mobility_feature_off(inp, SCTP_MOBILITY_PRIM_DELETED);
1470172156Srrs		return;
1471172091Srrs	}
1472172091Srrs	SCTPDBG(SCTP_DEBUG_ASCONF1, "delete_prim_timer: finished to keep deleted primary ");
1473172091Srrs	SCTPDBG_ADDR(SCTP_DEBUG_ASCONF1, &stcb->asoc.deleted_primary->ro._l_addr.sa);
1474172091Srrs	sctp_free_remote_addr(stcb->asoc.deleted_primary);
1475172091Srrs	stcb->asoc.deleted_primary = NULL;
1476172091Srrs	sctp_mobility_feature_off(inp, SCTP_MOBILITY_PRIM_DELETED);
1477172156Srrs	return;
1478172091Srrs}
1479172091Srrs
1480163953Srrs/*
1481163953Srrs * For the shutdown and shutdown-ack, we do not keep one around on the
1482163953Srrs * control queue. This means we must generate a new one and call the general
1483163953Srrs * chunk output routine, AFTER having done threshold management.
1484163953Srrs */
1485163953Srrsint
1486163953Srrssctp_shutdown_timer(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
1487163953Srrs    struct sctp_nets *net)
1488163953Srrs{
1489163953Srrs	struct sctp_nets *alt;
1490163953Srrs
1491163953Srrs	/* first threshold managment */
1492163953Srrs	if (sctp_threshold_management(inp, stcb, net, stcb->asoc.max_send_times)) {
1493163953Srrs		/* Assoc is over */
1494163953Srrs		return (1);
1495163953Srrs	}
1496163953Srrs	/* second select an alternative */
1497163953Srrs	alt = sctp_find_alternate_net(stcb, net, 0);
1498163953Srrs
1499163953Srrs	/* third generate a shutdown into the queue for out net */
1500163953Srrs	if (alt) {
1501163953Srrs		sctp_send_shutdown(stcb, alt);
1502163953Srrs	} else {
1503163953Srrs		/*
1504163953Srrs		 * if alt is NULL, there is no dest to send to??
1505163953Srrs		 */
1506163953Srrs		return (0);
1507163953Srrs	}
1508163953Srrs	/* fourth restart timer */
1509163953Srrs	sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, inp, stcb, alt);
1510163953Srrs	return (0);
1511163953Srrs}
1512163953Srrs
1513163953Srrsint
1514163953Srrssctp_shutdownack_timer(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
1515163953Srrs    struct sctp_nets *net)
1516163953Srrs{
1517163953Srrs	struct sctp_nets *alt;
1518163953Srrs
1519163953Srrs	/* first threshold managment */
1520163953Srrs	if (sctp_threshold_management(inp, stcb, net, stcb->asoc.max_send_times)) {
1521163953Srrs		/* Assoc is over */
1522163953Srrs		return (1);
1523163953Srrs	}
1524163953Srrs	/* second select an alternative */
1525163953Srrs	alt = sctp_find_alternate_net(stcb, net, 0);
1526163953Srrs
1527163953Srrs	/* third generate a shutdown into the queue for out net */
1528163953Srrs	sctp_send_shutdown_ack(stcb, alt);
1529163953Srrs
1530163953Srrs	/* fourth restart timer */
1531163953Srrs	sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNACK, inp, stcb, alt);
1532163953Srrs	return (0);
1533163953Srrs}
1534163953Srrs
1535163953Srrsstatic void
1536163953Srrssctp_audit_stream_queues_for_size(struct sctp_inpcb *inp,
1537163953Srrs    struct sctp_tcb *stcb)
1538163953Srrs{
1539163953Srrs	struct sctp_stream_out *outs;
1540163953Srrs	struct sctp_stream_queue_pending *sp;
1541163953Srrs	unsigned int chks_in_queue = 0;
1542163953Srrs	int being_filled = 0;
1543163953Srrs
1544163953Srrs	/*
1545163953Srrs	 * This function is ONLY called when the send/sent queues are empty.
1546163953Srrs	 */
1547163953Srrs	if ((stcb == NULL) || (inp == NULL))
1548163953Srrs		return;
1549163953Srrs
1550163953Srrs	if (stcb->asoc.sent_queue_retran_cnt) {
1551169420Srrs		SCTP_PRINTF("Hmm, sent_queue_retran_cnt is non-zero %d\n",
1552163953Srrs		    stcb->asoc.sent_queue_retran_cnt);
1553163953Srrs		stcb->asoc.sent_queue_retran_cnt = 0;
1554163953Srrs	}
1555163953Srrs	SCTP_TCB_SEND_LOCK(stcb);
1556163953Srrs	if (TAILQ_EMPTY(&stcb->asoc.out_wheel)) {
1557163953Srrs		int i, cnt = 0;
1558163953Srrs
1559163953Srrs		/* Check to see if a spoke fell off the wheel */
1560163953Srrs		for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
1561163953Srrs			if (!TAILQ_EMPTY(&stcb->asoc.strmout[i].outqueue)) {
1562163953Srrs				sctp_insert_on_wheel(stcb, &stcb->asoc, &stcb->asoc.strmout[i], 1);
1563163953Srrs				cnt++;
1564163953Srrs			}
1565163953Srrs		}
1566163953Srrs		if (cnt) {
1567163953Srrs			/* yep, we lost a spoke or two */
1568169420Srrs			SCTP_PRINTF("Found an additional %d streams NOT on outwheel, corrected\n", cnt);
1569163953Srrs		} else {
1570163953Srrs			/* no spokes lost, */
1571163953Srrs			stcb->asoc.total_output_queue_size = 0;
1572163953Srrs		}
1573163953Srrs		SCTP_TCB_SEND_UNLOCK(stcb);
1574163953Srrs		return;
1575163953Srrs	}
1576163953Srrs	SCTP_TCB_SEND_UNLOCK(stcb);
1577163953Srrs	/* Check to see if some data queued, if so report it */
1578163953Srrs	TAILQ_FOREACH(outs, &stcb->asoc.out_wheel, next_spoke) {
1579163953Srrs		if (!TAILQ_EMPTY(&outs->outqueue)) {
1580163953Srrs			TAILQ_FOREACH(sp, &outs->outqueue, next) {
1581163953Srrs				if (sp->msg_is_complete)
1582163953Srrs					being_filled++;
1583163953Srrs				chks_in_queue++;
1584163953Srrs			}
1585163953Srrs		}
1586163953Srrs	}
1587163953Srrs	if (chks_in_queue != stcb->asoc.stream_queue_cnt) {
1588169420Srrs		SCTP_PRINTF("Hmm, stream queue cnt at %d I counted %d in stream out wheel\n",
1589163953Srrs		    stcb->asoc.stream_queue_cnt, chks_in_queue);
1590163953Srrs	}
1591163953Srrs	if (chks_in_queue) {
1592163953Srrs		/* call the output queue function */
1593172090Srrs		sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_T3, SCTP_SO_NOT_LOCKED);
1594163953Srrs		if ((TAILQ_EMPTY(&stcb->asoc.send_queue)) &&
1595163953Srrs		    (TAILQ_EMPTY(&stcb->asoc.sent_queue))) {
1596163953Srrs			/*
1597163953Srrs			 * Probably should go in and make it go back through
1598163953Srrs			 * and add fragments allowed
1599163953Srrs			 */
1600163953Srrs			if (being_filled == 0) {
1601169420Srrs				SCTP_PRINTF("Still nothing moved %d chunks are stuck\n",
1602163953Srrs				    chks_in_queue);
1603163953Srrs			}
1604163953Srrs		}
1605163953Srrs	} else {
1606169420Srrs		SCTP_PRINTF("Found no chunks on any queue tot:%lu\n",
1607163953Srrs		    (u_long)stcb->asoc.total_output_queue_size);
1608163953Srrs		stcb->asoc.total_output_queue_size = 0;
1609163953Srrs	}
1610163953Srrs}
1611163953Srrs
1612163953Srrsint
1613163953Srrssctp_heartbeat_timer(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
1614163953Srrs    struct sctp_nets *net, int cnt_of_unconf)
1615163953Srrs{
1616171440Srrs	int ret;
1617171440Srrs
1618163953Srrs	if (net) {
1619163953Srrs		if (net->hb_responded == 0) {
1620167598Srrs			if (net->ro._s_addr) {
1621167598Srrs				/*
1622167598Srrs				 * Invalidate the src address if we did not
1623167598Srrs				 * get a response last time.
1624167598Srrs				 */
1625167598Srrs				sctp_free_ifa(net->ro._s_addr);
1626167598Srrs				net->ro._s_addr = NULL;
1627167598Srrs				net->src_addr_selected = 0;
1628167598Srrs			}
1629163953Srrs			sctp_backoff_on_timeout(stcb, net, 1, 0);
1630163953Srrs		}
1631163953Srrs		/* Zero PBA, if it needs it */
1632163953Srrs		if (net->partial_bytes_acked) {
1633163953Srrs			net->partial_bytes_acked = 0;
1634163953Srrs		}
1635163953Srrs	}
1636163953Srrs	if ((stcb->asoc.total_output_queue_size > 0) &&
1637163953Srrs	    (TAILQ_EMPTY(&stcb->asoc.send_queue)) &&
1638163953Srrs	    (TAILQ_EMPTY(&stcb->asoc.sent_queue))) {
1639163953Srrs		sctp_audit_stream_queues_for_size(inp, stcb);
1640163953Srrs	}
1641163953Srrs	/* Send a new HB, this will do threshold managment, pick a new dest */
1642163953Srrs	if (cnt_of_unconf == 0) {
1643163953Srrs		if (sctp_send_hb(stcb, 0, NULL) < 0) {
1644163953Srrs			return (1);
1645163953Srrs		}
1646163953Srrs	} else {
1647163953Srrs		/*
1648163953Srrs		 * this will send out extra hb's up to maxburst if there are
1649163953Srrs		 * any unconfirmed addresses.
1650163953Srrs		 */
1651170056Srrs		uint32_t cnt_sent = 0;
1652163953Srrs
1653163953Srrs		TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1654163953Srrs			if ((net->dest_state & SCTP_ADDR_UNCONFIRMED) &&
1655163953Srrs			    (net->dest_state & SCTP_ADDR_REACHABLE)) {
1656163953Srrs				cnt_sent++;
1657167598Srrs				if (net->hb_responded == 0) {
1658167598Srrs					/* Did we respond last time? */
1659167598Srrs					if (net->ro._s_addr) {
1660167598Srrs						sctp_free_ifa(net->ro._s_addr);
1661167598Srrs						net->ro._s_addr = NULL;
1662167598Srrs						net->src_addr_selected = 0;
1663167598Srrs					}
1664167598Srrs				}
1665171440Srrs				ret = sctp_send_hb(stcb, 1, net);
1666171440Srrs				if (ret < 0)
1667171440Srrs					return 1;
1668171440Srrs				else if (ret == 0) {
1669163953Srrs					break;
1670163953Srrs				}
1671179783Srrs				if (cnt_sent >= SCTP_BASE_SYSCTL(sctp_hb_maxburst))
1672163953Srrs					break;
1673163953Srrs			}
1674163953Srrs		}
1675163953Srrs	}
1676163953Srrs	return (0);
1677163953Srrs}
1678163953Srrs
1679163953Srrsint
1680163953Srrssctp_is_hb_timer_running(struct sctp_tcb *stcb)
1681163953Srrs{
1682165647Srrs	if (SCTP_OS_TIMER_PENDING(&stcb->asoc.hb_timer.timer)) {
1683163953Srrs		/* its running */
1684163953Srrs		return (1);
1685163953Srrs	} else {
1686163953Srrs		/* nope */
1687163953Srrs		return (0);
1688163953Srrs	}
1689163953Srrs}
1690163953Srrs
1691163953Srrsint
1692163953Srrssctp_is_sack_timer_running(struct sctp_tcb *stcb)
1693163953Srrs{
1694165647Srrs	if (SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
1695163953Srrs		/* its running */
1696163953Srrs		return (1);
1697163953Srrs	} else {
1698163953Srrs		/* nope */
1699163953Srrs		return (0);
1700163953Srrs	}
1701163953Srrs}
1702163953Srrs
1703163953Srrs#define SCTP_NUMBER_OF_MTU_SIZES 18
1704163953Srrsstatic uint32_t mtu_sizes[] = {
1705163953Srrs	68,
1706163953Srrs	296,
1707163953Srrs	508,
1708163953Srrs	512,
1709163953Srrs	544,
1710163953Srrs	576,
1711163953Srrs	1006,
1712163953Srrs	1492,
1713163953Srrs	1500,
1714163953Srrs	1536,
1715163953Srrs	2002,
1716163953Srrs	2048,
1717163953Srrs	4352,
1718163953Srrs	4464,
1719163953Srrs	8166,
1720163953Srrs	17914,
1721163953Srrs	32000,
1722163953Srrs	65535
1723163953Srrs};
1724163953Srrs
1725163953Srrs
1726163953Srrsstatic uint32_t
1727163953Srrssctp_getnext_mtu(struct sctp_inpcb *inp, uint32_t cur_mtu)
1728163953Srrs{
1729163953Srrs	/* select another MTU that is just bigger than this one */
1730163953Srrs	int i;
1731163953Srrs
1732163953Srrs	for (i = 0; i < SCTP_NUMBER_OF_MTU_SIZES; i++) {
1733163953Srrs		if (cur_mtu < mtu_sizes[i]) {
1734163953Srrs			/* no max_mtu is bigger than this one */
1735163953Srrs			return (mtu_sizes[i]);
1736163953Srrs		}
1737163953Srrs	}
1738163953Srrs	/* here return the highest allowable */
1739163953Srrs	return (cur_mtu);
1740163953Srrs}
1741163953Srrs
1742163953Srrs
1743163953Srrsvoid
1744163953Srrssctp_pathmtu_timer(struct sctp_inpcb *inp,
1745163953Srrs    struct sctp_tcb *stcb,
1746163953Srrs    struct sctp_nets *net)
1747163953Srrs{
1748179157Srrs	uint32_t next_mtu, mtu;
1749163953Srrs
1750163953Srrs	next_mtu = sctp_getnext_mtu(inp, net->mtu);
1751169352Srrs
1752179157Srrs	if ((next_mtu > net->mtu) && (net->port == 0)) {
1753169352Srrs		if ((net->src_addr_selected == 0) ||
1754169352Srrs		    (net->ro._s_addr == NULL) ||
1755169352Srrs		    (net->ro._s_addr->localifa_flags & SCTP_BEING_DELETED)) {
1756169420Srrs			if ((net->ro._s_addr != NULL) && (net->ro._s_addr->localifa_flags & SCTP_BEING_DELETED)) {
1757169352Srrs				sctp_free_ifa(net->ro._s_addr);
1758169352Srrs				net->ro._s_addr = NULL;
1759169352Srrs				net->src_addr_selected = 0;
1760169420Srrs			} else if (net->ro._s_addr == NULL) {
1761179157Srrs#if defined(INET6) && defined(SCTP_EMBEDDED_V6_SCOPE)
1762179157Srrs				if (net->ro._l_addr.sa.sa_family == AF_INET6) {
1763179157Srrs					struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
1764179157Srrs
1765179157Srrs					/* KAME hack: embed scopeid */
1766180387Srrs					(void)sa6_embedscope(sin6, MODULE_GLOBAL(MOD_INET6, ip6_use_defzone));
1767179157Srrs				}
1768179157Srrs#endif
1769179157Srrs
1770169420Srrs				net->ro._s_addr = sctp_source_address_selection(inp,
1771169420Srrs				    stcb,
1772169420Srrs				    (sctp_route_t *) & net->ro,
1773169420Srrs				    net, 0, stcb->asoc.vrf_id);
1774179157Srrs#if defined(INET6) && defined(SCTP_EMBEDDED_V6_SCOPE)
1775179157Srrs				if (net->ro._l_addr.sa.sa_family == AF_INET6) {
1776179157Srrs					struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
1777179157Srrs
1778179157Srrs					(void)sa6_recoverscope(sin6);
1779179157Srrs				}
1780179157Srrs#endif				/* INET6 */
1781169352Srrs			}
1782169352Srrs			if (net->ro._s_addr)
1783169352Srrs				net->src_addr_selected = 1;
1784169352Srrs		}
1785169352Srrs		if (net->ro._s_addr) {
1786169352Srrs			mtu = SCTP_GATHER_MTU_FROM_ROUTE(net->ro._s_addr, &net->ro._s_addr.sa, net->ro.ro_rt);
1787169352Srrs			if (mtu > next_mtu) {
1788163953Srrs				net->mtu = next_mtu;
1789163953Srrs			}
1790163953Srrs		}
1791163953Srrs	}
1792163953Srrs	/* restart the timer */
1793163953Srrs	sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net);
1794163953Srrs}
1795163953Srrs
1796163953Srrsvoid
1797163953Srrssctp_autoclose_timer(struct sctp_inpcb *inp,
1798163953Srrs    struct sctp_tcb *stcb,
1799163953Srrs    struct sctp_nets *net)
1800163953Srrs{
1801163953Srrs	struct timeval tn, *tim_touse;
1802163953Srrs	struct sctp_association *asoc;
1803163953Srrs	int ticks_gone_by;
1804163953Srrs
1805169378Srrs	(void)SCTP_GETTIME_TIMEVAL(&tn);
1806163953Srrs	if (stcb->asoc.sctp_autoclose_ticks &&
1807163953Srrs	    sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE)) {
1808163953Srrs		/* Auto close is on */
1809163953Srrs		asoc = &stcb->asoc;
1810163953Srrs		/* pick the time to use */
1811163953Srrs		if (asoc->time_last_rcvd.tv_sec >
1812163953Srrs		    asoc->time_last_sent.tv_sec) {
1813163953Srrs			tim_touse = &asoc->time_last_rcvd;
1814163953Srrs		} else {
1815163953Srrs			tim_touse = &asoc->time_last_sent;
1816163953Srrs		}
1817163953Srrs		/* Now has long enough transpired to autoclose? */
1818163953Srrs		ticks_gone_by = SEC_TO_TICKS(tn.tv_sec - tim_touse->tv_sec);
1819163953Srrs		if ((ticks_gone_by > 0) &&
1820163953Srrs		    (ticks_gone_by >= (int)asoc->sctp_autoclose_ticks)) {
1821163953Srrs			/*
1822163953Srrs			 * autoclose time has hit, call the output routine,
1823163953Srrs			 * which should do nothing just to be SURE we don't
1824163953Srrs			 * have hanging data. We can then safely check the
1825163953Srrs			 * queues and know that we are clear to send
1826163953Srrs			 * shutdown
1827163953Srrs			 */
1828172090Srrs			sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_AUTOCLOSE_TMR, SCTP_SO_NOT_LOCKED);
1829163953Srrs			/* Are we clean? */
1830163953Srrs			if (TAILQ_EMPTY(&asoc->send_queue) &&
1831163953Srrs			    TAILQ_EMPTY(&asoc->sent_queue)) {
1832163953Srrs				/*
1833163953Srrs				 * there is nothing queued to send, so I'm
1834163953Srrs				 * done...
1835163953Srrs				 */
1836166675Srrs				if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) {
1837163953Srrs					/* only send SHUTDOWN 1st time thru */
1838163953Srrs					sctp_send_shutdown(stcb, stcb->asoc.primary_destination);
1839166675Srrs					if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
1840166675Srrs					    (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
1841166675Srrs						SCTP_STAT_DECR_GAUGE32(sctps_currestab);
1842166675Srrs					}
1843171943Srrs					SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
1844172703Srrs					SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
1845163953Srrs					sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
1846163953Srrs					    stcb->sctp_ep, stcb,
1847163953Srrs					    asoc->primary_destination);
1848163953Srrs					sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
1849163953Srrs					    stcb->sctp_ep, stcb,
1850163953Srrs					    asoc->primary_destination);
1851163953Srrs				}
1852163953Srrs			}
1853163953Srrs		} else {
1854163953Srrs			/*
1855163953Srrs			 * No auto close at this time, reset t-o to check
1856163953Srrs			 * later
1857163953Srrs			 */
1858163953Srrs			int tmp;
1859163953Srrs
1860163953Srrs			/* fool the timer startup to use the time left */
1861163953Srrs			tmp = asoc->sctp_autoclose_ticks;
1862163953Srrs			asoc->sctp_autoclose_ticks -= ticks_gone_by;
1863163953Srrs			sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb,
1864163953Srrs			    net);
1865163953Srrs			/* restore the real tick value */
1866163953Srrs			asoc->sctp_autoclose_ticks = tmp;
1867163953Srrs		}
1868163953Srrs	}
1869163953Srrs}
1870163953Srrs
1871163953Srrsvoid
1872163953Srrssctp_iterator_timer(struct sctp_iterator *it)
1873163953Srrs{
1874163953Srrs	int iteration_count = 0;
1875167598Srrs	int inp_skip = 0;
1876163953Srrs
1877163953Srrs	/*
1878163953Srrs	 * only one iterator can run at a time. This is the only way we can
1879163953Srrs	 * cleanly pull ep's from underneath all the running interators when
1880163953Srrs	 * a ep is freed.
1881163953Srrs	 */
1882163953Srrs	SCTP_ITERATOR_LOCK();
1883163953Srrs	if (it->inp == NULL) {
1884163953Srrs		/* iterator is complete */
1885163953Srrsdone_with_iterator:
1886163953Srrs		SCTP_ITERATOR_UNLOCK();
1887163953Srrs		SCTP_INP_INFO_WLOCK();
1888179783Srrs		TAILQ_REMOVE(&SCTP_BASE_INFO(iteratorhead), it, sctp_nxt_itr);
1889163953Srrs		/* stopping the callout is not needed, in theory */
1890163953Srrs		SCTP_INP_INFO_WUNLOCK();
1891169378Srrs		(void)SCTP_OS_TIMER_STOP(&it->tmr.timer);
1892163953Srrs		if (it->function_atend != NULL) {
1893163953Srrs			(*it->function_atend) (it->pointer, it->val);
1894163953Srrs		}
1895170091Srrs		SCTP_FREE(it, SCTP_M_ITER);
1896163953Srrs		return;
1897163953Srrs	}
1898163953Srrsselect_a_new_ep:
1899163953Srrs	SCTP_INP_WLOCK(it->inp);
1900163953Srrs	while (((it->pcb_flags) &&
1901163953Srrs	    ((it->inp->sctp_flags & it->pcb_flags) != it->pcb_flags)) ||
1902163953Srrs	    ((it->pcb_features) &&
1903163953Srrs	    ((it->inp->sctp_features & it->pcb_features) != it->pcb_features))) {
1904163953Srrs		/* endpoint flags or features don't match, so keep looking */
1905163953Srrs		if (it->iterator_flags & SCTP_ITERATOR_DO_SINGLE_INP) {
1906163953Srrs			SCTP_INP_WUNLOCK(it->inp);
1907163953Srrs			goto done_with_iterator;
1908163953Srrs		}
1909163953Srrs		SCTP_INP_WUNLOCK(it->inp);
1910163953Srrs		it->inp = LIST_NEXT(it->inp, sctp_list);
1911163953Srrs		if (it->inp == NULL) {
1912163953Srrs			goto done_with_iterator;
1913163953Srrs		}
1914163953Srrs		SCTP_INP_WLOCK(it->inp);
1915163953Srrs	}
1916163953Srrs	if ((it->inp->inp_starting_point_for_iterator != NULL) &&
1917163953Srrs	    (it->inp->inp_starting_point_for_iterator != it)) {
1918169420Srrs		SCTP_PRINTF("Iterator collision, waiting for one at %p\n",
1919163979Sru		    it->inp);
1920163953Srrs		SCTP_INP_WUNLOCK(it->inp);
1921163953Srrs		goto start_timer_return;
1922163953Srrs	}
1923163953Srrs	/* mark the current iterator on the endpoint */
1924163953Srrs	it->inp->inp_starting_point_for_iterator = it;
1925163953Srrs	SCTP_INP_WUNLOCK(it->inp);
1926163953Srrs	SCTP_INP_RLOCK(it->inp);
1927163953Srrs	/* now go through each assoc which is in the desired state */
1928167598Srrs	if (it->done_current_ep == 0) {
1929167598Srrs		if (it->function_inp != NULL)
1930167598Srrs			inp_skip = (*it->function_inp) (it->inp, it->pointer, it->val);
1931167598Srrs		it->done_current_ep = 1;
1932167598Srrs	}
1933163953Srrs	if (it->stcb == NULL) {
1934163953Srrs		/* run the per instance function */
1935163953Srrs		it->stcb = LIST_FIRST(&it->inp->sctp_asoc_list);
1936163953Srrs	}
1937163953Srrs	SCTP_INP_RUNLOCK(it->inp);
1938167598Srrs	if ((inp_skip) || it->stcb == NULL) {
1939167598Srrs		if (it->function_inp_end != NULL) {
1940167598Srrs			inp_skip = (*it->function_inp_end) (it->inp,
1941167598Srrs			    it->pointer,
1942167598Srrs			    it->val);
1943167598Srrs		}
1944167598Srrs		goto no_stcb;
1945167598Srrs	}
1946163953Srrs	if ((it->stcb) &&
1947163953Srrs	    (it->stcb->asoc.stcb_starting_point_for_iterator == it)) {
1948163953Srrs		it->stcb->asoc.stcb_starting_point_for_iterator = NULL;
1949163953Srrs	}
1950163953Srrs	while (it->stcb) {
1951163953Srrs		SCTP_TCB_LOCK(it->stcb);
1952163953Srrs		if (it->asoc_state && ((it->stcb->asoc.state & it->asoc_state) != it->asoc_state)) {
1953163953Srrs			/* not in the right state... keep looking */
1954163953Srrs			SCTP_TCB_UNLOCK(it->stcb);
1955163953Srrs			goto next_assoc;
1956163953Srrs		}
1957163953Srrs		/* mark the current iterator on the assoc */
1958163953Srrs		it->stcb->asoc.stcb_starting_point_for_iterator = it;
1959163953Srrs		/* see if we have limited out the iterator loop */
1960163953Srrs		iteration_count++;
1961163953Srrs		if (iteration_count > SCTP_ITERATOR_MAX_AT_ONCE) {
1962163953Srrs	start_timer_return:
1963163953Srrs			/* set a timer to continue this later */
1964170943Srrs			if (it->stcb)
1965170943Srrs				SCTP_TCB_UNLOCK(it->stcb);
1966163953Srrs			sctp_timer_start(SCTP_TIMER_TYPE_ITERATOR,
1967163953Srrs			    (struct sctp_inpcb *)it, NULL, NULL);
1968163953Srrs			SCTP_ITERATOR_UNLOCK();
1969163953Srrs			return;
1970163953Srrs		}
1971163953Srrs		/* run function on this one */
1972163953Srrs		(*it->function_assoc) (it->inp, it->stcb, it->pointer, it->val);
1973163953Srrs
1974163953Srrs		/*
1975163953Srrs		 * we lie here, it really needs to have its own type but
1976163953Srrs		 * first I must verify that this won't effect things :-0
1977163953Srrs		 */
1978163953Srrs		if (it->no_chunk_output == 0)
1979172090Srrs			sctp_chunk_output(it->inp, it->stcb, SCTP_OUTPUT_FROM_T3, SCTP_SO_NOT_LOCKED);
1980163953Srrs
1981163953Srrs		SCTP_TCB_UNLOCK(it->stcb);
1982163953Srrsnext_assoc:
1983163953Srrs		it->stcb = LIST_NEXT(it->stcb, sctp_tcblist);
1984167598Srrs		if (it->stcb == NULL) {
1985167598Srrs			if (it->function_inp_end != NULL) {
1986167598Srrs				inp_skip = (*it->function_inp_end) (it->inp,
1987167598Srrs				    it->pointer,
1988167598Srrs				    it->val);
1989167598Srrs			}
1990167598Srrs		}
1991163953Srrs	}
1992167598Srrsno_stcb:
1993163953Srrs	/* done with all assocs on this endpoint, move on to next endpoint */
1994167598Srrs	it->done_current_ep = 0;
1995163953Srrs	SCTP_INP_WLOCK(it->inp);
1996163953Srrs	it->inp->inp_starting_point_for_iterator = NULL;
1997163953Srrs	SCTP_INP_WUNLOCK(it->inp);
1998163953Srrs	if (it->iterator_flags & SCTP_ITERATOR_DO_SINGLE_INP) {
1999163953Srrs		it->inp = NULL;
2000163953Srrs	} else {
2001163953Srrs		SCTP_INP_INFO_RLOCK();
2002163953Srrs		it->inp = LIST_NEXT(it->inp, sctp_list);
2003163953Srrs		SCTP_INP_INFO_RUNLOCK();
2004163953Srrs	}
2005163953Srrs	if (it->inp == NULL) {
2006163953Srrs		goto done_with_iterator;
2007163953Srrs	}
2008163953Srrs	goto select_a_new_ep;
2009163953Srrs}
2010