sctp_timer.c revision 212712
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 212712 2010-09-15 23:10:45Z tuexen $");
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>
52185694Srrs#include <netinet/udp.h>
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? */
218211944Stuexen				if ((stcb->asoc.sctp_cmt_on_off == 1) &&
219211944Stuexen				    (stcb->asoc.sctp_cmt_pf > 0)) {
220171440Srrs					net->dest_state &= ~SCTP_ADDR_PF;
221171440Srrs					SCTPDBG(SCTP_DEBUG_TIMER4, "Destination %p moved from PF to unreachable.\n",
222171440Srrs					    net);
223171440Srrs				}
224163953Srrs				sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN,
225163953Srrs				    stcb,
226163953Srrs				    SCTP_FAILED_THRESHOLD,
227172090Srrs				    (void *)net, SCTP_SO_NOT_LOCKED);
228163953Srrs			}
229163953Srrs		}
230163953Srrs		/*********HOLD THIS COMMENT FOR PATCH OF ALTERNATE
231163953Srrs		 *********ROUTING CODE
232163953Srrs		 */
233163953Srrs		/*********HOLD THIS COMMENT FOR END OF PATCH OF ALTERNATE
234163953Srrs		 *********ROUTING CODE
235163953Srrs		 */
236163953Srrs	}
237163953Srrs	if (stcb == NULL)
238163953Srrs		return (0);
239163953Srrs
240163953Srrs	if (net) {
241163953Srrs		if ((net->dest_state & SCTP_ADDR_UNCONFIRMED) == 0) {
242179783Srrs			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
243171943Srrs				sctp_misc_ints(SCTP_THRESHOLD_INCR,
244171943Srrs				    stcb->asoc.overall_error_count,
245171943Srrs				    (stcb->asoc.overall_error_count + 1),
246171943Srrs				    SCTP_FROM_SCTP_TIMER,
247171943Srrs				    __LINE__);
248171943Srrs			}
249163953Srrs			stcb->asoc.overall_error_count++;
250163953Srrs		}
251163953Srrs	} else {
252179783Srrs		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
253171943Srrs			sctp_misc_ints(SCTP_THRESHOLD_INCR,
254171943Srrs			    stcb->asoc.overall_error_count,
255171943Srrs			    (stcb->asoc.overall_error_count + 1),
256171943Srrs			    SCTP_FROM_SCTP_TIMER,
257171943Srrs			    __LINE__);
258171943Srrs		}
259163953Srrs		stcb->asoc.overall_error_count++;
260163953Srrs	}
261169420Srrs	SCTPDBG(SCTP_DEBUG_TIMER4, "Overall error count for %p now %d thresh:%u state:%x\n",
262169420Srrs	    &stcb->asoc, stcb->asoc.overall_error_count,
263169420Srrs	    (uint32_t) threshold,
264169420Srrs	    ((net == NULL) ? (uint32_t) 0 : (uint32_t) net->dest_state));
265163953Srrs	/*
266163953Srrs	 * We specifically do not do >= to give the assoc one more change
267163953Srrs	 * before we fail it.
268163953Srrs	 */
269163953Srrs	if (stcb->asoc.overall_error_count > threshold) {
270163953Srrs		/* Abort notification sends a ULP notify */
271163953Srrs		struct mbuf *oper;
272163953Srrs
273163953Srrs		oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
274163953Srrs		    0, M_DONTWAIT, 1, MT_DATA);
275163953Srrs		if (oper) {
276163953Srrs			struct sctp_paramhdr *ph;
277163953Srrs			uint32_t *ippp;
278163953Srrs
279165647Srrs			SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
280163953Srrs			    sizeof(uint32_t);
281163953Srrs			ph = mtod(oper, struct sctp_paramhdr *);
282163953Srrs			ph->param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
283165647Srrs			ph->param_length = htons(SCTP_BUF_LEN(oper));
284163953Srrs			ippp = (uint32_t *) (ph + 1);
285165220Srrs			*ippp = htonl(SCTP_FROM_SCTP_TIMER + SCTP_LOC_1);
286163953Srrs		}
287165220Srrs		inp->last_abort_code = SCTP_FROM_SCTP_TIMER + SCTP_LOC_1;
288172090Srrs		sctp_abort_an_association(inp, stcb, SCTP_FAILED_THRESHOLD, oper, SCTP_SO_NOT_LOCKED);
289163953Srrs		return (1);
290163953Srrs	}
291163953Srrs	return (0);
292163953Srrs}
293163953Srrs
294163953Srrsstruct sctp_nets *
295163953Srrssctp_find_alternate_net(struct sctp_tcb *stcb,
296163953Srrs    struct sctp_nets *net,
297171440Srrs    int mode)
298163953Srrs{
299163953Srrs	/* Find and return an alternate network if possible */
300171440Srrs	struct sctp_nets *alt, *mnet, *min_errors_net = NULL, *max_cwnd_net = NULL;
301163953Srrs	int once;
302163953Srrs
303171440Srrs	/* JRS 5/14/07 - Initialize min_errors to an impossible value. */
304171440Srrs	int min_errors = -1;
305171440Srrs	uint32_t max_cwnd = 0;
306171440Srrs
307163953Srrs	if (stcb->asoc.numnets == 1) {
308163953Srrs		/* No others but net */
309163953Srrs		return (TAILQ_FIRST(&stcb->asoc.nets));
310163953Srrs	}
311171440Srrs	/*
312171440Srrs	 * JRS 5/14/07 - If mode is set to 2, use the CMT PF find alternate
313171440Srrs	 * net algorithm. This algorithm chooses the active destination (not
314171440Srrs	 * in PF state) with the largest cwnd value. If all destinations are
315171440Srrs	 * in PF state, unreachable, or unconfirmed, choose the desination
316171440Srrs	 * that is in PF state with the lowest error count. In case of a
317171440Srrs	 * tie, choose the destination that was most recently active.
318171440Srrs	 */
319171440Srrs	if (mode == 2) {
320163953Srrs		TAILQ_FOREACH(mnet, &stcb->asoc.nets, sctp_next) {
321171440Srrs			/*
322171440Srrs			 * JRS 5/14/07 - If the destination is unreachable
323171440Srrs			 * or unconfirmed, skip it.
324171440Srrs			 */
325163953Srrs			if (((mnet->dest_state & SCTP_ADDR_REACHABLE) != SCTP_ADDR_REACHABLE) ||
326171440Srrs			    (mnet->dest_state & SCTP_ADDR_UNCONFIRMED)) {
327171440Srrs				continue;
328171440Srrs			}
329171440Srrs			/*
330171440Srrs			 * JRS 5/14/07 -  If the destination is reachable
331171440Srrs			 * but in PF state, compare the error count of the
332171440Srrs			 * destination to the minimum error count seen thus
333171440Srrs			 * far. Store the destination with the lower error
334171440Srrs			 * count.  If the error counts are equal, store the
335171440Srrs			 * destination that was most recently active.
336171440Srrs			 */
337171440Srrs			if (mnet->dest_state & SCTP_ADDR_PF) {
338171440Srrs				/*
339171440Srrs				 * JRS 5/14/07 - If the destination under
340171440Srrs				 * consideration is the current destination,
341171440Srrs				 * work as if the error count is one higher.
342171440Srrs				 * The actual error count will not be
343171440Srrs				 * incremented until later in the t3
344171440Srrs				 * handler.
345171440Srrs				 */
346171440Srrs				if (mnet == net) {
347171440Srrs					if (min_errors == -1) {
348171440Srrs						min_errors = mnet->error_count + 1;
349171440Srrs						min_errors_net = mnet;
350171440Srrs					} else if (mnet->error_count + 1 < min_errors) {
351171440Srrs						min_errors = mnet->error_count + 1;
352171440Srrs						min_errors_net = mnet;
353171440Srrs					} else if (mnet->error_count + 1 == min_errors
354171440Srrs					    && mnet->last_active > min_errors_net->last_active) {
355171440Srrs						min_errors_net = mnet;
356171440Srrs						min_errors = mnet->error_count + 1;
357171440Srrs					}
358171440Srrs					continue;
359171440Srrs				} else {
360171440Srrs					if (min_errors == -1) {
361171440Srrs						min_errors = mnet->error_count;
362171440Srrs						min_errors_net = mnet;
363171440Srrs					} else if (mnet->error_count < min_errors) {
364171440Srrs						min_errors = mnet->error_count;
365171440Srrs						min_errors_net = mnet;
366171440Srrs					} else if (mnet->error_count == min_errors
367171440Srrs					    && mnet->last_active > min_errors_net->last_active) {
368171440Srrs						min_errors_net = mnet;
369171440Srrs						min_errors = mnet->error_count;
370171440Srrs					}
371171440Srrs					continue;
372171440Srrs				}
373171440Srrs			}
374171440Srrs			/*
375171440Srrs			 * JRS 5/14/07 - If the destination is reachable and
376171440Srrs			 * not in PF state, compare the cwnd of the
377171440Srrs			 * destination to the highest cwnd seen thus far.
378171440Srrs			 * Store the destination with the higher cwnd value.
379171440Srrs			 * If the cwnd values are equal, randomly choose one
380171440Srrs			 * of the two destinations.
381171440Srrs			 */
382171440Srrs			if (max_cwnd < mnet->cwnd) {
383171440Srrs				max_cwnd_net = mnet;
384171440Srrs				max_cwnd = mnet->cwnd;
385171440Srrs			} else if (max_cwnd == mnet->cwnd) {
386171440Srrs				uint32_t rndval;
387171440Srrs				uint8_t this_random;
388171440Srrs
389171440Srrs				if (stcb->asoc.hb_random_idx > 3) {
390171440Srrs					rndval = sctp_select_initial_TSN(&stcb->sctp_ep->sctp_ep);
391171440Srrs					memcpy(stcb->asoc.hb_random_values, &rndval, sizeof(stcb->asoc.hb_random_values));
392171440Srrs					this_random = stcb->asoc.hb_random_values[0];
393171440Srrs					stcb->asoc.hb_random_idx++;
394171440Srrs					stcb->asoc.hb_ect_randombit = 0;
395171440Srrs				} else {
396171440Srrs					this_random = stcb->asoc.hb_random_values[stcb->asoc.hb_random_idx];
397171440Srrs					stcb->asoc.hb_random_idx++;
398171440Srrs					stcb->asoc.hb_ect_randombit = 0;
399171440Srrs				}
400171440Srrs				if (this_random % 2 == 1) {
401171440Srrs					max_cwnd_net = mnet;
402180387Srrs					max_cwnd = mnet->cwnd;	/* Useless? */
403171440Srrs				}
404171440Srrs			}
405171440Srrs		}
406171440Srrs		/*
407171440Srrs		 * JRS 5/14/07 - After all destination have been considered
408171440Srrs		 * as alternates, check to see if there was some active
409171440Srrs		 * destination (not in PF state).  If not, check to see if
410171440Srrs		 * there was some PF destination with the minimum number of
411171440Srrs		 * errors.  If not, return the original destination.  If
412171440Srrs		 * there is a min_errors_net, remove the PF flag from that
413171440Srrs		 * destination, set the cwnd to one or two MTUs, and return
414171440Srrs		 * the destination as an alt. If there was some active
415171440Srrs		 * destination with a highest cwnd, return the destination
416171440Srrs		 * as an alt.
417171440Srrs		 */
418171440Srrs		if (max_cwnd_net == NULL) {
419171440Srrs			if (min_errors_net == NULL) {
420171440Srrs				return (net);
421171440Srrs			}
422171440Srrs			min_errors_net->dest_state &= ~SCTP_ADDR_PF;
423211944Stuexen			min_errors_net->cwnd = min_errors_net->mtu * stcb->asoc.sctp_cmt_pf;
424171440Srrs			if (SCTP_OS_TIMER_PENDING(&min_errors_net->rxt_timer.timer)) {
425171440Srrs				sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
426171440Srrs				    stcb, min_errors_net,
427171440Srrs				    SCTP_FROM_SCTP_TIMER + SCTP_LOC_2);
428171440Srrs			}
429171440Srrs			SCTPDBG(SCTP_DEBUG_TIMER4, "Destination %p moved from PF to active with %d errors.\n",
430171440Srrs			    min_errors_net, min_errors_net->error_count);
431171440Srrs			return (min_errors_net);
432171440Srrs		} else {
433171440Srrs			return (max_cwnd_net);
434171440Srrs		}
435171440Srrs	}
436171440Srrs	/*
437171440Srrs	 * JRS 5/14/07 - If mode is set to 1, use the CMT policy for
438171440Srrs	 * choosing an alternate net.
439171440Srrs	 */
440171440Srrs	else if (mode == 1) {
441171440Srrs		TAILQ_FOREACH(mnet, &stcb->asoc.nets, sctp_next) {
442171440Srrs			if (((mnet->dest_state & SCTP_ADDR_REACHABLE) != SCTP_ADDR_REACHABLE) ||
443163953Srrs			    (mnet->dest_state & SCTP_ADDR_UNCONFIRMED)
444163953Srrs			    ) {
445163953Srrs				/*
446163953Srrs				 * will skip ones that are not-reachable or
447163953Srrs				 * unconfirmed
448163953Srrs				 */
449163953Srrs				continue;
450163953Srrs			}
451171440Srrs			if (max_cwnd < mnet->cwnd) {
452171440Srrs				max_cwnd_net = mnet;
453171440Srrs				max_cwnd = mnet->cwnd;
454171440Srrs			} else if (max_cwnd == mnet->cwnd) {
455163953Srrs				uint32_t rndval;
456163953Srrs				uint8_t this_random;
457163953Srrs
458163953Srrs				if (stcb->asoc.hb_random_idx > 3) {
459163953Srrs					rndval = sctp_select_initial_TSN(&stcb->sctp_ep->sctp_ep);
460163953Srrs					memcpy(stcb->asoc.hb_random_values, &rndval,
461163953Srrs					    sizeof(stcb->asoc.hb_random_values));
462163953Srrs					this_random = stcb->asoc.hb_random_values[0];
463163953Srrs					stcb->asoc.hb_random_idx = 0;
464163953Srrs					stcb->asoc.hb_ect_randombit = 0;
465163953Srrs				} else {
466163953Srrs					this_random = stcb->asoc.hb_random_values[stcb->asoc.hb_random_idx];
467163953Srrs					stcb->asoc.hb_random_idx++;
468163953Srrs					stcb->asoc.hb_ect_randombit = 0;
469163953Srrs				}
470163953Srrs				if (this_random % 2) {
471171440Srrs					max_cwnd_net = mnet;
472171440Srrs					max_cwnd = mnet->cwnd;
473163953Srrs				}
474163953Srrs			}
475163953Srrs		}
476171440Srrs		if (max_cwnd_net) {
477171440Srrs			return (max_cwnd_net);
478163953Srrs		}
479163953Srrs	}
480163953Srrs	mnet = net;
481163953Srrs	once = 0;
482163953Srrs
483163953Srrs	if (mnet == NULL) {
484163953Srrs		mnet = TAILQ_FIRST(&stcb->asoc.nets);
485212225Srrs		if (mnet == NULL) {
486212225Srrs			return (NULL);
487212225Srrs		}
488163953Srrs	}
489163953Srrs	do {
490163953Srrs		alt = TAILQ_NEXT(mnet, sctp_next);
491163953Srrs		if (alt == NULL) {
492163953Srrs			once++;
493163953Srrs			if (once > 1) {
494163953Srrs				break;
495163953Srrs			}
496163953Srrs			alt = TAILQ_FIRST(&stcb->asoc.nets);
497212225Srrs			if (alt == NULL) {
498212225Srrs				return (NULL);
499212225Srrs			}
500163953Srrs		}
501163953Srrs		if (alt->ro.ro_rt == NULL) {
502167598Srrs			if (alt->ro._s_addr) {
503167598Srrs				sctp_free_ifa(alt->ro._s_addr);
504167598Srrs				alt->ro._s_addr = NULL;
505167598Srrs			}
506163953Srrs			alt->src_addr_selected = 0;
507163953Srrs		}
508163953Srrs		if (
509163953Srrs		    ((alt->dest_state & SCTP_ADDR_REACHABLE) == SCTP_ADDR_REACHABLE) &&
510163953Srrs		    (alt->ro.ro_rt != NULL) &&
511169655Srrs		/* sa_ignore NO_NULL_CHK */
512163953Srrs		    (!(alt->dest_state & SCTP_ADDR_UNCONFIRMED))
513163953Srrs		    ) {
514163953Srrs			/* Found a reachable address */
515163953Srrs			break;
516163953Srrs		}
517163953Srrs		mnet = alt;
518163953Srrs	} while (alt != NULL);
519163953Srrs
520163953Srrs	if (alt == NULL) {
521163953Srrs		/* Case where NO insv network exists (dormant state) */
522163953Srrs		/* we rotate destinations */
523163953Srrs		once = 0;
524163953Srrs		mnet = net;
525163953Srrs		do {
526212225Srrs			if (mnet == NULL) {
527212225Srrs				return (TAILQ_FIRST(&stcb->asoc.nets));
528212225Srrs			}
529163953Srrs			alt = TAILQ_NEXT(mnet, sctp_next);
530163953Srrs			if (alt == NULL) {
531163953Srrs				once++;
532163953Srrs				if (once > 1) {
533163953Srrs					break;
534163953Srrs				}
535163953Srrs				alt = TAILQ_FIRST(&stcb->asoc.nets);
536163953Srrs			}
537169655Srrs			/* sa_ignore NO_NULL_CHK */
538163953Srrs			if ((!(alt->dest_state & SCTP_ADDR_UNCONFIRMED)) &&
539163953Srrs			    (alt != net)) {
540163953Srrs				/* Found an alternate address */
541163953Srrs				break;
542163953Srrs			}
543163953Srrs			mnet = alt;
544163953Srrs		} while (alt != NULL);
545163953Srrs	}
546163953Srrs	if (alt == NULL) {
547163953Srrs		return (net);
548163953Srrs	}
549163953Srrs	return (alt);
550163953Srrs}
551163953Srrs
552171440Srrs
553171440Srrs
554163953Srrsstatic void
555163953Srrssctp_backoff_on_timeout(struct sctp_tcb *stcb,
556163953Srrs    struct sctp_nets *net,
557163953Srrs    int win_probe,
558210599Srrs    int num_marked, int num_abandoned)
559163953Srrs{
560170642Srrs	if (net->RTO == 0) {
561170642Srrs		net->RTO = stcb->asoc.minrto;
562170642Srrs	}
563163953Srrs	net->RTO <<= 1;
564163953Srrs	if (net->RTO > stcb->asoc.maxrto) {
565163953Srrs		net->RTO = stcb->asoc.maxrto;
566163953Srrs	}
567210599Srrs	if ((win_probe == 0) && (num_marked || num_abandoned)) {
568163953Srrs		/* We don't apply penalty to window probe scenarios */
569171440Srrs		/* JRS - Use the congestion control given in the CC module */
570171440Srrs		stcb->asoc.cc_functions.sctp_cwnd_update_after_timeout(stcb, net);
571163953Srrs	}
572163953Srrs}
573163953Srrs
574184333Srrs#ifndef INVARIANTS
575184333Srrsstatic void
576182367Srrssctp_recover_sent_list(struct sctp_tcb *stcb)
577182367Srrs{
578182367Srrs	struct sctp_tmit_chunk *chk, *tp2;
579182367Srrs	struct sctp_association *asoc;
580182367Srrs
581182367Srrs	asoc = &stcb->asoc;
582182367Srrs	chk = TAILQ_FIRST(&stcb->asoc.sent_queue);
583182367Srrs	for (; chk != NULL; chk = tp2) {
584182367Srrs		tp2 = TAILQ_NEXT(chk, sctp_next);
585182367Srrs		if ((compare_with_wrap(stcb->asoc.last_acked_seq,
586182367Srrs		    chk->rec.data.TSN_seq,
587182367Srrs		    MAX_TSN)) ||
588182367Srrs		    (stcb->asoc.last_acked_seq == chk->rec.data.TSN_seq)) {
589182367Srrs
590182367Srrs			SCTP_PRINTF("Found chk:%p tsn:%x <= last_acked_seq:%x\n",
591182367Srrs			    chk, chk->rec.data.TSN_seq, stcb->asoc.last_acked_seq);
592182367Srrs			TAILQ_REMOVE(&asoc->sent_queue, chk, sctp_next);
593182367Srrs			if (chk->pr_sctp_on) {
594182367Srrs				if (asoc->pr_sctp_cnt != 0)
595182367Srrs					asoc->pr_sctp_cnt--;
596182367Srrs			}
597182367Srrs			if (chk->data) {
598182367Srrs				/* sa_ignore NO_NULL_CHK */
599182367Srrs				sctp_free_bufspace(stcb, asoc, chk, 1);
600182367Srrs				sctp_m_freem(chk->data);
601196260Stuexen				if (asoc->peer_supports_prsctp && PR_SCTP_BUF_ENABLED(chk->flags)) {
602182367Srrs					asoc->sent_queue_cnt_removeable--;
603182367Srrs				}
604182367Srrs			}
605182367Srrs			chk->data = NULL;
606182367Srrs			asoc->sent_queue_cnt--;
607182367Srrs			sctp_free_a_chunk(stcb, chk);
608182367Srrs		}
609182367Srrs	}
610182367Srrs	SCTP_PRINTF("after recover order is as follows\n");
611182367Srrs	chk = TAILQ_FIRST(&stcb->asoc.sent_queue);
612182367Srrs	for (; chk != NULL; chk = tp2) {
613182367Srrs		tp2 = TAILQ_NEXT(chk, sctp_next);
614182367Srrs		SCTP_PRINTF("chk:%p TSN:%x\n", chk, chk->rec.data.TSN_seq);
615182367Srrs	}
616182367Srrs}
617182367Srrs
618184333Srrs#endif
619184333Srrs
620163953Srrsstatic int
621163953Srrssctp_mark_all_for_resend(struct sctp_tcb *stcb,
622163953Srrs    struct sctp_nets *net,
623163953Srrs    struct sctp_nets *alt,
624163953Srrs    int window_probe,
625210599Srrs    int *num_marked,
626210599Srrs    int *num_abandoned)
627163953Srrs{
628163953Srrs
629163953Srrs	/*
630163953Srrs	 * Mark all chunks (well not all) that were sent to *net for
631163953Srrs	 * retransmission. Move them to alt for there destination as well...
632163953Srrs	 * We only mark chunks that have been outstanding long enough to
633163953Srrs	 * have received feed-back.
634163953Srrs	 */
635210599Srrs	struct sctp_tmit_chunk *chk, *tp2;
636163953Srrs	struct sctp_nets *lnets;
637163953Srrs	struct timeval now, min_wait, tv;
638163953Srrs	int cur_rtt;
639210599Srrs	int cnt_abandoned;
640168709Srrs	int audit_tf, num_mk, fir;
641163953Srrs	unsigned int cnt_mk;
642168709Srrs	uint32_t orig_flight, orig_tf;
643163953Srrs	uint32_t tsnlast, tsnfirst;
644182367Srrs	int recovery_cnt = 0;
645163953Srrs
646171440Srrs
647163953Srrs	/* none in flight now */
648163953Srrs	audit_tf = 0;
649163953Srrs	fir = 0;
650163953Srrs	/*
651163953Srrs	 * figure out how long a data chunk must be pending before we can
652163953Srrs	 * mark it ..
653163953Srrs	 */
654169378Srrs	(void)SCTP_GETTIME_TIMEVAL(&now);
655163953Srrs	/* get cur rto in micro-seconds */
656163953Srrs	cur_rtt = (((net->lastsa >> 2) + net->lastsv) >> 1);
657163953Srrs	cur_rtt *= 1000;
658179783Srrs	if (SCTP_BASE_SYSCTL(sctp_logging_level) & (SCTP_EARLYFR_LOGGING_ENABLE | SCTP_FR_LOGGING_ENABLE)) {
659170744Srrs		sctp_log_fr(cur_rtt,
660170744Srrs		    stcb->asoc.peers_rwnd,
661170744Srrs		    window_probe,
662170744Srrs		    SCTP_FR_T3_MARK_TIME);
663170744Srrs		sctp_log_fr(net->flight_size,
664170744Srrs		    SCTP_OS_TIMER_PENDING(&net->fr_timer.timer),
665170744Srrs		    SCTP_OS_TIMER_ACTIVE(&net->fr_timer.timer),
666170744Srrs		    SCTP_FR_CWND_REPORT);
667170744Srrs		sctp_log_fr(net->flight_size, net->cwnd, stcb->asoc.total_flight, SCTP_FR_CWND_REPORT);
668170744Srrs	}
669163953Srrs	tv.tv_sec = cur_rtt / 1000000;
670163953Srrs	tv.tv_usec = cur_rtt % 1000000;
671163953Srrs	min_wait = now;
672163953Srrs	timevalsub(&min_wait, &tv);
673163953Srrs	if (min_wait.tv_sec < 0 || min_wait.tv_usec < 0) {
674163953Srrs		/*
675163953Srrs		 * if we hit here, we don't have enough seconds on the clock
676163953Srrs		 * to account for the RTO. We just let the lower seconds be
677163953Srrs		 * the bounds and don't worry about it. This may mean we
678163953Srrs		 * will mark a lot more than we should.
679163953Srrs		 */
680163953Srrs		min_wait.tv_sec = min_wait.tv_usec = 0;
681163953Srrs	}
682179783Srrs	if (SCTP_BASE_SYSCTL(sctp_logging_level) & (SCTP_EARLYFR_LOGGING_ENABLE | SCTP_FR_LOGGING_ENABLE)) {
683170744Srrs		sctp_log_fr(cur_rtt, now.tv_sec, now.tv_usec, SCTP_FR_T3_MARK_TIME);
684170744Srrs		sctp_log_fr(0, min_wait.tv_sec, min_wait.tv_usec, SCTP_FR_T3_MARK_TIME);
685170744Srrs	}
686163953Srrs	/*
687163953Srrs	 * Our rwnd will be incorrect here since we are not adding back the
688163953Srrs	 * cnt * mbuf but we will fix that down below.
689163953Srrs	 */
690163953Srrs	orig_flight = net->flight_size;
691168709Srrs	orig_tf = stcb->asoc.total_flight;
692168709Srrs
693163953Srrs	net->fast_retran_ip = 0;
694163953Srrs	/* Now on to each chunk */
695210599Srrs	cnt_abandoned = 0;
696163953Srrs	num_mk = cnt_mk = 0;
697163953Srrs	tsnfirst = tsnlast = 0;
698184333Srrs#ifndef INVARIANTS
699182367Srrsstart_again:
700184333Srrs#endif
701163953Srrs	chk = TAILQ_FIRST(&stcb->asoc.sent_queue);
702163953Srrs	for (; chk != NULL; chk = tp2) {
703163953Srrs		tp2 = TAILQ_NEXT(chk, sctp_next);
704163953Srrs		if ((compare_with_wrap(stcb->asoc.last_acked_seq,
705163953Srrs		    chk->rec.data.TSN_seq,
706163953Srrs		    MAX_TSN)) ||
707163953Srrs		    (stcb->asoc.last_acked_seq == chk->rec.data.TSN_seq)) {
708163953Srrs			/* Strange case our list got out of order? */
709182367Srrs			SCTP_PRINTF("Our list is out of order? last_acked:%x chk:%x",
710182367Srrs			    (unsigned int)stcb->asoc.last_acked_seq, (unsigned int)chk->rec.data.TSN_seq);
711182367Srrs			recovery_cnt++;
712182367Srrs#ifdef INVARIANTS
713182367Srrs			panic("last acked >= chk on sent-Q");
714182367Srrs#else
715182367Srrs			SCTP_PRINTF("Recover attempts a restart cnt:%d\n", recovery_cnt);
716182367Srrs			sctp_recover_sent_list(stcb);
717182367Srrs			if (recovery_cnt < 10) {
718182367Srrs				goto start_again;
719182367Srrs			} else {
720182367Srrs				SCTP_PRINTF("Recovery fails %d times??\n", recovery_cnt);
721182367Srrs			}
722182367Srrs#endif
723163953Srrs		}
724163953Srrs		if ((chk->whoTo == net) && (chk->sent < SCTP_DATAGRAM_ACKED)) {
725163953Srrs			/*
726163953Srrs			 * found one to mark: If it is less than
727163953Srrs			 * DATAGRAM_ACKED it MUST not be a skipped or marked
728163953Srrs			 * TSN but instead one that is either already set
729163953Srrs			 * for retransmission OR one that needs
730163953Srrs			 * retransmission.
731163953Srrs			 */
732163953Srrs
733163953Srrs			/* validate its been outstanding long enough */
734179783Srrs			if (SCTP_BASE_SYSCTL(sctp_logging_level) & (SCTP_EARLYFR_LOGGING_ENABLE | SCTP_FR_LOGGING_ENABLE)) {
735170744Srrs				sctp_log_fr(chk->rec.data.TSN_seq,
736170744Srrs				    chk->sent_rcv_time.tv_sec,
737170744Srrs				    chk->sent_rcv_time.tv_usec,
738170744Srrs				    SCTP_FR_T3_MARK_TIME);
739170744Srrs			}
740163953Srrs			if ((chk->sent_rcv_time.tv_sec > min_wait.tv_sec) && (window_probe == 0)) {
741163953Srrs				/*
742163953Srrs				 * we have reached a chunk that was sent
743163953Srrs				 * some seconds past our min.. forget it we
744163953Srrs				 * will find no more to send.
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			} else if ((chk->sent_rcv_time.tv_sec == min_wait.tv_sec) &&
754163953Srrs			    (window_probe == 0)) {
755163953Srrs				/*
756163953Srrs				 * we must look at the micro seconds to
757163953Srrs				 * know.
758163953Srrs				 */
759163953Srrs				if (chk->sent_rcv_time.tv_usec >= min_wait.tv_usec) {
760163953Srrs					/*
761163953Srrs					 * ok it was sent after our boundary
762163953Srrs					 * time.
763163953Srrs					 */
764179783Srrs					if (SCTP_BASE_SYSCTL(sctp_logging_level) & (SCTP_EARLYFR_LOGGING_ENABLE | SCTP_FR_LOGGING_ENABLE)) {
765170744Srrs						sctp_log_fr(0,
766170744Srrs						    chk->sent_rcv_time.tv_sec,
767170744Srrs						    chk->sent_rcv_time.tv_usec,
768170744Srrs						    SCTP_FR_T3_STOPPED);
769170744Srrs					}
770163953Srrs					continue;
771163953Srrs				}
772163953Srrs			}
773196260Stuexen			if (stcb->asoc.peer_supports_prsctp && PR_SCTP_TTL_ENABLED(chk->flags)) {
774163953Srrs				/* Is it expired? */
775163953Srrs				if ((now.tv_sec > chk->rec.data.timetodrop.tv_sec) ||
776163953Srrs				    ((chk->rec.data.timetodrop.tv_sec == now.tv_sec) &&
777163953Srrs				    (now.tv_usec > chk->rec.data.timetodrop.tv_usec))) {
778163953Srrs					/* Yes so drop it */
779163953Srrs					if (chk->data) {
780169420Srrs						(void)sctp_release_pr_sctp_chunk(stcb,
781163953Srrs						    chk,
782163953Srrs						    (SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_SENT),
783189790Srrs						    SCTP_SO_NOT_LOCKED);
784210599Srrs						cnt_abandoned++;
785163953Srrs					}
786185694Srrs					continue;
787163953Srrs				}
788163953Srrs			}
789196260Stuexen			if (stcb->asoc.peer_supports_prsctp && PR_SCTP_RTX_ENABLED(chk->flags)) {
790163953Srrs				/* Has it been retransmitted tv_sec times? */
791163953Srrs				if (chk->snd_count > chk->rec.data.timetodrop.tv_sec) {
792163953Srrs					if (chk->data) {
793169420Srrs						(void)sctp_release_pr_sctp_chunk(stcb,
794163953Srrs						    chk,
795163953Srrs						    (SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_SENT),
796189790Srrs						    SCTP_SO_NOT_LOCKED);
797210599Srrs						cnt_abandoned++;
798163953Srrs					}
799185694Srrs					continue;
800163953Srrs				}
801163953Srrs			}
802168709Srrs			if (chk->sent < SCTP_DATAGRAM_RESEND) {
803163953Srrs				sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
804163953Srrs				num_mk++;
805163953Srrs				if (fir == 0) {
806163953Srrs					fir = 1;
807163953Srrs					tsnfirst = chk->rec.data.TSN_seq;
808163953Srrs				}
809163953Srrs				tsnlast = chk->rec.data.TSN_seq;
810179783Srrs				if (SCTP_BASE_SYSCTL(sctp_logging_level) & (SCTP_EARLYFR_LOGGING_ENABLE | SCTP_FR_LOGGING_ENABLE)) {
811170744Srrs					sctp_log_fr(chk->rec.data.TSN_seq, chk->snd_count,
812170744Srrs					    0, SCTP_FR_T3_MARKED);
813170744Srrs				}
814168709Srrs				if (chk->rec.data.chunk_was_revoked) {
815168709Srrs					/* deflate the cwnd */
816168709Srrs					chk->whoTo->cwnd -= chk->book_size;
817168709Srrs					chk->rec.data.chunk_was_revoked = 0;
818168709Srrs				}
819168709Srrs				net->marked_retrans++;
820168709Srrs				stcb->asoc.marked_retrans++;
821179783Srrs				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
822170744Srrs					sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_RSND_TO,
823170744Srrs					    chk->whoTo->flight_size,
824170744Srrs					    chk->book_size,
825170744Srrs					    (uintptr_t) chk->whoTo,
826170744Srrs					    chk->rec.data.TSN_seq);
827170744Srrs				}
828168709Srrs				sctp_flight_size_decrease(chk);
829168709Srrs				sctp_total_flight_decrease(stcb, chk);
830168709Srrs				stcb->asoc.peers_rwnd += chk->send_size;
831179783Srrs				stcb->asoc.peers_rwnd += SCTP_BASE_SYSCTL(sctp_peer_chunk_oh);
832163953Srrs			}
833163953Srrs			chk->sent = SCTP_DATAGRAM_RESEND;
834163953Srrs			SCTP_STAT_INCR(sctps_markedretrans);
835165220Srrs
836163953Srrs			/* reset the TSN for striking and other FR stuff */
837163953Srrs			chk->rec.data.doing_fast_retransmit = 0;
838163953Srrs			/* Clear any time so NO RTT is being done */
839163953Srrs			chk->do_rtt = 0;
840163953Srrs			if (alt != net) {
841163953Srrs				sctp_free_remote_addr(chk->whoTo);
842163953Srrs				chk->no_fr_allowed = 1;
843163953Srrs				chk->whoTo = alt;
844163953Srrs				atomic_add_int(&alt->ref_count, 1);
845163953Srrs			} else {
846163953Srrs				chk->no_fr_allowed = 0;
847163953Srrs				if (TAILQ_EMPTY(&stcb->asoc.send_queue)) {
848163953Srrs					chk->rec.data.fast_retran_tsn = stcb->asoc.sending_seq;
849163953Srrs				} else {
850163953Srrs					chk->rec.data.fast_retran_tsn = (TAILQ_FIRST(&stcb->asoc.send_queue))->rec.data.TSN_seq;
851163953Srrs				}
852163953Srrs			}
853170181Srrs			/*
854170181Srrs			 * CMT: Do not allow FRs on retransmitted TSNs.
855170181Srrs			 */
856211944Stuexen			if (stcb->asoc.sctp_cmt_on_off == 1) {
857163953Srrs				chk->no_fr_allowed = 1;
858163953Srrs			}
859210599Srrs#ifdef THIS_SHOULD_NOT_BE_DONE
860163953Srrs		} else if (chk->sent == SCTP_DATAGRAM_ACKED) {
861163953Srrs			/* remember highest acked one */
862163953Srrs			could_be_sent = chk;
863210599Srrs#endif
864163953Srrs		}
865163953Srrs		if (chk->sent == SCTP_DATAGRAM_RESEND) {
866163953Srrs			cnt_mk++;
867163953Srrs		}
868163953Srrs	}
869168709Srrs	if ((orig_flight - net->flight_size) != (orig_tf - stcb->asoc.total_flight)) {
870168709Srrs		/* we did not subtract the same things? */
871168709Srrs		audit_tf = 1;
872168709Srrs	}
873179783Srrs	if (SCTP_BASE_SYSCTL(sctp_logging_level) & (SCTP_EARLYFR_LOGGING_ENABLE | SCTP_FR_LOGGING_ENABLE)) {
874170744Srrs		sctp_log_fr(tsnfirst, tsnlast, num_mk, SCTP_FR_T3_TIMEOUT);
875170744Srrs	}
876163953Srrs#ifdef SCTP_DEBUG
877169420Srrs	if (num_mk) {
878169420Srrs		SCTPDBG(SCTP_DEBUG_TIMER1, "LAST TSN marked was %x\n",
879169420Srrs		    tsnlast);
880169420Srrs		SCTPDBG(SCTP_DEBUG_TIMER1, "Num marked for retransmission was %d peer-rwd:%ld\n",
881169420Srrs		    num_mk, (u_long)stcb->asoc.peers_rwnd);
882169420Srrs		SCTPDBG(SCTP_DEBUG_TIMER1, "LAST TSN marked was %x\n",
883169420Srrs		    tsnlast);
884169420Srrs		SCTPDBG(SCTP_DEBUG_TIMER1, "Num marked for retransmission was %d peer-rwd:%d\n",
885169420Srrs		    num_mk,
886169420Srrs		    (int)stcb->asoc.peers_rwnd);
887163953Srrs	}
888163953Srrs#endif
889163953Srrs	*num_marked = num_mk;
890210599Srrs	*num_abandoned = cnt_abandoned;
891210493Srrs	/*
892210493Srrs	 * Now check for a ECN Echo that may be stranded And include the
893210493Srrs	 * cnt_mk'd to have all resends in the control queue.
894210493Srrs	 */
895210493Srrs	TAILQ_FOREACH(chk, &stcb->asoc.control_send_queue, sctp_next) {
896210493Srrs		if (chk->sent == SCTP_DATAGRAM_RESEND) {
897210493Srrs			cnt_mk++;
898210493Srrs		}
899210493Srrs		if ((chk->whoTo == net) &&
900210493Srrs		    (chk->rec.chunk_id.id == SCTP_ECN_ECHO)) {
901210493Srrs			sctp_free_remote_addr(chk->whoTo);
902210493Srrs			chk->whoTo = alt;
903210493Srrs			if (chk->sent != SCTP_DATAGRAM_RESEND) {
904210493Srrs				chk->sent = SCTP_DATAGRAM_RESEND;
905210493Srrs				sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
906210493Srrs				cnt_mk++;
907210493Srrs			}
908210493Srrs			atomic_add_int(&alt->ref_count, 1);
909210493Srrs		}
910210493Srrs	}
911210599Srrs#ifdef THIS_SHOULD_NOT_BE_DONE
912163953Srrs	if ((stcb->asoc.sent_queue_retran_cnt == 0) && (could_be_sent)) {
913163953Srrs		/* fix it so we retransmit the highest acked anyway */
914163953Srrs		sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
915163953Srrs		cnt_mk++;
916163953Srrs		could_be_sent->sent = SCTP_DATAGRAM_RESEND;
917163953Srrs	}
918210599Srrs#endif
919163953Srrs	if (stcb->asoc.sent_queue_retran_cnt != cnt_mk) {
920165220Srrs#ifdef INVARIANTS
921171477Srrs		SCTP_PRINTF("Local Audit says there are %d for retran asoc cnt:%d we marked:%d this time\n",
922171477Srrs		    cnt_mk, stcb->asoc.sent_queue_retran_cnt, num_mk);
923163953Srrs#endif
924163953Srrs#ifndef SCTP_AUDITING_ENABLED
925163953Srrs		stcb->asoc.sent_queue_retran_cnt = cnt_mk;
926163953Srrs#endif
927163953Srrs	}
928163953Srrs	if (audit_tf) {
929169420Srrs		SCTPDBG(SCTP_DEBUG_TIMER4,
930169420Srrs		    "Audit total flight due to negative value net:%p\n",
931169420Srrs		    net);
932163953Srrs		stcb->asoc.total_flight = 0;
933163953Srrs		stcb->asoc.total_flight_count = 0;
934163953Srrs		/* Clear all networks flight size */
935163953Srrs		TAILQ_FOREACH(lnets, &stcb->asoc.nets, sctp_next) {
936163953Srrs			lnets->flight_size = 0;
937169420Srrs			SCTPDBG(SCTP_DEBUG_TIMER4,
938169420Srrs			    "Net:%p c-f cwnd:%d ssthresh:%d\n",
939169420Srrs			    lnets, lnets->cwnd, lnets->ssthresh);
940163953Srrs		}
941163953Srrs		TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
942163953Srrs			if (chk->sent < SCTP_DATAGRAM_RESEND) {
943179783Srrs				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
944170744Srrs					sctp_misc_ints(SCTP_FLIGHT_LOG_UP,
945170744Srrs					    chk->whoTo->flight_size,
946170744Srrs					    chk->book_size,
947170744Srrs					    (uintptr_t) chk->whoTo,
948170744Srrs					    chk->rec.data.TSN_seq);
949170744Srrs				}
950168709Srrs				sctp_flight_size_increase(chk);
951168709Srrs				sctp_total_flight_increase(stcb, chk);
952163953Srrs			}
953163953Srrs		}
954163953Srrs	}
955163953Srrs	/*
956163953Srrs	 * Setup the ecn nonce re-sync point. We do this since
957163953Srrs	 * retranmissions are NOT setup for ECN. This means that do to
958163953Srrs	 * Karn's rule, we don't know the total of the peers ecn bits.
959163953Srrs	 */
960163953Srrs	chk = TAILQ_FIRST(&stcb->asoc.send_queue);
961163953Srrs	if (chk == NULL) {
962163953Srrs		stcb->asoc.nonce_resync_tsn = stcb->asoc.sending_seq;
963163953Srrs	} else {
964163953Srrs		stcb->asoc.nonce_resync_tsn = chk->rec.data.TSN_seq;
965163953Srrs	}
966163953Srrs	stcb->asoc.nonce_wait_for_ecne = 0;
967163953Srrs	stcb->asoc.nonce_sum_check = 0;
968163953Srrs	/* We return 1 if we only have a window probe outstanding */
969163953Srrs	return (0);
970163953Srrs}
971163953Srrs
972163953Srrs
973163953Srrsint
974163953Srrssctp_t3rxt_timer(struct sctp_inpcb *inp,
975163953Srrs    struct sctp_tcb *stcb,
976163953Srrs    struct sctp_nets *net)
977163953Srrs{
978163953Srrs	struct sctp_nets *alt;
979210599Srrs	int win_probe, num_mk, num_abandoned;
980163953Srrs
981179783Srrs	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
982170744Srrs		sctp_log_fr(0, 0, 0, SCTP_FR_T3_TIMEOUT);
983170744Srrs	}
984179783Srrs	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
985163953Srrs		struct sctp_nets *lnet;
986163953Srrs
987163953Srrs		TAILQ_FOREACH(lnet, &stcb->asoc.nets, sctp_next) {
988163953Srrs			if (net == lnet) {
989163953Srrs				sctp_log_cwnd(stcb, lnet, 1, SCTP_CWND_LOG_FROM_T3);
990163953Srrs			} else {
991163953Srrs				sctp_log_cwnd(stcb, lnet, 0, SCTP_CWND_LOG_FROM_T3);
992163953Srrs			}
993163953Srrs		}
994163953Srrs	}
995163953Srrs	/* Find an alternate and mark those for retransmission */
996163953Srrs	if ((stcb->asoc.peers_rwnd == 0) &&
997163953Srrs	    (stcb->asoc.total_flight < net->mtu)) {
998163953Srrs		SCTP_STAT_INCR(sctps_timowindowprobe);
999163953Srrs		win_probe = 1;
1000163953Srrs	} else {
1001163953Srrs		win_probe = 0;
1002163953Srrs	}
1003168709Srrs
1004171440Srrs	/*
1005171440Srrs	 * JRS 5/14/07 - If CMT PF is on and the destination if not already
1006171440Srrs	 * in PF state, set the destination to PF state and store the
1007171440Srrs	 * current time as the time that the destination was last active. In
1008171440Srrs	 * addition, find an alternate destination with PF-based
1009171440Srrs	 * find_alt_net().
1010171440Srrs	 */
1011211944Stuexen	if ((stcb->asoc.sctp_cmt_on_off == 1) &&
1012211944Stuexen	    (stcb->asoc.sctp_cmt_pf > 0)) {
1013171440Srrs		if ((net->dest_state & SCTP_ADDR_PF) != SCTP_ADDR_PF) {
1014171440Srrs			net->dest_state |= SCTP_ADDR_PF;
1015171477Srrs			net->last_active = sctp_get_tick_count();
1016171440Srrs			SCTPDBG(SCTP_DEBUG_TIMER4, "Destination %p moved from active to PF.\n",
1017171440Srrs			    net);
1018171440Srrs		}
1019171440Srrs		alt = sctp_find_alternate_net(stcb, net, 2);
1020211944Stuexen	} else if (stcb->asoc.sctp_cmt_on_off == 1) {
1021168709Srrs		/*
1022168709Srrs		 * CMT: Using RTX_SSTHRESH policy for CMT. If CMT is being
1023168709Srrs		 * used, then pick dest with largest ssthresh for any
1024168709Srrs		 * retransmission.
1025168709Srrs		 */
1026168709Srrs		alt = net;
1027168709Srrs		alt = sctp_find_alternate_net(stcb, alt, 1);
1028168709Srrs		/*
1029168709Srrs		 * CUCv2: If a different dest is picked for the
1030168709Srrs		 * retransmission, then new (rtx-)pseudo_cumack needs to be
1031168709Srrs		 * tracked for orig dest. Let CUCv2 track new (rtx-)
1032168709Srrs		 * pseudo-cumack always.
1033168709Srrs		 */
1034168709Srrs		net->find_pseudo_cumack = 1;
1035168709Srrs		net->find_rtx_pseudo_cumack = 1;
1036168709Srrs	} else {		/* CMT is OFF */
1037168709Srrs		alt = sctp_find_alternate_net(stcb, net, 0);
1038168709Srrs	}
1039210599Srrs	num_mk = 0;
1040210599Srrs	num_abandoned = 0;
1041210599Srrs	(void)sctp_mark_all_for_resend(stcb, net, alt, win_probe,
1042210599Srrs	    &num_mk, &num_abandoned);
1043163953Srrs	/* FR Loss recovery just ended with the T3. */
1044163953Srrs	stcb->asoc.fast_retran_loss_recovery = 0;
1045163953Srrs
1046163953Srrs	/* CMT FR loss recovery ended with the T3 */
1047163953Srrs	net->fast_retran_loss_recovery = 0;
1048163953Srrs
1049163953Srrs	/*
1050163953Srrs	 * setup the sat loss recovery that prevents satellite cwnd advance.
1051163953Srrs	 */
1052163953Srrs	stcb->asoc.sat_t3_loss_recovery = 1;
1053163953Srrs	stcb->asoc.sat_t3_recovery_tsn = stcb->asoc.sending_seq;
1054163953Srrs
1055163953Srrs	/* Backoff the timer and cwnd */
1056210599Srrs	sctp_backoff_on_timeout(stcb, net, win_probe, num_mk, num_abandoned);
1057163953Srrs	if (win_probe == 0) {
1058163953Srrs		/* We don't do normal threshold management on window probes */
1059163953Srrs		if (sctp_threshold_management(inp, stcb, net,
1060163953Srrs		    stcb->asoc.max_send_times)) {
1061163953Srrs			/* Association was destroyed */
1062163953Srrs			return (1);
1063163953Srrs		} else {
1064163953Srrs			if (net != stcb->asoc.primary_destination) {
1065163953Srrs				/* send a immediate HB if our RTO is stale */
1066163953Srrs				struct timeval now;
1067163953Srrs				unsigned int ms_goneby;
1068163953Srrs
1069169378Srrs				(void)SCTP_GETTIME_TIMEVAL(&now);
1070163953Srrs				if (net->last_sent_time.tv_sec) {
1071163953Srrs					ms_goneby = (now.tv_sec - net->last_sent_time.tv_sec) * 1000;
1072163953Srrs				} else {
1073163953Srrs					ms_goneby = 0;
1074163953Srrs				}
1075163953Srrs				if ((ms_goneby > net->RTO) || (net->RTO == 0)) {
1076163953Srrs					/*
1077163953Srrs					 * no recent feed back in an RTO or
1078163953Srrs					 * more, request a RTT update
1079163953Srrs					 */
1080171440Srrs					if (sctp_send_hb(stcb, 1, net) < 0)
1081185694Srrs						/*
1082185694Srrs						 * Less than 0 means we lost
1083185694Srrs						 * the assoc
1084185694Srrs						 */
1085185694Srrs						return (1);
1086163953Srrs				}
1087163953Srrs			}
1088163953Srrs		}
1089163953Srrs	} else {
1090163953Srrs		/*
1091163953Srrs		 * For a window probe we don't penalize the net's but only
1092163953Srrs		 * the association. This may fail it if SACKs are not coming
1093163953Srrs		 * back. If sack's are coming with rwnd locked at 0, we will
1094163953Srrs		 * continue to hold things waiting for rwnd to raise
1095163953Srrs		 */
1096163953Srrs		if (sctp_threshold_management(inp, stcb, NULL,
1097163953Srrs		    stcb->asoc.max_send_times)) {
1098163953Srrs			/* Association was destroyed */
1099163953Srrs			return (1);
1100163953Srrs		}
1101163953Srrs	}
1102163953Srrs	if (net->dest_state & SCTP_ADDR_NOT_REACHABLE) {
1103163953Srrs		/* Move all pending over too */
1104212712Stuexen		sctp_move_chunks_from_net(stcb, net);
1105169352Srrs
1106169352Srrs		/*
1107169352Srrs		 * Get the address that failed, to force a new src address
1108169352Srrs		 * selecton and a route allocation.
1109169352Srrs		 */
1110169352Srrs		if (net->ro._s_addr) {
1111169352Srrs			sctp_free_ifa(net->ro._s_addr);
1112169352Srrs			net->ro._s_addr = NULL;
1113169352Srrs		}
1114169352Srrs		net->src_addr_selected = 0;
1115169352Srrs
1116169352Srrs		/* Force a route allocation too */
1117169352Srrs		if (net->ro.ro_rt) {
1118169352Srrs			RTFREE(net->ro.ro_rt);
1119169352Srrs			net->ro.ro_rt = NULL;
1120169352Srrs		}
1121163953Srrs		/* Was it our primary? */
1122163953Srrs		if ((stcb->asoc.primary_destination == net) && (alt != net)) {
1123163953Srrs			/*
1124163953Srrs			 * Yes, note it as such and find an alternate note:
1125163953Srrs			 * this means HB code must use this to resent the
1126163953Srrs			 * primary if it goes active AND if someone does a
1127163953Srrs			 * change-primary then this flag must be cleared
1128163953Srrs			 * from any net structures.
1129163953Srrs			 */
1130163953Srrs			if (sctp_set_primary_addr(stcb,
1131163953Srrs			    (struct sockaddr *)NULL,
1132163953Srrs			    alt) == 0) {
1133163953Srrs				net->dest_state |= SCTP_ADDR_WAS_PRIMARY;
1134163953Srrs			}
1135163953Srrs		}
1136211944Stuexen	} else if ((stcb->asoc.sctp_cmt_on_off == 1) &&
1137211944Stuexen		    (stcb->asoc.sctp_cmt_pf > 0) &&
1138211944Stuexen	    ((net->dest_state & SCTP_ADDR_PF) == SCTP_ADDR_PF)) {
1139171440Srrs		/*
1140171440Srrs		 * JRS 5/14/07 - If the destination hasn't failed completely
1141171440Srrs		 * but is in PF state, a PF-heartbeat needs to be sent
1142171440Srrs		 * manually.
1143171440Srrs		 */
1144171440Srrs		if (sctp_send_hb(stcb, 1, net) < 0)
1145185694Srrs			/* Return less than 0 means we lost the association */
1146185694Srrs			return (1);
1147163953Srrs	}
1148163953Srrs	/*
1149163953Srrs	 * Special case for cookie-echo'ed case, we don't do output but must
1150163953Srrs	 * await the COOKIE-ACK before retransmission
1151163953Srrs	 */
1152163953Srrs	if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_ECHOED) {
1153163953Srrs		/*
1154163953Srrs		 * Here we just reset the timer and start again since we
1155163953Srrs		 * have not established the asoc
1156163953Srrs		 */
1157163953Srrs		sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
1158163953Srrs		return (0);
1159163953Srrs	}
1160163953Srrs	if (stcb->asoc.peer_supports_prsctp) {
1161163953Srrs		struct sctp_tmit_chunk *lchk;
1162163953Srrs
1163163953Srrs		lchk = sctp_try_advance_peer_ack_point(stcb, &stcb->asoc);
1164163953Srrs		/* C3. See if we need to send a Fwd-TSN */
1165163953Srrs		if (compare_with_wrap(stcb->asoc.advanced_peer_ack_point,
1166163953Srrs		    stcb->asoc.last_acked_seq, MAX_TSN)) {
1167163953Srrs			/*
1168163953Srrs			 * ISSUE with ECN, see FWD-TSN processing for notes
1169163953Srrs			 * on issues that will occur when the ECN NONCE
1170163953Srrs			 * stuff is put into SCTP for cross checking.
1171163953Srrs			 */
1172163953Srrs			send_forward_tsn(stcb, &stcb->asoc);
1173163953Srrs			if (lchk) {
1174163953Srrs				/* Assure a timer is up */
1175163953Srrs				sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, stcb, lchk->whoTo);
1176163953Srrs			}
1177163953Srrs		}
1178163953Srrs	}
1179179783Srrs	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_MONITOR_ENABLE) {
1180170744Srrs		sctp_log_cwnd(stcb, net, net->cwnd, SCTP_CWND_LOG_FROM_RTX);
1181170744Srrs	}
1182163953Srrs	return (0);
1183163953Srrs}
1184163953Srrs
1185163953Srrsint
1186163953Srrssctp_t1init_timer(struct sctp_inpcb *inp,
1187163953Srrs    struct sctp_tcb *stcb,
1188163953Srrs    struct sctp_nets *net)
1189163953Srrs{
1190163953Srrs	/* bump the thresholds */
1191163953Srrs	if (stcb->asoc.delayed_connection) {
1192163953Srrs		/*
1193163953Srrs		 * special hook for delayed connection. The library did NOT
1194163953Srrs		 * complete the rest of its sends.
1195163953Srrs		 */
1196163953Srrs		stcb->asoc.delayed_connection = 0;
1197172090Srrs		sctp_send_initiate(inp, stcb, SCTP_SO_NOT_LOCKED);
1198163953Srrs		return (0);
1199163953Srrs	}
1200163953Srrs	if (SCTP_GET_STATE((&stcb->asoc)) != SCTP_STATE_COOKIE_WAIT) {
1201163953Srrs		return (0);
1202163953Srrs	}
1203163953Srrs	if (sctp_threshold_management(inp, stcb, net,
1204163953Srrs	    stcb->asoc.max_init_times)) {
1205163953Srrs		/* Association was destroyed */
1206163953Srrs		return (1);
1207163953Srrs	}
1208163953Srrs	stcb->asoc.dropped_special_cnt = 0;
1209210599Srrs	sctp_backoff_on_timeout(stcb, stcb->asoc.primary_destination, 1, 0, 0);
1210163953Srrs	if (stcb->asoc.initial_init_rto_max < net->RTO) {
1211163953Srrs		net->RTO = stcb->asoc.initial_init_rto_max;
1212163953Srrs	}
1213163953Srrs	if (stcb->asoc.numnets > 1) {
1214163953Srrs		/* If we have more than one addr use it */
1215163953Srrs		struct sctp_nets *alt;
1216163953Srrs
1217163953Srrs		alt = sctp_find_alternate_net(stcb, stcb->asoc.primary_destination, 0);
1218163953Srrs		if ((alt != NULL) && (alt != stcb->asoc.primary_destination)) {
1219212712Stuexen			sctp_move_chunks_from_net(stcb, stcb->asoc.primary_destination);
1220163953Srrs			stcb->asoc.primary_destination = alt;
1221163953Srrs		}
1222163953Srrs	}
1223163953Srrs	/* Send out a new init */
1224172090Srrs	sctp_send_initiate(inp, stcb, SCTP_SO_NOT_LOCKED);
1225163953Srrs	return (0);
1226163953Srrs}
1227163953Srrs
1228163953Srrs/*
1229163953Srrs * For cookie and asconf we actually need to find and mark for resend, then
1230163953Srrs * increment the resend counter (after all the threshold management stuff of
1231163953Srrs * course).
1232163953Srrs */
1233163953Srrsint
1234163953Srrssctp_cookie_timer(struct sctp_inpcb *inp,
1235163953Srrs    struct sctp_tcb *stcb,
1236163953Srrs    struct sctp_nets *net)
1237163953Srrs{
1238163953Srrs	struct sctp_nets *alt;
1239163953Srrs	struct sctp_tmit_chunk *cookie;
1240163953Srrs
1241163953Srrs	/* first before all else we must find the cookie */
1242163953Srrs	TAILQ_FOREACH(cookie, &stcb->asoc.control_send_queue, sctp_next) {
1243163953Srrs		if (cookie->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
1244163953Srrs			break;
1245163953Srrs		}
1246163953Srrs	}
1247163953Srrs	if (cookie == NULL) {
1248163953Srrs		if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_ECHOED) {
1249163953Srrs			/* FOOBAR! */
1250163953Srrs			struct mbuf *oper;
1251163953Srrs
1252163953Srrs			oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
1253163953Srrs			    0, M_DONTWAIT, 1, MT_DATA);
1254163953Srrs			if (oper) {
1255163953Srrs				struct sctp_paramhdr *ph;
1256163953Srrs				uint32_t *ippp;
1257163953Srrs
1258165647Srrs				SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
1259163953Srrs				    sizeof(uint32_t);
1260163953Srrs				ph = mtod(oper, struct sctp_paramhdr *);
1261163953Srrs				ph->param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1262165647Srrs				ph->param_length = htons(SCTP_BUF_LEN(oper));
1263163953Srrs				ippp = (uint32_t *) (ph + 1);
1264171440Srrs				*ippp = htonl(SCTP_FROM_SCTP_TIMER + SCTP_LOC_3);
1265163953Srrs			}
1266171440Srrs			inp->last_abort_code = SCTP_FROM_SCTP_TIMER + SCTP_LOC_4;
1267163953Srrs			sctp_abort_an_association(inp, stcb, SCTP_INTERNAL_ERROR,
1268172090Srrs			    oper, SCTP_SO_NOT_LOCKED);
1269163953Srrs		} else {
1270165220Srrs#ifdef INVARIANTS
1271163953Srrs			panic("Cookie timer expires in wrong state?");
1272163953Srrs#else
1273169420Srrs			SCTP_PRINTF("Strange in state %d not cookie-echoed yet c-e timer expires?\n", SCTP_GET_STATE(&stcb->asoc));
1274163953Srrs			return (0);
1275163953Srrs#endif
1276163953Srrs		}
1277163953Srrs		return (0);
1278163953Srrs	}
1279163953Srrs	/* Ok we found the cookie, threshold management next */
1280163953Srrs	if (sctp_threshold_management(inp, stcb, cookie->whoTo,
1281163953Srrs	    stcb->asoc.max_init_times)) {
1282163953Srrs		/* Assoc is over */
1283163953Srrs		return (1);
1284163953Srrs	}
1285163953Srrs	/*
1286163953Srrs	 * cleared theshold management now lets backoff the address & select
1287163953Srrs	 * an alternate
1288163953Srrs	 */
1289163953Srrs	stcb->asoc.dropped_special_cnt = 0;
1290210599Srrs	sctp_backoff_on_timeout(stcb, cookie->whoTo, 1, 0, 0);
1291163953Srrs	alt = sctp_find_alternate_net(stcb, cookie->whoTo, 0);
1292163953Srrs	if (alt != cookie->whoTo) {
1293163953Srrs		sctp_free_remote_addr(cookie->whoTo);
1294163953Srrs		cookie->whoTo = alt;
1295163953Srrs		atomic_add_int(&alt->ref_count, 1);
1296163953Srrs	}
1297163953Srrs	/* Now mark the retran info */
1298163953Srrs	if (cookie->sent != SCTP_DATAGRAM_RESEND) {
1299163953Srrs		sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
1300163953Srrs	}
1301163953Srrs	cookie->sent = SCTP_DATAGRAM_RESEND;
1302163953Srrs	/*
1303163953Srrs	 * Now call the output routine to kick out the cookie again, Note we
1304163953Srrs	 * don't mark any chunks for retran so that FR will need to kick in
1305163953Srrs	 * to move these (or a send timer).
1306163953Srrs	 */
1307163953Srrs	return (0);
1308163953Srrs}
1309163953Srrs
1310163953Srrsint
1311163953Srrssctp_strreset_timer(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
1312163953Srrs    struct sctp_nets *net)
1313163953Srrs{
1314163953Srrs	struct sctp_nets *alt;
1315163953Srrs	struct sctp_tmit_chunk *strrst = NULL, *chk = NULL;
1316163953Srrs
1317163953Srrs	if (stcb->asoc.stream_reset_outstanding == 0) {
1318163953Srrs		return (0);
1319163953Srrs	}
1320163953Srrs	/* find the existing STRRESET, we use the seq number we sent out on */
1321169420Srrs	(void)sctp_find_stream_reset(stcb, stcb->asoc.str_reset_seq_out, &strrst);
1322163953Srrs	if (strrst == NULL) {
1323163953Srrs		return (0);
1324163953Srrs	}
1325163953Srrs	/* do threshold management */
1326163953Srrs	if (sctp_threshold_management(inp, stcb, strrst->whoTo,
1327163953Srrs	    stcb->asoc.max_send_times)) {
1328163953Srrs		/* Assoc is over */
1329163953Srrs		return (1);
1330163953Srrs	}
1331163953Srrs	/*
1332163953Srrs	 * cleared theshold management now lets backoff the address & select
1333163953Srrs	 * an alternate
1334163953Srrs	 */
1335210599Srrs	sctp_backoff_on_timeout(stcb, strrst->whoTo, 1, 0, 0);
1336163953Srrs	alt = sctp_find_alternate_net(stcb, strrst->whoTo, 0);
1337163953Srrs	sctp_free_remote_addr(strrst->whoTo);
1338163953Srrs	strrst->whoTo = alt;
1339163953Srrs	atomic_add_int(&alt->ref_count, 1);
1340163953Srrs
1341163953Srrs	/* See if a ECN Echo is also stranded */
1342163953Srrs	TAILQ_FOREACH(chk, &stcb->asoc.control_send_queue, sctp_next) {
1343163953Srrs		if ((chk->whoTo == net) &&
1344163953Srrs		    (chk->rec.chunk_id.id == SCTP_ECN_ECHO)) {
1345163953Srrs			sctp_free_remote_addr(chk->whoTo);
1346163953Srrs			if (chk->sent != SCTP_DATAGRAM_RESEND) {
1347163953Srrs				chk->sent = SCTP_DATAGRAM_RESEND;
1348163953Srrs				sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
1349163953Srrs			}
1350163953Srrs			chk->whoTo = alt;
1351163953Srrs			atomic_add_int(&alt->ref_count, 1);
1352163953Srrs		}
1353163953Srrs	}
1354163953Srrs	if (net->dest_state & SCTP_ADDR_NOT_REACHABLE) {
1355163953Srrs		/*
1356163953Srrs		 * If the address went un-reachable, we need to move to
1357163953Srrs		 * alternates for ALL chk's in queue
1358163953Srrs		 */
1359212712Stuexen		sctp_move_chunks_from_net(stcb, net);
1360163953Srrs	}
1361163953Srrs	/* mark the retran info */
1362163953Srrs	if (strrst->sent != SCTP_DATAGRAM_RESEND)
1363163953Srrs		sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
1364163953Srrs	strrst->sent = SCTP_DATAGRAM_RESEND;
1365163953Srrs
1366163953Srrs	/* restart the timer */
1367163953Srrs	sctp_timer_start(SCTP_TIMER_TYPE_STRRESET, inp, stcb, strrst->whoTo);
1368163953Srrs	return (0);
1369163953Srrs}
1370163953Srrs
1371163953Srrsint
1372163953Srrssctp_asconf_timer(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
1373163953Srrs    struct sctp_nets *net)
1374163953Srrs{
1375163953Srrs	struct sctp_nets *alt;
1376179157Srrs	struct sctp_tmit_chunk *asconf, *chk, *nchk;
1377163953Srrs
1378171572Srrs	/* is this a first send, or a retransmission? */
1379179157Srrs	if (TAILQ_EMPTY(&stcb->asoc.asconf_send_queue)) {
1380163953Srrs		/* compose a new ASCONF chunk and send it */
1381172190Srrs		sctp_send_asconf(stcb, net, SCTP_ADDR_NOT_LOCKED);
1382163953Srrs	} else {
1383171572Srrs		/*
1384171572Srrs		 * Retransmission of the existing ASCONF is needed
1385171572Srrs		 */
1386163953Srrs
1387163953Srrs		/* find the existing ASCONF */
1388179157Srrs		asconf = TAILQ_FIRST(&stcb->asoc.asconf_send_queue);
1389163953Srrs		if (asconf == NULL) {
1390163953Srrs			return (0);
1391163953Srrs		}
1392163953Srrs		/* do threshold management */
1393163953Srrs		if (sctp_threshold_management(inp, stcb, asconf->whoTo,
1394163953Srrs		    stcb->asoc.max_send_times)) {
1395163953Srrs			/* Assoc is over */
1396163953Srrs			return (1);
1397163953Srrs		}
1398163953Srrs		if (asconf->snd_count > stcb->asoc.max_send_times) {
1399163953Srrs			/*
1400171572Srrs			 * Something is rotten: our peer is not responding
1401171572Srrs			 * to ASCONFs but apparently is to other chunks.
1402171572Srrs			 * i.e. it is not properly handling the chunk type
1403171572Srrs			 * upper bits. Mark this peer as ASCONF incapable
1404171572Srrs			 * and cleanup.
1405163953Srrs			 */
1406169420Srrs			SCTPDBG(SCTP_DEBUG_TIMER1, "asconf_timer: Peer has not responded to our repeated ASCONFs\n");
1407163953Srrs			sctp_asconf_cleanup(stcb, net);
1408163953Srrs			return (0);
1409163953Srrs		}
1410163953Srrs		/*
1411171572Srrs		 * cleared threshold management, so now backoff the net and
1412171572Srrs		 * select an alternate
1413163953Srrs		 */
1414210599Srrs		sctp_backoff_on_timeout(stcb, asconf->whoTo, 1, 0, 0);
1415163953Srrs		alt = sctp_find_alternate_net(stcb, asconf->whoTo, 0);
1416179157Srrs		if (asconf->whoTo != alt) {
1417179157Srrs			sctp_free_remote_addr(asconf->whoTo);
1418179157Srrs			asconf->whoTo = alt;
1419179157Srrs			atomic_add_int(&alt->ref_count, 1);
1420179157Srrs		}
1421171572Srrs		/* See if an ECN Echo is also stranded */
1422163953Srrs		TAILQ_FOREACH(chk, &stcb->asoc.control_send_queue, sctp_next) {
1423163953Srrs			if ((chk->whoTo == net) &&
1424163953Srrs			    (chk->rec.chunk_id.id == SCTP_ECN_ECHO)) {
1425163953Srrs				sctp_free_remote_addr(chk->whoTo);
1426163953Srrs				chk->whoTo = alt;
1427163953Srrs				if (chk->sent != SCTP_DATAGRAM_RESEND) {
1428163953Srrs					chk->sent = SCTP_DATAGRAM_RESEND;
1429163953Srrs					sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
1430163953Srrs				}
1431163953Srrs				atomic_add_int(&alt->ref_count, 1);
1432163953Srrs			}
1433163953Srrs		}
1434179157Srrs		for (chk = asconf; chk; chk = nchk) {
1435179157Srrs			nchk = TAILQ_NEXT(chk, sctp_next);
1436179157Srrs			if (chk->whoTo != alt) {
1437179157Srrs				sctp_free_remote_addr(chk->whoTo);
1438179157Srrs				chk->whoTo = alt;
1439179157Srrs				atomic_add_int(&alt->ref_count, 1);
1440179157Srrs			}
1441179157Srrs			if (asconf->sent != SCTP_DATAGRAM_RESEND && chk->sent != SCTP_DATAGRAM_UNSENT)
1442179157Srrs				sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
1443179157Srrs			chk->sent = SCTP_DATAGRAM_RESEND;
1444179157Srrs		}
1445163953Srrs		if (net->dest_state & SCTP_ADDR_NOT_REACHABLE) {
1446163953Srrs			/*
1447163953Srrs			 * If the address went un-reachable, we need to move
1448171572Srrs			 * to the alternate for ALL chunks in queue
1449163953Srrs			 */
1450212712Stuexen			sctp_move_chunks_from_net(stcb, net);
1451163953Srrs		}
1452163953Srrs		/* mark the retran info */
1453163953Srrs		if (asconf->sent != SCTP_DATAGRAM_RESEND)
1454163953Srrs			sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
1455163953Srrs		asconf->sent = SCTP_DATAGRAM_RESEND;
1456179157Srrs
1457179157Srrs		/* send another ASCONF if any and we can do */
1458179157Srrs		sctp_send_asconf(stcb, alt, SCTP_ADDR_NOT_LOCKED);
1459163953Srrs	}
1460163953Srrs	return (0);
1461163953Srrs}
1462163953Srrs
1463172091Srrs/* Mobility adaptation */
1464172156Srrsvoid
1465172091Srrssctp_delete_prim_timer(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
1466172091Srrs    struct sctp_nets *net)
1467172091Srrs{
1468172091Srrs	if (stcb->asoc.deleted_primary == NULL) {
1469172091Srrs		SCTPDBG(SCTP_DEBUG_ASCONF1, "delete_prim_timer: deleted_primary is not stored...\n");
1470172091Srrs		sctp_mobility_feature_off(inp, SCTP_MOBILITY_PRIM_DELETED);
1471172156Srrs		return;
1472172091Srrs	}
1473172091Srrs	SCTPDBG(SCTP_DEBUG_ASCONF1, "delete_prim_timer: finished to keep deleted primary ");
1474172091Srrs	SCTPDBG_ADDR(SCTP_DEBUG_ASCONF1, &stcb->asoc.deleted_primary->ro._l_addr.sa);
1475172091Srrs	sctp_free_remote_addr(stcb->asoc.deleted_primary);
1476172091Srrs	stcb->asoc.deleted_primary = NULL;
1477172091Srrs	sctp_mobility_feature_off(inp, SCTP_MOBILITY_PRIM_DELETED);
1478172156Srrs	return;
1479172091Srrs}
1480172091Srrs
1481163953Srrs/*
1482163953Srrs * For the shutdown and shutdown-ack, we do not keep one around on the
1483163953Srrs * control queue. This means we must generate a new one and call the general
1484163953Srrs * chunk output routine, AFTER having done threshold management.
1485163953Srrs */
1486163953Srrsint
1487163953Srrssctp_shutdown_timer(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
1488163953Srrs    struct sctp_nets *net)
1489163953Srrs{
1490163953Srrs	struct sctp_nets *alt;
1491163953Srrs
1492163953Srrs	/* first threshold managment */
1493163953Srrs	if (sctp_threshold_management(inp, stcb, net, stcb->asoc.max_send_times)) {
1494163953Srrs		/* Assoc is over */
1495163953Srrs		return (1);
1496163953Srrs	}
1497163953Srrs	/* second select an alternative */
1498163953Srrs	alt = sctp_find_alternate_net(stcb, net, 0);
1499163953Srrs
1500163953Srrs	/* third generate a shutdown into the queue for out net */
1501163953Srrs	if (alt) {
1502163953Srrs		sctp_send_shutdown(stcb, alt);
1503163953Srrs	} else {
1504163953Srrs		/*
1505163953Srrs		 * if alt is NULL, there is no dest to send to??
1506163953Srrs		 */
1507163953Srrs		return (0);
1508163953Srrs	}
1509163953Srrs	/* fourth restart timer */
1510163953Srrs	sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, inp, stcb, alt);
1511163953Srrs	return (0);
1512163953Srrs}
1513163953Srrs
1514163953Srrsint
1515163953Srrssctp_shutdownack_timer(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
1516163953Srrs    struct sctp_nets *net)
1517163953Srrs{
1518163953Srrs	struct sctp_nets *alt;
1519163953Srrs
1520163953Srrs	/* first threshold managment */
1521163953Srrs	if (sctp_threshold_management(inp, stcb, net, stcb->asoc.max_send_times)) {
1522163953Srrs		/* Assoc is over */
1523163953Srrs		return (1);
1524163953Srrs	}
1525163953Srrs	/* second select an alternative */
1526163953Srrs	alt = sctp_find_alternate_net(stcb, net, 0);
1527163953Srrs
1528163953Srrs	/* third generate a shutdown into the queue for out net */
1529163953Srrs	sctp_send_shutdown_ack(stcb, alt);
1530163953Srrs
1531163953Srrs	/* fourth restart timer */
1532163953Srrs	sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNACK, inp, stcb, alt);
1533163953Srrs	return (0);
1534163953Srrs}
1535163953Srrs
1536163953Srrsstatic void
1537163953Srrssctp_audit_stream_queues_for_size(struct sctp_inpcb *inp,
1538163953Srrs    struct sctp_tcb *stcb)
1539163953Srrs{
1540163953Srrs	struct sctp_stream_out *outs;
1541163953Srrs	struct sctp_stream_queue_pending *sp;
1542163953Srrs	unsigned int chks_in_queue = 0;
1543163953Srrs	int being_filled = 0;
1544163953Srrs
1545163953Srrs	/*
1546163953Srrs	 * This function is ONLY called when the send/sent queues are empty.
1547163953Srrs	 */
1548163953Srrs	if ((stcb == NULL) || (inp == NULL))
1549163953Srrs		return;
1550163953Srrs
1551163953Srrs	if (stcb->asoc.sent_queue_retran_cnt) {
1552169420Srrs		SCTP_PRINTF("Hmm, sent_queue_retran_cnt is non-zero %d\n",
1553163953Srrs		    stcb->asoc.sent_queue_retran_cnt);
1554163953Srrs		stcb->asoc.sent_queue_retran_cnt = 0;
1555163953Srrs	}
1556163953Srrs	SCTP_TCB_SEND_LOCK(stcb);
1557163953Srrs	if (TAILQ_EMPTY(&stcb->asoc.out_wheel)) {
1558163953Srrs		int i, cnt = 0;
1559163953Srrs
1560163953Srrs		/* Check to see if a spoke fell off the wheel */
1561163953Srrs		for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
1562163953Srrs			if (!TAILQ_EMPTY(&stcb->asoc.strmout[i].outqueue)) {
1563163953Srrs				sctp_insert_on_wheel(stcb, &stcb->asoc, &stcb->asoc.strmout[i], 1);
1564163953Srrs				cnt++;
1565163953Srrs			}
1566163953Srrs		}
1567163953Srrs		if (cnt) {
1568163953Srrs			/* yep, we lost a spoke or two */
1569169420Srrs			SCTP_PRINTF("Found an additional %d streams NOT on outwheel, corrected\n", cnt);
1570163953Srrs		} else {
1571163953Srrs			/* no spokes lost, */
1572163953Srrs			stcb->asoc.total_output_queue_size = 0;
1573163953Srrs		}
1574163953Srrs		SCTP_TCB_SEND_UNLOCK(stcb);
1575163953Srrs		return;
1576163953Srrs	}
1577163953Srrs	SCTP_TCB_SEND_UNLOCK(stcb);
1578163953Srrs	/* Check to see if some data queued, if so report it */
1579163953Srrs	TAILQ_FOREACH(outs, &stcb->asoc.out_wheel, next_spoke) {
1580163953Srrs		if (!TAILQ_EMPTY(&outs->outqueue)) {
1581163953Srrs			TAILQ_FOREACH(sp, &outs->outqueue, next) {
1582163953Srrs				if (sp->msg_is_complete)
1583163953Srrs					being_filled++;
1584163953Srrs				chks_in_queue++;
1585163953Srrs			}
1586163953Srrs		}
1587163953Srrs	}
1588163953Srrs	if (chks_in_queue != stcb->asoc.stream_queue_cnt) {
1589169420Srrs		SCTP_PRINTF("Hmm, stream queue cnt at %d I counted %d in stream out wheel\n",
1590163953Srrs		    stcb->asoc.stream_queue_cnt, chks_in_queue);
1591163953Srrs	}
1592163953Srrs	if (chks_in_queue) {
1593163953Srrs		/* call the output queue function */
1594172090Srrs		sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_T3, SCTP_SO_NOT_LOCKED);
1595163953Srrs		if ((TAILQ_EMPTY(&stcb->asoc.send_queue)) &&
1596163953Srrs		    (TAILQ_EMPTY(&stcb->asoc.sent_queue))) {
1597163953Srrs			/*
1598163953Srrs			 * Probably should go in and make it go back through
1599163953Srrs			 * and add fragments allowed
1600163953Srrs			 */
1601163953Srrs			if (being_filled == 0) {
1602169420Srrs				SCTP_PRINTF("Still nothing moved %d chunks are stuck\n",
1603163953Srrs				    chks_in_queue);
1604163953Srrs			}
1605163953Srrs		}
1606163953Srrs	} else {
1607169420Srrs		SCTP_PRINTF("Found no chunks on any queue tot:%lu\n",
1608163953Srrs		    (u_long)stcb->asoc.total_output_queue_size);
1609163953Srrs		stcb->asoc.total_output_queue_size = 0;
1610163953Srrs	}
1611163953Srrs}
1612163953Srrs
1613163953Srrsint
1614163953Srrssctp_heartbeat_timer(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
1615163953Srrs    struct sctp_nets *net, int cnt_of_unconf)
1616163953Srrs{
1617171440Srrs	int ret;
1618171440Srrs
1619163953Srrs	if (net) {
1620163953Srrs		if (net->hb_responded == 0) {
1621167598Srrs			if (net->ro._s_addr) {
1622167598Srrs				/*
1623167598Srrs				 * Invalidate the src address if we did not
1624167598Srrs				 * get a response last time.
1625167598Srrs				 */
1626167598Srrs				sctp_free_ifa(net->ro._s_addr);
1627167598Srrs				net->ro._s_addr = NULL;
1628167598Srrs				net->src_addr_selected = 0;
1629167598Srrs			}
1630210599Srrs			sctp_backoff_on_timeout(stcb, net, 1, 0, 0);
1631163953Srrs		}
1632163953Srrs		/* Zero PBA, if it needs it */
1633163953Srrs		if (net->partial_bytes_acked) {
1634163953Srrs			net->partial_bytes_acked = 0;
1635163953Srrs		}
1636163953Srrs	}
1637163953Srrs	if ((stcb->asoc.total_output_queue_size > 0) &&
1638163953Srrs	    (TAILQ_EMPTY(&stcb->asoc.send_queue)) &&
1639163953Srrs	    (TAILQ_EMPTY(&stcb->asoc.sent_queue))) {
1640163953Srrs		sctp_audit_stream_queues_for_size(inp, stcb);
1641163953Srrs	}
1642163953Srrs	/* Send a new HB, this will do threshold managment, pick a new dest */
1643163953Srrs	if (cnt_of_unconf == 0) {
1644163953Srrs		if (sctp_send_hb(stcb, 0, NULL) < 0) {
1645163953Srrs			return (1);
1646163953Srrs		}
1647163953Srrs	} else {
1648163953Srrs		/*
1649163953Srrs		 * this will send out extra hb's up to maxburst if there are
1650163953Srrs		 * any unconfirmed addresses.
1651163953Srrs		 */
1652170056Srrs		uint32_t cnt_sent = 0;
1653163953Srrs
1654163953Srrs		TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1655163953Srrs			if ((net->dest_state & SCTP_ADDR_UNCONFIRMED) &&
1656163953Srrs			    (net->dest_state & SCTP_ADDR_REACHABLE)) {
1657163953Srrs				cnt_sent++;
1658167598Srrs				if (net->hb_responded == 0) {
1659167598Srrs					/* Did we respond last time? */
1660167598Srrs					if (net->ro._s_addr) {
1661167598Srrs						sctp_free_ifa(net->ro._s_addr);
1662167598Srrs						net->ro._s_addr = NULL;
1663167598Srrs						net->src_addr_selected = 0;
1664167598Srrs					}
1665167598Srrs				}
1666171440Srrs				ret = sctp_send_hb(stcb, 1, net);
1667171440Srrs				if (ret < 0)
1668171440Srrs					return 1;
1669171440Srrs				else if (ret == 0) {
1670163953Srrs					break;
1671163953Srrs				}
1672179783Srrs				if (cnt_sent >= SCTP_BASE_SYSCTL(sctp_hb_maxburst))
1673163953Srrs					break;
1674163953Srrs			}
1675163953Srrs		}
1676163953Srrs	}
1677163953Srrs	return (0);
1678163953Srrs}
1679163953Srrs
1680163953Srrsint
1681163953Srrssctp_is_hb_timer_running(struct sctp_tcb *stcb)
1682163953Srrs{
1683165647Srrs	if (SCTP_OS_TIMER_PENDING(&stcb->asoc.hb_timer.timer)) {
1684163953Srrs		/* its running */
1685163953Srrs		return (1);
1686163953Srrs	} else {
1687163953Srrs		/* nope */
1688163953Srrs		return (0);
1689163953Srrs	}
1690163953Srrs}
1691163953Srrs
1692163953Srrsint
1693163953Srrssctp_is_sack_timer_running(struct sctp_tcb *stcb)
1694163953Srrs{
1695165647Srrs	if (SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
1696163953Srrs		/* its running */
1697163953Srrs		return (1);
1698163953Srrs	} else {
1699163953Srrs		/* nope */
1700163953Srrs		return (0);
1701163953Srrs	}
1702163953Srrs}
1703163953Srrs
1704163953Srrs#define SCTP_NUMBER_OF_MTU_SIZES 18
1705163953Srrsstatic uint32_t mtu_sizes[] = {
1706163953Srrs	68,
1707163953Srrs	296,
1708163953Srrs	508,
1709163953Srrs	512,
1710163953Srrs	544,
1711163953Srrs	576,
1712163953Srrs	1006,
1713163953Srrs	1492,
1714163953Srrs	1500,
1715163953Srrs	1536,
1716163953Srrs	2002,
1717163953Srrs	2048,
1718163953Srrs	4352,
1719163953Srrs	4464,
1720163953Srrs	8166,
1721163953Srrs	17914,
1722163953Srrs	32000,
1723163953Srrs	65535
1724163953Srrs};
1725163953Srrs
1726163953Srrs
1727163953Srrsstatic uint32_t
1728163953Srrssctp_getnext_mtu(struct sctp_inpcb *inp, uint32_t cur_mtu)
1729163953Srrs{
1730163953Srrs	/* select another MTU that is just bigger than this one */
1731163953Srrs	int i;
1732163953Srrs
1733163953Srrs	for (i = 0; i < SCTP_NUMBER_OF_MTU_SIZES; i++) {
1734163953Srrs		if (cur_mtu < mtu_sizes[i]) {
1735163953Srrs			/* no max_mtu is bigger than this one */
1736163953Srrs			return (mtu_sizes[i]);
1737163953Srrs		}
1738163953Srrs	}
1739163953Srrs	/* here return the highest allowable */
1740163953Srrs	return (cur_mtu);
1741163953Srrs}
1742163953Srrs
1743163953Srrs
1744163953Srrsvoid
1745163953Srrssctp_pathmtu_timer(struct sctp_inpcb *inp,
1746163953Srrs    struct sctp_tcb *stcb,
1747163953Srrs    struct sctp_nets *net)
1748163953Srrs{
1749179157Srrs	uint32_t next_mtu, mtu;
1750163953Srrs
1751163953Srrs	next_mtu = sctp_getnext_mtu(inp, net->mtu);
1752169352Srrs
1753179157Srrs	if ((next_mtu > net->mtu) && (net->port == 0)) {
1754169352Srrs		if ((net->src_addr_selected == 0) ||
1755169352Srrs		    (net->ro._s_addr == NULL) ||
1756169352Srrs		    (net->ro._s_addr->localifa_flags & SCTP_BEING_DELETED)) {
1757169420Srrs			if ((net->ro._s_addr != NULL) && (net->ro._s_addr->localifa_flags & SCTP_BEING_DELETED)) {
1758169352Srrs				sctp_free_ifa(net->ro._s_addr);
1759169352Srrs				net->ro._s_addr = NULL;
1760169352Srrs				net->src_addr_selected = 0;
1761169420Srrs			} else if (net->ro._s_addr == NULL) {
1762179157Srrs#if defined(INET6) && defined(SCTP_EMBEDDED_V6_SCOPE)
1763179157Srrs				if (net->ro._l_addr.sa.sa_family == AF_INET6) {
1764179157Srrs					struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
1765179157Srrs
1766179157Srrs					/* KAME hack: embed scopeid */
1767197288Srrs					(void)sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone));
1768179157Srrs				}
1769179157Srrs#endif
1770179157Srrs
1771169420Srrs				net->ro._s_addr = sctp_source_address_selection(inp,
1772169420Srrs				    stcb,
1773169420Srrs				    (sctp_route_t *) & net->ro,
1774169420Srrs				    net, 0, stcb->asoc.vrf_id);
1775179157Srrs#if defined(INET6) && defined(SCTP_EMBEDDED_V6_SCOPE)
1776179157Srrs				if (net->ro._l_addr.sa.sa_family == AF_INET6) {
1777179157Srrs					struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
1778179157Srrs
1779179157Srrs					(void)sa6_recoverscope(sin6);
1780179157Srrs				}
1781179157Srrs#endif				/* INET6 */
1782169352Srrs			}
1783169352Srrs			if (net->ro._s_addr)
1784169352Srrs				net->src_addr_selected = 1;
1785169352Srrs		}
1786169352Srrs		if (net->ro._s_addr) {
1787169352Srrs			mtu = SCTP_GATHER_MTU_FROM_ROUTE(net->ro._s_addr, &net->ro._s_addr.sa, net->ro.ro_rt);
1788185694Srrs			if (net->port) {
1789185694Srrs				mtu -= sizeof(struct udphdr);
1790185694Srrs			}
1791169352Srrs			if (mtu > next_mtu) {
1792163953Srrs				net->mtu = next_mtu;
1793163953Srrs			}
1794163953Srrs		}
1795163953Srrs	}
1796163953Srrs	/* restart the timer */
1797163953Srrs	sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net);
1798163953Srrs}
1799163953Srrs
1800163953Srrsvoid
1801163953Srrssctp_autoclose_timer(struct sctp_inpcb *inp,
1802163953Srrs    struct sctp_tcb *stcb,
1803163953Srrs    struct sctp_nets *net)
1804163953Srrs{
1805163953Srrs	struct timeval tn, *tim_touse;
1806163953Srrs	struct sctp_association *asoc;
1807163953Srrs	int ticks_gone_by;
1808163953Srrs
1809169378Srrs	(void)SCTP_GETTIME_TIMEVAL(&tn);
1810163953Srrs	if (stcb->asoc.sctp_autoclose_ticks &&
1811163953Srrs	    sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE)) {
1812163953Srrs		/* Auto close is on */
1813163953Srrs		asoc = &stcb->asoc;
1814163953Srrs		/* pick the time to use */
1815163953Srrs		if (asoc->time_last_rcvd.tv_sec >
1816163953Srrs		    asoc->time_last_sent.tv_sec) {
1817163953Srrs			tim_touse = &asoc->time_last_rcvd;
1818163953Srrs		} else {
1819163953Srrs			tim_touse = &asoc->time_last_sent;
1820163953Srrs		}
1821163953Srrs		/* Now has long enough transpired to autoclose? */
1822163953Srrs		ticks_gone_by = SEC_TO_TICKS(tn.tv_sec - tim_touse->tv_sec);
1823163953Srrs		if ((ticks_gone_by > 0) &&
1824163953Srrs		    (ticks_gone_by >= (int)asoc->sctp_autoclose_ticks)) {
1825163953Srrs			/*
1826163953Srrs			 * autoclose time has hit, call the output routine,
1827163953Srrs			 * which should do nothing just to be SURE we don't
1828163953Srrs			 * have hanging data. We can then safely check the
1829163953Srrs			 * queues and know that we are clear to send
1830163953Srrs			 * shutdown
1831163953Srrs			 */
1832172090Srrs			sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_AUTOCLOSE_TMR, SCTP_SO_NOT_LOCKED);
1833163953Srrs			/* Are we clean? */
1834163953Srrs			if (TAILQ_EMPTY(&asoc->send_queue) &&
1835163953Srrs			    TAILQ_EMPTY(&asoc->sent_queue)) {
1836163953Srrs				/*
1837163953Srrs				 * there is nothing queued to send, so I'm
1838163953Srrs				 * done...
1839163953Srrs				 */
1840166675Srrs				if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) {
1841163953Srrs					/* only send SHUTDOWN 1st time thru */
1842163953Srrs					sctp_send_shutdown(stcb, stcb->asoc.primary_destination);
1843166675Srrs					if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
1844166675Srrs					    (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
1845166675Srrs						SCTP_STAT_DECR_GAUGE32(sctps_currestab);
1846166675Srrs					}
1847171943Srrs					SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
1848172703Srrs					SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
1849163953Srrs					sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
1850163953Srrs					    stcb->sctp_ep, stcb,
1851163953Srrs					    asoc->primary_destination);
1852163953Srrs					sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
1853163953Srrs					    stcb->sctp_ep, stcb,
1854163953Srrs					    asoc->primary_destination);
1855163953Srrs				}
1856163953Srrs			}
1857163953Srrs		} else {
1858163953Srrs			/*
1859163953Srrs			 * No auto close at this time, reset t-o to check
1860163953Srrs			 * later
1861163953Srrs			 */
1862163953Srrs			int tmp;
1863163953Srrs
1864163953Srrs			/* fool the timer startup to use the time left */
1865163953Srrs			tmp = asoc->sctp_autoclose_ticks;
1866163953Srrs			asoc->sctp_autoclose_ticks -= ticks_gone_by;
1867163953Srrs			sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb,
1868163953Srrs			    net);
1869163953Srrs			/* restore the real tick value */
1870163953Srrs			asoc->sctp_autoclose_ticks = tmp;
1871163953Srrs		}
1872163953Srrs	}
1873163953Srrs}
1874