1163953Srrs/*-
2169382Srrs * Copyright (c) 2001-2007, by Cisco Systems, Inc. All rights reserved.
3237896Stuexen * Copyright (c) 2008-2012, by Randall Stewart. All rights reserved.
4237896Stuexen * Copyright (c) 2008-2012, by Michael Tuexen. All rights reserved.
5163953Srrs *
6163953Srrs * Redistribution and use in source and binary forms, with or without
7163953Srrs * modification, are permitted provided that the following conditions are met:
8163953Srrs *
9163953Srrs * a) Redistributions of source code must retain the above copyright notice,
10231038Stuexen *    this list of conditions and the following disclaimer.
11163953Srrs *
12163953Srrs * b) Redistributions in binary form must reproduce the above copyright
13163953Srrs *    notice, this list of conditions and the following disclaimer in
14231038Stuexen *    the documentation and/or other materials provided with the distribution.
15163953Srrs *
16163953Srrs * c) Neither the name of Cisco Systems, Inc. nor the names of its
17163953Srrs *    contributors may be used to endorse or promote products derived
18163953Srrs *    from this software without specific prior written permission.
19163953Srrs *
20163953Srrs * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21163953Srrs * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22163953Srrs * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23163953Srrs * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24163953Srrs * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25163953Srrs * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26163953Srrs * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27163953Srrs * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28163953Srrs * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29163953Srrs * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30163953Srrs * THE POSSIBILITY OF SUCH DAMAGE.
31163953Srrs */
32163953Srrs
33163953Srrs#include <sys/cdefs.h>
34163953Srrs__FBSDID("$FreeBSD$");
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_audit_retranmission_queue(struct sctp_association *asoc)
57163953Srrs{
58163953Srrs	struct sctp_tmit_chunk *chk;
59163953Srrs
60169420Srrs	SCTPDBG(SCTP_DEBUG_TIMER4, "Audit invoked on send queue cnt:%d onqueue:%d\n",
61169420Srrs	    asoc->sent_queue_retran_cnt,
62169420Srrs	    asoc->sent_queue_cnt);
63163953Srrs	asoc->sent_queue_retran_cnt = 0;
64163953Srrs	asoc->sent_queue_cnt = 0;
65163953Srrs	TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
66163953Srrs		if (chk->sent == SCTP_DATAGRAM_RESEND) {
67163953Srrs			sctp_ucount_incr(asoc->sent_queue_retran_cnt);
68163953Srrs		}
69163953Srrs		asoc->sent_queue_cnt++;
70163953Srrs	}
71163953Srrs	TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
72163953Srrs		if (chk->sent == SCTP_DATAGRAM_RESEND) {
73163953Srrs			sctp_ucount_incr(asoc->sent_queue_retran_cnt);
74163953Srrs		}
75163953Srrs	}
76179157Srrs	TAILQ_FOREACH(chk, &asoc->asconf_send_queue, sctp_next) {
77179157Srrs		if (chk->sent == SCTP_DATAGRAM_RESEND) {
78179157Srrs			sctp_ucount_incr(asoc->sent_queue_retran_cnt);
79179157Srrs		}
80179157Srrs	}
81169420Srrs	SCTPDBG(SCTP_DEBUG_TIMER4, "Audit completes retran:%d onqueue:%d\n",
82169420Srrs	    asoc->sent_queue_retran_cnt,
83169420Srrs	    asoc->sent_queue_cnt);
84163953Srrs}
85163953Srrs
86163953Srrsint
87163953Srrssctp_threshold_management(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
88163953Srrs    struct sctp_nets *net, uint16_t threshold)
89163953Srrs{
90163953Srrs	if (net) {
91163953Srrs		net->error_count++;
92169420Srrs		SCTPDBG(SCTP_DEBUG_TIMER4, "Error count for %p now %d thresh:%d\n",
93252927Stuexen		    (void *)net, net->error_count,
94169420Srrs		    net->failure_threshold);
95163953Srrs		if (net->error_count > net->failure_threshold) {
96163953Srrs			/* We had a threshold failure */
97163953Srrs			if (net->dest_state & SCTP_ADDR_REACHABLE) {
98163953Srrs				net->dest_state &= ~SCTP_ADDR_REACHABLE;
99167598Srrs				net->dest_state &= ~SCTP_ADDR_REQ_PRIMARY;
100224641Stuexen				net->dest_state &= ~SCTP_ADDR_PF;
101163953Srrs				sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN,
102237888Stuexen				    stcb, 0,
103172090Srrs				    (void *)net, SCTP_SO_NOT_LOCKED);
104163953Srrs			}
105224641Stuexen		} else if ((net->pf_threshold < net->failure_threshold) &&
106224641Stuexen		    (net->error_count > net->pf_threshold)) {
107224641Stuexen			if (!(net->dest_state & SCTP_ADDR_PF)) {
108224641Stuexen				net->dest_state |= SCTP_ADDR_PF;
109224641Stuexen				net->last_active = sctp_get_tick_count();
110224641Stuexen				sctp_send_hb(stcb, net, SCTP_SO_NOT_LOCKED);
111224641Stuexen				sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_TIMER + SCTP_LOC_3);
112224641Stuexen				sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net);
113224641Stuexen			}
114163953Srrs		}
115163953Srrs	}
116163953Srrs	if (stcb == NULL)
117163953Srrs		return (0);
118163953Srrs
119163953Srrs	if (net) {
120163953Srrs		if ((net->dest_state & SCTP_ADDR_UNCONFIRMED) == 0) {
121179783Srrs			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
122171943Srrs				sctp_misc_ints(SCTP_THRESHOLD_INCR,
123171943Srrs				    stcb->asoc.overall_error_count,
124171943Srrs				    (stcb->asoc.overall_error_count + 1),
125171943Srrs				    SCTP_FROM_SCTP_TIMER,
126171943Srrs				    __LINE__);
127171943Srrs			}
128163953Srrs			stcb->asoc.overall_error_count++;
129163953Srrs		}
130163953Srrs	} else {
131179783Srrs		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
132171943Srrs			sctp_misc_ints(SCTP_THRESHOLD_INCR,
133171943Srrs			    stcb->asoc.overall_error_count,
134171943Srrs			    (stcb->asoc.overall_error_count + 1),
135171943Srrs			    SCTP_FROM_SCTP_TIMER,
136171943Srrs			    __LINE__);
137171943Srrs		}
138163953Srrs		stcb->asoc.overall_error_count++;
139163953Srrs	}
140169420Srrs	SCTPDBG(SCTP_DEBUG_TIMER4, "Overall error count for %p now %d thresh:%u state:%x\n",
141252927Stuexen	    (void *)&stcb->asoc, stcb->asoc.overall_error_count,
142169420Srrs	    (uint32_t) threshold,
143169420Srrs	    ((net == NULL) ? (uint32_t) 0 : (uint32_t) net->dest_state));
144163953Srrs	/*
145163953Srrs	 * We specifically do not do >= to give the assoc one more change
146163953Srrs	 * before we fail it.
147163953Srrs	 */
148163953Srrs	if (stcb->asoc.overall_error_count > threshold) {
149163953Srrs		/* Abort notification sends a ULP notify */
150266181Stuexen		struct mbuf *op_err;
151163953Srrs
152266181Stuexen		op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION,
153266181Stuexen		    "Association error couter exceeded");
154165220Srrs		inp->last_abort_code = SCTP_FROM_SCTP_TIMER + SCTP_LOC_1;
155266181Stuexen		sctp_abort_an_association(inp, stcb, op_err, SCTP_SO_NOT_LOCKED);
156163953Srrs		return (1);
157163953Srrs	}
158163953Srrs	return (0);
159163953Srrs}
160163953Srrs
161214928Stuexen/*
162214928Stuexen * sctp_find_alternate_net() returns a non-NULL pointer as long
163214928Stuexen * the argument net is non-NULL.
164214928Stuexen */
165163953Srrsstruct sctp_nets *
166163953Srrssctp_find_alternate_net(struct sctp_tcb *stcb,
167163953Srrs    struct sctp_nets *net,
168171440Srrs    int mode)
169163953Srrs{
170163953Srrs	/* Find and return an alternate network if possible */
171171440Srrs	struct sctp_nets *alt, *mnet, *min_errors_net = NULL, *max_cwnd_net = NULL;
172163953Srrs	int once;
173163953Srrs
174171440Srrs	/* JRS 5/14/07 - Initialize min_errors to an impossible value. */
175171440Srrs	int min_errors = -1;
176171440Srrs	uint32_t max_cwnd = 0;
177171440Srrs
178163953Srrs	if (stcb->asoc.numnets == 1) {
179163953Srrs		/* No others but net */
180163953Srrs		return (TAILQ_FIRST(&stcb->asoc.nets));
181163953Srrs	}
182171440Srrs	/*
183171440Srrs	 * JRS 5/14/07 - If mode is set to 2, use the CMT PF find alternate
184171440Srrs	 * net algorithm. This algorithm chooses the active destination (not
185171440Srrs	 * in PF state) with the largest cwnd value. If all destinations are
186171440Srrs	 * in PF state, unreachable, or unconfirmed, choose the desination
187171440Srrs	 * that is in PF state with the lowest error count. In case of a
188171440Srrs	 * tie, choose the destination that was most recently active.
189171440Srrs	 */
190171440Srrs	if (mode == 2) {
191163953Srrs		TAILQ_FOREACH(mnet, &stcb->asoc.nets, sctp_next) {
192171440Srrs			/*
193171440Srrs			 * JRS 5/14/07 - If the destination is unreachable
194171440Srrs			 * or unconfirmed, skip it.
195171440Srrs			 */
196163953Srrs			if (((mnet->dest_state & SCTP_ADDR_REACHABLE) != SCTP_ADDR_REACHABLE) ||
197171440Srrs			    (mnet->dest_state & SCTP_ADDR_UNCONFIRMED)) {
198171440Srrs				continue;
199171440Srrs			}
200171440Srrs			/*
201171440Srrs			 * JRS 5/14/07 -  If the destination is reachable
202171440Srrs			 * but in PF state, compare the error count of the
203171440Srrs			 * destination to the minimum error count seen thus
204171440Srrs			 * far. Store the destination with the lower error
205171440Srrs			 * count.  If the error counts are equal, store the
206171440Srrs			 * destination that was most recently active.
207171440Srrs			 */
208171440Srrs			if (mnet->dest_state & SCTP_ADDR_PF) {
209171440Srrs				/*
210171440Srrs				 * JRS 5/14/07 - If the destination under
211171440Srrs				 * consideration is the current destination,
212171440Srrs				 * work as if the error count is one higher.
213171440Srrs				 * The actual error count will not be
214171440Srrs				 * incremented until later in the t3
215171440Srrs				 * handler.
216171440Srrs				 */
217171440Srrs				if (mnet == net) {
218171440Srrs					if (min_errors == -1) {
219171440Srrs						min_errors = mnet->error_count + 1;
220171440Srrs						min_errors_net = mnet;
221171440Srrs					} else if (mnet->error_count + 1 < min_errors) {
222171440Srrs						min_errors = mnet->error_count + 1;
223171440Srrs						min_errors_net = mnet;
224171440Srrs					} else if (mnet->error_count + 1 == min_errors
225171440Srrs					    && mnet->last_active > min_errors_net->last_active) {
226171440Srrs						min_errors_net = mnet;
227171440Srrs						min_errors = mnet->error_count + 1;
228171440Srrs					}
229171440Srrs					continue;
230171440Srrs				} else {
231171440Srrs					if (min_errors == -1) {
232171440Srrs						min_errors = mnet->error_count;
233171440Srrs						min_errors_net = mnet;
234171440Srrs					} else if (mnet->error_count < min_errors) {
235171440Srrs						min_errors = mnet->error_count;
236171440Srrs						min_errors_net = mnet;
237171440Srrs					} else if (mnet->error_count == min_errors
238171440Srrs					    && mnet->last_active > min_errors_net->last_active) {
239171440Srrs						min_errors_net = mnet;
240171440Srrs						min_errors = mnet->error_count;
241171440Srrs					}
242171440Srrs					continue;
243171440Srrs				}
244171440Srrs			}
245171440Srrs			/*
246171440Srrs			 * JRS 5/14/07 - If the destination is reachable and
247171440Srrs			 * not in PF state, compare the cwnd of the
248171440Srrs			 * destination to the highest cwnd seen thus far.
249171440Srrs			 * Store the destination with the higher cwnd value.
250171440Srrs			 * If the cwnd values are equal, randomly choose one
251171440Srrs			 * of the two destinations.
252171440Srrs			 */
253171440Srrs			if (max_cwnd < mnet->cwnd) {
254171440Srrs				max_cwnd_net = mnet;
255171440Srrs				max_cwnd = mnet->cwnd;
256171440Srrs			} else if (max_cwnd == mnet->cwnd) {
257171440Srrs				uint32_t rndval;
258171440Srrs				uint8_t this_random;
259171440Srrs
260171440Srrs				if (stcb->asoc.hb_random_idx > 3) {
261171440Srrs					rndval = sctp_select_initial_TSN(&stcb->sctp_ep->sctp_ep);
262171440Srrs					memcpy(stcb->asoc.hb_random_values, &rndval, sizeof(stcb->asoc.hb_random_values));
263171440Srrs					this_random = stcb->asoc.hb_random_values[0];
264171440Srrs					stcb->asoc.hb_random_idx++;
265171440Srrs					stcb->asoc.hb_ect_randombit = 0;
266171440Srrs				} else {
267171440Srrs					this_random = stcb->asoc.hb_random_values[stcb->asoc.hb_random_idx];
268171440Srrs					stcb->asoc.hb_random_idx++;
269171440Srrs					stcb->asoc.hb_ect_randombit = 0;
270171440Srrs				}
271171440Srrs				if (this_random % 2 == 1) {
272171440Srrs					max_cwnd_net = mnet;
273180387Srrs					max_cwnd = mnet->cwnd;	/* Useless? */
274171440Srrs				}
275171440Srrs			}
276171440Srrs		}
277171440Srrs		if (max_cwnd_net == NULL) {
278171440Srrs			if (min_errors_net == NULL) {
279171440Srrs				return (net);
280171440Srrs			}
281171440Srrs			return (min_errors_net);
282171440Srrs		} else {
283171440Srrs			return (max_cwnd_net);
284171440Srrs		}
285171440Srrs	}
286171440Srrs	/*
287171440Srrs	 * JRS 5/14/07 - If mode is set to 1, use the CMT policy for
288171440Srrs	 * choosing an alternate net.
289171440Srrs	 */
290171440Srrs	else if (mode == 1) {
291171440Srrs		TAILQ_FOREACH(mnet, &stcb->asoc.nets, sctp_next) {
292171440Srrs			if (((mnet->dest_state & SCTP_ADDR_REACHABLE) != SCTP_ADDR_REACHABLE) ||
293214928Stuexen			    (mnet->dest_state & SCTP_ADDR_UNCONFIRMED)) {
294163953Srrs				/*
295163953Srrs				 * will skip ones that are not-reachable or
296163953Srrs				 * unconfirmed
297163953Srrs				 */
298163953Srrs				continue;
299163953Srrs			}
300171440Srrs			if (max_cwnd < mnet->cwnd) {
301171440Srrs				max_cwnd_net = mnet;
302171440Srrs				max_cwnd = mnet->cwnd;
303171440Srrs			} else if (max_cwnd == mnet->cwnd) {
304163953Srrs				uint32_t rndval;
305163953Srrs				uint8_t this_random;
306163953Srrs
307163953Srrs				if (stcb->asoc.hb_random_idx > 3) {
308163953Srrs					rndval = sctp_select_initial_TSN(&stcb->sctp_ep->sctp_ep);
309163953Srrs					memcpy(stcb->asoc.hb_random_values, &rndval,
310163953Srrs					    sizeof(stcb->asoc.hb_random_values));
311163953Srrs					this_random = stcb->asoc.hb_random_values[0];
312163953Srrs					stcb->asoc.hb_random_idx = 0;
313163953Srrs					stcb->asoc.hb_ect_randombit = 0;
314163953Srrs				} else {
315163953Srrs					this_random = stcb->asoc.hb_random_values[stcb->asoc.hb_random_idx];
316163953Srrs					stcb->asoc.hb_random_idx++;
317163953Srrs					stcb->asoc.hb_ect_randombit = 0;
318163953Srrs				}
319163953Srrs				if (this_random % 2) {
320171440Srrs					max_cwnd_net = mnet;
321171440Srrs					max_cwnd = mnet->cwnd;
322163953Srrs				}
323163953Srrs			}
324163953Srrs		}
325171440Srrs		if (max_cwnd_net) {
326171440Srrs			return (max_cwnd_net);
327163953Srrs		}
328163953Srrs	}
329163953Srrs	mnet = net;
330163953Srrs	once = 0;
331163953Srrs
332163953Srrs	if (mnet == NULL) {
333163953Srrs		mnet = TAILQ_FIRST(&stcb->asoc.nets);
334212225Srrs		if (mnet == NULL) {
335212225Srrs			return (NULL);
336212225Srrs		}
337163953Srrs	}
338163953Srrs	do {
339163953Srrs		alt = TAILQ_NEXT(mnet, sctp_next);
340163953Srrs		if (alt == NULL) {
341163953Srrs			once++;
342163953Srrs			if (once > 1) {
343163953Srrs				break;
344163953Srrs			}
345163953Srrs			alt = TAILQ_FIRST(&stcb->asoc.nets);
346212225Srrs			if (alt == NULL) {
347212225Srrs				return (NULL);
348212225Srrs			}
349163953Srrs		}
350163953Srrs		if (alt->ro.ro_rt == NULL) {
351167598Srrs			if (alt->ro._s_addr) {
352167598Srrs				sctp_free_ifa(alt->ro._s_addr);
353167598Srrs				alt->ro._s_addr = NULL;
354167598Srrs			}
355163953Srrs			alt->src_addr_selected = 0;
356163953Srrs		}
357214928Stuexen		/* sa_ignore NO_NULL_CHK */
358214928Stuexen		if (((alt->dest_state & SCTP_ADDR_REACHABLE) == SCTP_ADDR_REACHABLE) &&
359163953Srrs		    (alt->ro.ro_rt != NULL) &&
360214928Stuexen		    (!(alt->dest_state & SCTP_ADDR_UNCONFIRMED))) {
361163953Srrs			/* Found a reachable address */
362163953Srrs			break;
363163953Srrs		}
364163953Srrs		mnet = alt;
365163953Srrs	} while (alt != NULL);
366163953Srrs
367163953Srrs	if (alt == NULL) {
368163953Srrs		/* Case where NO insv network exists (dormant state) */
369163953Srrs		/* we rotate destinations */
370163953Srrs		once = 0;
371163953Srrs		mnet = net;
372163953Srrs		do {
373212225Srrs			if (mnet == NULL) {
374212225Srrs				return (TAILQ_FIRST(&stcb->asoc.nets));
375212225Srrs			}
376163953Srrs			alt = TAILQ_NEXT(mnet, sctp_next);
377163953Srrs			if (alt == NULL) {
378163953Srrs				once++;
379163953Srrs				if (once > 1) {
380163953Srrs					break;
381163953Srrs				}
382163953Srrs				alt = TAILQ_FIRST(&stcb->asoc.nets);
383163953Srrs			}
384169655Srrs			/* sa_ignore NO_NULL_CHK */
385163953Srrs			if ((!(alt->dest_state & SCTP_ADDR_UNCONFIRMED)) &&
386163953Srrs			    (alt != net)) {
387163953Srrs				/* Found an alternate address */
388163953Srrs				break;
389163953Srrs			}
390163953Srrs			mnet = alt;
391163953Srrs		} while (alt != NULL);
392163953Srrs	}
393163953Srrs	if (alt == NULL) {
394163953Srrs		return (net);
395163953Srrs	}
396163953Srrs	return (alt);
397163953Srrs}
398163953Srrs
399163953Srrsstatic void
400163953Srrssctp_backoff_on_timeout(struct sctp_tcb *stcb,
401163953Srrs    struct sctp_nets *net,
402163953Srrs    int win_probe,
403210599Srrs    int num_marked, int num_abandoned)
404163953Srrs{
405170642Srrs	if (net->RTO == 0) {
406170642Srrs		net->RTO = stcb->asoc.minrto;
407170642Srrs	}
408163953Srrs	net->RTO <<= 1;
409163953Srrs	if (net->RTO > stcb->asoc.maxrto) {
410163953Srrs		net->RTO = stcb->asoc.maxrto;
411163953Srrs	}
412210599Srrs	if ((win_probe == 0) && (num_marked || num_abandoned)) {
413163953Srrs		/* We don't apply penalty to window probe scenarios */
414171440Srrs		/* JRS - Use the congestion control given in the CC module */
415171440Srrs		stcb->asoc.cc_functions.sctp_cwnd_update_after_timeout(stcb, net);
416163953Srrs	}
417163953Srrs}
418163953Srrs
419184333Srrs#ifndef INVARIANTS
420184333Srrsstatic void
421182367Srrssctp_recover_sent_list(struct sctp_tcb *stcb)
422182367Srrs{
423216822Stuexen	struct sctp_tmit_chunk *chk, *nchk;
424182367Srrs	struct sctp_association *asoc;
425182367Srrs
426182367Srrs	asoc = &stcb->asoc;
427216822Stuexen	TAILQ_FOREACH_SAFE(chk, &asoc->sent_queue, sctp_next, nchk) {
428216825Stuexen		if (SCTP_TSN_GE(asoc->last_acked_seq, chk->rec.data.TSN_seq)) {
429182367Srrs			SCTP_PRINTF("Found chk:%p tsn:%x <= last_acked_seq:%x\n",
430252927Stuexen			    (void *)chk, chk->rec.data.TSN_seq, asoc->last_acked_seq);
431252943Stuexen			if (chk->sent != SCTP_DATAGRAM_NR_ACKED) {
432252942Stuexen				if (asoc->strmout[chk->rec.data.stream_number].chunks_on_queues > 0) {
433252942Stuexen					asoc->strmout[chk->rec.data.stream_number].chunks_on_queues--;
434252942Stuexen				}
435252942Stuexen			}
436182367Srrs			TAILQ_REMOVE(&asoc->sent_queue, chk, sctp_next);
437265963Stuexen			if (PR_SCTP_ENABLED(chk->flags)) {
438182367Srrs				if (asoc->pr_sctp_cnt != 0)
439182367Srrs					asoc->pr_sctp_cnt--;
440182367Srrs			}
441182367Srrs			if (chk->data) {
442182367Srrs				/* sa_ignore NO_NULL_CHK */
443182367Srrs				sctp_free_bufspace(stcb, asoc, chk, 1);
444182367Srrs				sctp_m_freem(chk->data);
445216822Stuexen				chk->data = NULL;
446196260Stuexen				if (asoc->peer_supports_prsctp && PR_SCTP_BUF_ENABLED(chk->flags)) {
447182367Srrs					asoc->sent_queue_cnt_removeable--;
448182367Srrs				}
449182367Srrs			}
450182367Srrs			asoc->sent_queue_cnt--;
451221627Stuexen			sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
452182367Srrs		}
453182367Srrs	}
454182367Srrs	SCTP_PRINTF("after recover order is as follows\n");
455216822Stuexen	TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
456252927Stuexen		SCTP_PRINTF("chk:%p TSN:%x\n", (void *)chk, chk->rec.data.TSN_seq);
457182367Srrs	}
458182367Srrs}
459182367Srrs
460184333Srrs#endif
461184333Srrs
462163953Srrsstatic int
463163953Srrssctp_mark_all_for_resend(struct sctp_tcb *stcb,
464163953Srrs    struct sctp_nets *net,
465163953Srrs    struct sctp_nets *alt,
466163953Srrs    int window_probe,
467210599Srrs    int *num_marked,
468210599Srrs    int *num_abandoned)
469163953Srrs{
470163953Srrs
471163953Srrs	/*
472163953Srrs	 * Mark all chunks (well not all) that were sent to *net for
473163953Srrs	 * retransmission. Move them to alt for there destination as well...
474163953Srrs	 * We only mark chunks that have been outstanding long enough to
475163953Srrs	 * have received feed-back.
476163953Srrs	 */
477216822Stuexen	struct sctp_tmit_chunk *chk, *nchk;
478163953Srrs	struct sctp_nets *lnets;
479163953Srrs	struct timeval now, min_wait, tv;
480219014Stuexen	int cur_rto;
481210599Srrs	int cnt_abandoned;
482168709Srrs	int audit_tf, num_mk, fir;
483163953Srrs	unsigned int cnt_mk;
484168709Srrs	uint32_t orig_flight, orig_tf;
485163953Srrs	uint32_t tsnlast, tsnfirst;
486182367Srrs	int recovery_cnt = 0;
487163953Srrs
488171440Srrs
489163953Srrs	/* none in flight now */
490163953Srrs	audit_tf = 0;
491163953Srrs	fir = 0;
492163953Srrs	/*
493163953Srrs	 * figure out how long a data chunk must be pending before we can
494163953Srrs	 * mark it ..
495163953Srrs	 */
496169378Srrs	(void)SCTP_GETTIME_TIMEVAL(&now);
497163953Srrs	/* get cur rto in micro-seconds */
498219014Stuexen	cur_rto = (net->lastsa >> SCTP_RTT_SHIFT) + net->lastsv;
499219014Stuexen	cur_rto *= 1000;
500224641Stuexen	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
501219014Stuexen		sctp_log_fr(cur_rto,
502170744Srrs		    stcb->asoc.peers_rwnd,
503170744Srrs		    window_probe,
504170744Srrs		    SCTP_FR_T3_MARK_TIME);
505224641Stuexen		sctp_log_fr(net->flight_size, 0, 0, SCTP_FR_CWND_REPORT);
506170744Srrs		sctp_log_fr(net->flight_size, net->cwnd, stcb->asoc.total_flight, SCTP_FR_CWND_REPORT);
507170744Srrs	}
508219014Stuexen	tv.tv_sec = cur_rto / 1000000;
509219014Stuexen	tv.tv_usec = cur_rto % 1000000;
510163953Srrs	min_wait = now;
511163953Srrs	timevalsub(&min_wait, &tv);
512163953Srrs	if (min_wait.tv_sec < 0 || min_wait.tv_usec < 0) {
513163953Srrs		/*
514163953Srrs		 * if we hit here, we don't have enough seconds on the clock
515163953Srrs		 * to account for the RTO. We just let the lower seconds be
516163953Srrs		 * the bounds and don't worry about it. This may mean we
517163953Srrs		 * will mark a lot more than we should.
518163953Srrs		 */
519163953Srrs		min_wait.tv_sec = min_wait.tv_usec = 0;
520163953Srrs	}
521224641Stuexen	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
522219014Stuexen		sctp_log_fr(cur_rto, now.tv_sec, now.tv_usec, SCTP_FR_T3_MARK_TIME);
523170744Srrs		sctp_log_fr(0, min_wait.tv_sec, min_wait.tv_usec, SCTP_FR_T3_MARK_TIME);
524170744Srrs	}
525163953Srrs	/*
526163953Srrs	 * Our rwnd will be incorrect here since we are not adding back the
527163953Srrs	 * cnt * mbuf but we will fix that down below.
528163953Srrs	 */
529163953Srrs	orig_flight = net->flight_size;
530168709Srrs	orig_tf = stcb->asoc.total_flight;
531168709Srrs
532163953Srrs	net->fast_retran_ip = 0;
533163953Srrs	/* Now on to each chunk */
534210599Srrs	cnt_abandoned = 0;
535163953Srrs	num_mk = cnt_mk = 0;
536163953Srrs	tsnfirst = tsnlast = 0;
537184333Srrs#ifndef INVARIANTS
538182367Srrsstart_again:
539184333Srrs#endif
540216822Stuexen	TAILQ_FOREACH_SAFE(chk, &stcb->asoc.sent_queue, sctp_next, nchk) {
541216825Stuexen		if (SCTP_TSN_GE(stcb->asoc.last_acked_seq, chk->rec.data.TSN_seq)) {
542163953Srrs			/* Strange case our list got out of order? */
543266052Stuexen			SCTP_PRINTF("Our list is out of order? last_acked:%x chk:%x\n",
544182367Srrs			    (unsigned int)stcb->asoc.last_acked_seq, (unsigned int)chk->rec.data.TSN_seq);
545182367Srrs			recovery_cnt++;
546182367Srrs#ifdef INVARIANTS
547182367Srrs			panic("last acked >= chk on sent-Q");
548182367Srrs#else
549182367Srrs			SCTP_PRINTF("Recover attempts a restart cnt:%d\n", recovery_cnt);
550182367Srrs			sctp_recover_sent_list(stcb);
551182367Srrs			if (recovery_cnt < 10) {
552182367Srrs				goto start_again;
553182367Srrs			} else {
554182367Srrs				SCTP_PRINTF("Recovery fails %d times??\n", recovery_cnt);
555182367Srrs			}
556182367Srrs#endif
557163953Srrs		}
558163953Srrs		if ((chk->whoTo == net) && (chk->sent < SCTP_DATAGRAM_ACKED)) {
559163953Srrs			/*
560163953Srrs			 * found one to mark: If it is less than
561163953Srrs			 * DATAGRAM_ACKED it MUST not be a skipped or marked
562163953Srrs			 * TSN but instead one that is either already set
563163953Srrs			 * for retransmission OR one that needs
564163953Srrs			 * retransmission.
565163953Srrs			 */
566163953Srrs
567163953Srrs			/* validate its been outstanding long enough */
568224641Stuexen			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
569170744Srrs				sctp_log_fr(chk->rec.data.TSN_seq,
570170744Srrs				    chk->sent_rcv_time.tv_sec,
571170744Srrs				    chk->sent_rcv_time.tv_usec,
572170744Srrs				    SCTP_FR_T3_MARK_TIME);
573170744Srrs			}
574163953Srrs			if ((chk->sent_rcv_time.tv_sec > min_wait.tv_sec) && (window_probe == 0)) {
575163953Srrs				/*
576163953Srrs				 * we have reached a chunk that was sent
577163953Srrs				 * some seconds past our min.. forget it we
578163953Srrs				 * will find no more to send.
579163953Srrs				 */
580224641Stuexen				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
581170744Srrs					sctp_log_fr(0,
582170744Srrs					    chk->sent_rcv_time.tv_sec,
583170744Srrs					    chk->sent_rcv_time.tv_usec,
584170744Srrs					    SCTP_FR_T3_STOPPED);
585170744Srrs				}
586163953Srrs				continue;
587163953Srrs			} else if ((chk->sent_rcv_time.tv_sec == min_wait.tv_sec) &&
588163953Srrs			    (window_probe == 0)) {
589163953Srrs				/*
590163953Srrs				 * we must look at the micro seconds to
591163953Srrs				 * know.
592163953Srrs				 */
593163953Srrs				if (chk->sent_rcv_time.tv_usec >= min_wait.tv_usec) {
594163953Srrs					/*
595163953Srrs					 * ok it was sent after our boundary
596163953Srrs					 * time.
597163953Srrs					 */
598163953Srrs					continue;
599163953Srrs				}
600163953Srrs			}
601196260Stuexen			if (stcb->asoc.peer_supports_prsctp && PR_SCTP_TTL_ENABLED(chk->flags)) {
602163953Srrs				/* Is it expired? */
603212801Stuexen				if (timevalcmp(&now, &chk->rec.data.timetodrop, >)) {
604163953Srrs					/* Yes so drop it */
605163953Srrs					if (chk->data) {
606169420Srrs						(void)sctp_release_pr_sctp_chunk(stcb,
607163953Srrs						    chk,
608237889Stuexen						    1,
609189790Srrs						    SCTP_SO_NOT_LOCKED);
610210599Srrs						cnt_abandoned++;
611163953Srrs					}
612185694Srrs					continue;
613163953Srrs				}
614163953Srrs			}
615196260Stuexen			if (stcb->asoc.peer_supports_prsctp && PR_SCTP_RTX_ENABLED(chk->flags)) {
616163953Srrs				/* Has it been retransmitted tv_sec times? */
617163953Srrs				if (chk->snd_count > chk->rec.data.timetodrop.tv_sec) {
618163953Srrs					if (chk->data) {
619169420Srrs						(void)sctp_release_pr_sctp_chunk(stcb,
620163953Srrs						    chk,
621237889Stuexen						    1,
622189790Srrs						    SCTP_SO_NOT_LOCKED);
623210599Srrs						cnt_abandoned++;
624163953Srrs					}
625185694Srrs					continue;
626163953Srrs				}
627163953Srrs			}
628168709Srrs			if (chk->sent < SCTP_DATAGRAM_RESEND) {
629163953Srrs				sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
630163953Srrs				num_mk++;
631163953Srrs				if (fir == 0) {
632163953Srrs					fir = 1;
633163953Srrs					tsnfirst = chk->rec.data.TSN_seq;
634163953Srrs				}
635163953Srrs				tsnlast = chk->rec.data.TSN_seq;
636224641Stuexen				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
637170744Srrs					sctp_log_fr(chk->rec.data.TSN_seq, chk->snd_count,
638170744Srrs					    0, SCTP_FR_T3_MARKED);
639170744Srrs				}
640168709Srrs				if (chk->rec.data.chunk_was_revoked) {
641168709Srrs					/* deflate the cwnd */
642168709Srrs					chk->whoTo->cwnd -= chk->book_size;
643168709Srrs					chk->rec.data.chunk_was_revoked = 0;
644168709Srrs				}
645168709Srrs				net->marked_retrans++;
646168709Srrs				stcb->asoc.marked_retrans++;
647179783Srrs				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
648170744Srrs					sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_RSND_TO,
649170744Srrs					    chk->whoTo->flight_size,
650170744Srrs					    chk->book_size,
651170744Srrs					    (uintptr_t) chk->whoTo,
652170744Srrs					    chk->rec.data.TSN_seq);
653170744Srrs				}
654168709Srrs				sctp_flight_size_decrease(chk);
655168709Srrs				sctp_total_flight_decrease(stcb, chk);
656168709Srrs				stcb->asoc.peers_rwnd += chk->send_size;
657179783Srrs				stcb->asoc.peers_rwnd += SCTP_BASE_SYSCTL(sctp_peer_chunk_oh);
658163953Srrs			}
659163953Srrs			chk->sent = SCTP_DATAGRAM_RESEND;
660163953Srrs			SCTP_STAT_INCR(sctps_markedretrans);
661165220Srrs
662163953Srrs			/* reset the TSN for striking and other FR stuff */
663163953Srrs			chk->rec.data.doing_fast_retransmit = 0;
664163953Srrs			/* Clear any time so NO RTT is being done */
665219397Srrs
666219397Srrs			if (chk->do_rtt) {
667219397Srrs				if (chk->whoTo->rto_needed == 0) {
668219397Srrs					chk->whoTo->rto_needed = 1;
669219397Srrs				}
670219397Srrs			}
671163953Srrs			chk->do_rtt = 0;
672163953Srrs			if (alt != net) {
673163953Srrs				sctp_free_remote_addr(chk->whoTo);
674163953Srrs				chk->no_fr_allowed = 1;
675163953Srrs				chk->whoTo = alt;
676163953Srrs				atomic_add_int(&alt->ref_count, 1);
677163953Srrs			} else {
678163953Srrs				chk->no_fr_allowed = 0;
679163953Srrs				if (TAILQ_EMPTY(&stcb->asoc.send_queue)) {
680163953Srrs					chk->rec.data.fast_retran_tsn = stcb->asoc.sending_seq;
681163953Srrs				} else {
682163953Srrs					chk->rec.data.fast_retran_tsn = (TAILQ_FIRST(&stcb->asoc.send_queue))->rec.data.TSN_seq;
683163953Srrs				}
684163953Srrs			}
685170181Srrs			/*
686170181Srrs			 * CMT: Do not allow FRs on retransmitted TSNs.
687170181Srrs			 */
688216669Stuexen			if (stcb->asoc.sctp_cmt_on_off > 0) {
689163953Srrs				chk->no_fr_allowed = 1;
690163953Srrs			}
691210599Srrs#ifdef THIS_SHOULD_NOT_BE_DONE
692163953Srrs		} else if (chk->sent == SCTP_DATAGRAM_ACKED) {
693163953Srrs			/* remember highest acked one */
694163953Srrs			could_be_sent = chk;
695210599Srrs#endif
696163953Srrs		}
697163953Srrs		if (chk->sent == SCTP_DATAGRAM_RESEND) {
698163953Srrs			cnt_mk++;
699163953Srrs		}
700163953Srrs	}
701168709Srrs	if ((orig_flight - net->flight_size) != (orig_tf - stcb->asoc.total_flight)) {
702168709Srrs		/* we did not subtract the same things? */
703168709Srrs		audit_tf = 1;
704168709Srrs	}
705224641Stuexen	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
706170744Srrs		sctp_log_fr(tsnfirst, tsnlast, num_mk, SCTP_FR_T3_TIMEOUT);
707170744Srrs	}
708163953Srrs#ifdef SCTP_DEBUG
709169420Srrs	if (num_mk) {
710169420Srrs		SCTPDBG(SCTP_DEBUG_TIMER1, "LAST TSN marked was %x\n",
711169420Srrs		    tsnlast);
712169420Srrs		SCTPDBG(SCTP_DEBUG_TIMER1, "Num marked for retransmission was %d peer-rwd:%ld\n",
713169420Srrs		    num_mk, (u_long)stcb->asoc.peers_rwnd);
714169420Srrs		SCTPDBG(SCTP_DEBUG_TIMER1, "LAST TSN marked was %x\n",
715169420Srrs		    tsnlast);
716169420Srrs		SCTPDBG(SCTP_DEBUG_TIMER1, "Num marked for retransmission was %d peer-rwd:%d\n",
717169420Srrs		    num_mk,
718169420Srrs		    (int)stcb->asoc.peers_rwnd);
719163953Srrs	}
720163953Srrs#endif
721163953Srrs	*num_marked = num_mk;
722210599Srrs	*num_abandoned = cnt_abandoned;
723210493Srrs	/*
724210493Srrs	 * Now check for a ECN Echo that may be stranded And include the
725210493Srrs	 * cnt_mk'd to have all resends in the control queue.
726210493Srrs	 */
727210493Srrs	TAILQ_FOREACH(chk, &stcb->asoc.control_send_queue, sctp_next) {
728210493Srrs		if (chk->sent == SCTP_DATAGRAM_RESEND) {
729210493Srrs			cnt_mk++;
730210493Srrs		}
731210493Srrs		if ((chk->whoTo == net) &&
732210493Srrs		    (chk->rec.chunk_id.id == SCTP_ECN_ECHO)) {
733210493Srrs			sctp_free_remote_addr(chk->whoTo);
734210493Srrs			chk->whoTo = alt;
735210493Srrs			if (chk->sent != SCTP_DATAGRAM_RESEND) {
736210493Srrs				chk->sent = SCTP_DATAGRAM_RESEND;
737210493Srrs				sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
738210493Srrs				cnt_mk++;
739210493Srrs			}
740210493Srrs			atomic_add_int(&alt->ref_count, 1);
741210493Srrs		}
742210493Srrs	}
743210599Srrs#ifdef THIS_SHOULD_NOT_BE_DONE
744163953Srrs	if ((stcb->asoc.sent_queue_retran_cnt == 0) && (could_be_sent)) {
745163953Srrs		/* fix it so we retransmit the highest acked anyway */
746163953Srrs		sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
747163953Srrs		cnt_mk++;
748163953Srrs		could_be_sent->sent = SCTP_DATAGRAM_RESEND;
749163953Srrs	}
750210599Srrs#endif
751163953Srrs	if (stcb->asoc.sent_queue_retran_cnt != cnt_mk) {
752165220Srrs#ifdef INVARIANTS
753171477Srrs		SCTP_PRINTF("Local Audit says there are %d for retran asoc cnt:%d we marked:%d this time\n",
754171477Srrs		    cnt_mk, stcb->asoc.sent_queue_retran_cnt, num_mk);
755163953Srrs#endif
756163953Srrs#ifndef SCTP_AUDITING_ENABLED
757163953Srrs		stcb->asoc.sent_queue_retran_cnt = cnt_mk;
758163953Srrs#endif
759163953Srrs	}
760163953Srrs	if (audit_tf) {
761169420Srrs		SCTPDBG(SCTP_DEBUG_TIMER4,
762169420Srrs		    "Audit total flight due to negative value net:%p\n",
763252927Stuexen		    (void *)net);
764163953Srrs		stcb->asoc.total_flight = 0;
765163953Srrs		stcb->asoc.total_flight_count = 0;
766163953Srrs		/* Clear all networks flight size */
767163953Srrs		TAILQ_FOREACH(lnets, &stcb->asoc.nets, sctp_next) {
768163953Srrs			lnets->flight_size = 0;
769169420Srrs			SCTPDBG(SCTP_DEBUG_TIMER4,
770169420Srrs			    "Net:%p c-f cwnd:%d ssthresh:%d\n",
771252927Stuexen			    (void *)lnets, lnets->cwnd, lnets->ssthresh);
772163953Srrs		}
773163953Srrs		TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
774163953Srrs			if (chk->sent < SCTP_DATAGRAM_RESEND) {
775179783Srrs				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
776170744Srrs					sctp_misc_ints(SCTP_FLIGHT_LOG_UP,
777170744Srrs					    chk->whoTo->flight_size,
778170744Srrs					    chk->book_size,
779170744Srrs					    (uintptr_t) chk->whoTo,
780170744Srrs					    chk->rec.data.TSN_seq);
781170744Srrs				}
782168709Srrs				sctp_flight_size_increase(chk);
783168709Srrs				sctp_total_flight_increase(stcb, chk);
784163953Srrs			}
785163953Srrs		}
786163953Srrs	}
787163953Srrs	/* We return 1 if we only have a window probe outstanding */
788163953Srrs	return (0);
789163953Srrs}
790163953Srrs
791163953Srrs
792163953Srrsint
793163953Srrssctp_t3rxt_timer(struct sctp_inpcb *inp,
794163953Srrs    struct sctp_tcb *stcb,
795163953Srrs    struct sctp_nets *net)
796163953Srrs{
797163953Srrs	struct sctp_nets *alt;
798210599Srrs	int win_probe, num_mk, num_abandoned;
799163953Srrs
800179783Srrs	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
801170744Srrs		sctp_log_fr(0, 0, 0, SCTP_FR_T3_TIMEOUT);
802170744Srrs	}
803179783Srrs	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
804163953Srrs		struct sctp_nets *lnet;
805163953Srrs
806163953Srrs		TAILQ_FOREACH(lnet, &stcb->asoc.nets, sctp_next) {
807163953Srrs			if (net == lnet) {
808163953Srrs				sctp_log_cwnd(stcb, lnet, 1, SCTP_CWND_LOG_FROM_T3);
809163953Srrs			} else {
810163953Srrs				sctp_log_cwnd(stcb, lnet, 0, SCTP_CWND_LOG_FROM_T3);
811163953Srrs			}
812163953Srrs		}
813163953Srrs	}
814163953Srrs	/* Find an alternate and mark those for retransmission */
815163953Srrs	if ((stcb->asoc.peers_rwnd == 0) &&
816163953Srrs	    (stcb->asoc.total_flight < net->mtu)) {
817163953Srrs		SCTP_STAT_INCR(sctps_timowindowprobe);
818163953Srrs		win_probe = 1;
819163953Srrs	} else {
820163953Srrs		win_probe = 0;
821163953Srrs	}
822168709Srrs
823163953Srrs	if (win_probe == 0) {
824163953Srrs		/* We don't do normal threshold management on window probes */
825163953Srrs		if (sctp_threshold_management(inp, stcb, net,
826163953Srrs		    stcb->asoc.max_send_times)) {
827163953Srrs			/* Association was destroyed */
828163953Srrs			return (1);
829163953Srrs		} else {
830163953Srrs			if (net != stcb->asoc.primary_destination) {
831163953Srrs				/* send a immediate HB if our RTO is stale */
832163953Srrs				struct timeval now;
833163953Srrs				unsigned int ms_goneby;
834163953Srrs
835169378Srrs				(void)SCTP_GETTIME_TIMEVAL(&now);
836163953Srrs				if (net->last_sent_time.tv_sec) {
837163953Srrs					ms_goneby = (now.tv_sec - net->last_sent_time.tv_sec) * 1000;
838163953Srrs				} else {
839163953Srrs					ms_goneby = 0;
840163953Srrs				}
841224641Stuexen				if ((net->dest_state & SCTP_ADDR_PF) == 0) {
842224641Stuexen					if ((ms_goneby > net->RTO) || (net->RTO == 0)) {
843185694Srrs						/*
844224641Stuexen						 * no recent feed back in an
845224641Stuexen						 * RTO or more, request a
846224641Stuexen						 * RTT update
847185694Srrs						 */
848224641Stuexen						sctp_send_hb(stcb, net, SCTP_SO_NOT_LOCKED);
849224641Stuexen					}
850163953Srrs				}
851163953Srrs			}
852163953Srrs		}
853163953Srrs	} else {
854163953Srrs		/*
855163953Srrs		 * For a window probe we don't penalize the net's but only
856163953Srrs		 * the association. This may fail it if SACKs are not coming
857163953Srrs		 * back. If sack's are coming with rwnd locked at 0, we will
858163953Srrs		 * continue to hold things waiting for rwnd to raise
859163953Srrs		 */
860163953Srrs		if (sctp_threshold_management(inp, stcb, NULL,
861163953Srrs		    stcb->asoc.max_send_times)) {
862163953Srrs			/* Association was destroyed */
863163953Srrs			return (1);
864163953Srrs		}
865163953Srrs	}
866224641Stuexen	if (stcb->asoc.sctp_cmt_on_off > 0) {
867224641Stuexen		if (net->pf_threshold < net->failure_threshold) {
868224641Stuexen			alt = sctp_find_alternate_net(stcb, net, 2);
869224641Stuexen		} else {
870224641Stuexen			/*
871224641Stuexen			 * CMT: Using RTX_SSTHRESH policy for CMT. If CMT is
872224641Stuexen			 * being used, then pick dest with largest ssthresh
873224641Stuexen			 * for any retransmission.
874224641Stuexen			 */
875224641Stuexen			alt = sctp_find_alternate_net(stcb, net, 1);
876224641Stuexen			/*
877224641Stuexen			 * CUCv2: If a different dest is picked for the
878224641Stuexen			 * retransmission, then new (rtx-)pseudo_cumack
879224641Stuexen			 * needs to be tracked for orig dest. Let CUCv2
880224641Stuexen			 * track new (rtx-) pseudo-cumack always.
881224641Stuexen			 */
882224641Stuexen			net->find_pseudo_cumack = 1;
883224641Stuexen			net->find_rtx_pseudo_cumack = 1;
884224641Stuexen		}
885224641Stuexen	} else {
886224641Stuexen		alt = sctp_find_alternate_net(stcb, net, 0);
887224641Stuexen	}
888224641Stuexen
889224641Stuexen	num_mk = 0;
890224641Stuexen	num_abandoned = 0;
891224641Stuexen	(void)sctp_mark_all_for_resend(stcb, net, alt, win_probe,
892224641Stuexen	    &num_mk, &num_abandoned);
893224641Stuexen	/* FR Loss recovery just ended with the T3. */
894224641Stuexen	stcb->asoc.fast_retran_loss_recovery = 0;
895224641Stuexen
896224641Stuexen	/* CMT FR loss recovery ended with the T3 */
897224641Stuexen	net->fast_retran_loss_recovery = 0;
898224641Stuexen	if ((stcb->asoc.cc_functions.sctp_cwnd_new_transmission_begins) &&
899224641Stuexen	    (net->flight_size == 0)) {
900224641Stuexen		(*stcb->asoc.cc_functions.sctp_cwnd_new_transmission_begins) (stcb, net);
901224641Stuexen	}
902224641Stuexen	/*
903224641Stuexen	 * setup the sat loss recovery that prevents satellite cwnd advance.
904224641Stuexen	 */
905224641Stuexen	stcb->asoc.sat_t3_loss_recovery = 1;
906224641Stuexen	stcb->asoc.sat_t3_recovery_tsn = stcb->asoc.sending_seq;
907224641Stuexen
908224641Stuexen	/* Backoff the timer and cwnd */
909224641Stuexen	sctp_backoff_on_timeout(stcb, net, win_probe, num_mk, num_abandoned);
910224641Stuexen	if ((!(net->dest_state & SCTP_ADDR_REACHABLE)) ||
911224641Stuexen	    (net->dest_state & SCTP_ADDR_PF)) {
912163953Srrs		/* Move all pending over too */
913212712Stuexen		sctp_move_chunks_from_net(stcb, net);
914169352Srrs
915169352Srrs		/*
916169352Srrs		 * Get the address that failed, to force a new src address
917169352Srrs		 * selecton and a route allocation.
918169352Srrs		 */
919169352Srrs		if (net->ro._s_addr) {
920169352Srrs			sctp_free_ifa(net->ro._s_addr);
921169352Srrs			net->ro._s_addr = NULL;
922169352Srrs		}
923169352Srrs		net->src_addr_selected = 0;
924169352Srrs
925169352Srrs		/* Force a route allocation too */
926169352Srrs		if (net->ro.ro_rt) {
927169352Srrs			RTFREE(net->ro.ro_rt);
928169352Srrs			net->ro.ro_rt = NULL;
929169352Srrs		}
930163953Srrs		/* Was it our primary? */
931163953Srrs		if ((stcb->asoc.primary_destination == net) && (alt != net)) {
932163953Srrs			/*
933163953Srrs			 * Yes, note it as such and find an alternate note:
934163953Srrs			 * this means HB code must use this to resent the
935163953Srrs			 * primary if it goes active AND if someone does a
936163953Srrs			 * change-primary then this flag must be cleared
937163953Srrs			 * from any net structures.
938163953Srrs			 */
939224641Stuexen			if (stcb->asoc.alternate) {
940224641Stuexen				sctp_free_remote_addr(stcb->asoc.alternate);
941163953Srrs			}
942224641Stuexen			stcb->asoc.alternate = alt;
943224641Stuexen			atomic_add_int(&stcb->asoc.alternate->ref_count, 1);
944163953Srrs		}
945163953Srrs	}
946163953Srrs	/*
947163953Srrs	 * Special case for cookie-echo'ed case, we don't do output but must
948163953Srrs	 * await the COOKIE-ACK before retransmission
949163953Srrs	 */
950163953Srrs	if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_ECHOED) {
951163953Srrs		/*
952163953Srrs		 * Here we just reset the timer and start again since we
953163953Srrs		 * have not established the asoc
954163953Srrs		 */
955163953Srrs		sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
956163953Srrs		return (0);
957163953Srrs	}
958163953Srrs	if (stcb->asoc.peer_supports_prsctp) {
959163953Srrs		struct sctp_tmit_chunk *lchk;
960163953Srrs
961163953Srrs		lchk = sctp_try_advance_peer_ack_point(stcb, &stcb->asoc);
962163953Srrs		/* C3. See if we need to send a Fwd-TSN */
963216825Stuexen		if (SCTP_TSN_GT(stcb->asoc.advanced_peer_ack_point, stcb->asoc.last_acked_seq)) {
964163953Srrs			send_forward_tsn(stcb, &stcb->asoc);
965163953Srrs			if (lchk) {
966163953Srrs				/* Assure a timer is up */
967163953Srrs				sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, stcb, lchk->whoTo);
968163953Srrs			}
969163953Srrs		}
970163953Srrs	}
971179783Srrs	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_MONITOR_ENABLE) {
972170744Srrs		sctp_log_cwnd(stcb, net, net->cwnd, SCTP_CWND_LOG_FROM_RTX);
973170744Srrs	}
974163953Srrs	return (0);
975163953Srrs}
976163953Srrs
977163953Srrsint
978163953Srrssctp_t1init_timer(struct sctp_inpcb *inp,
979163953Srrs    struct sctp_tcb *stcb,
980163953Srrs    struct sctp_nets *net)
981163953Srrs{
982163953Srrs	/* bump the thresholds */
983163953Srrs	if (stcb->asoc.delayed_connection) {
984163953Srrs		/*
985163953Srrs		 * special hook for delayed connection. The library did NOT
986163953Srrs		 * complete the rest of its sends.
987163953Srrs		 */
988163953Srrs		stcb->asoc.delayed_connection = 0;
989172090Srrs		sctp_send_initiate(inp, stcb, SCTP_SO_NOT_LOCKED);
990163953Srrs		return (0);
991163953Srrs	}
992163953Srrs	if (SCTP_GET_STATE((&stcb->asoc)) != SCTP_STATE_COOKIE_WAIT) {
993163953Srrs		return (0);
994163953Srrs	}
995163953Srrs	if (sctp_threshold_management(inp, stcb, net,
996163953Srrs	    stcb->asoc.max_init_times)) {
997163953Srrs		/* Association was destroyed */
998163953Srrs		return (1);
999163953Srrs	}
1000163953Srrs	stcb->asoc.dropped_special_cnt = 0;
1001210599Srrs	sctp_backoff_on_timeout(stcb, stcb->asoc.primary_destination, 1, 0, 0);
1002163953Srrs	if (stcb->asoc.initial_init_rto_max < net->RTO) {
1003163953Srrs		net->RTO = stcb->asoc.initial_init_rto_max;
1004163953Srrs	}
1005163953Srrs	if (stcb->asoc.numnets > 1) {
1006163953Srrs		/* If we have more than one addr use it */
1007163953Srrs		struct sctp_nets *alt;
1008163953Srrs
1009163953Srrs		alt = sctp_find_alternate_net(stcb, stcb->asoc.primary_destination, 0);
1010214928Stuexen		if (alt != stcb->asoc.primary_destination) {
1011212712Stuexen			sctp_move_chunks_from_net(stcb, stcb->asoc.primary_destination);
1012163953Srrs			stcb->asoc.primary_destination = alt;
1013163953Srrs		}
1014163953Srrs	}
1015163953Srrs	/* Send out a new init */
1016172090Srrs	sctp_send_initiate(inp, stcb, SCTP_SO_NOT_LOCKED);
1017163953Srrs	return (0);
1018163953Srrs}
1019163953Srrs
1020163953Srrs/*
1021163953Srrs * For cookie and asconf we actually need to find and mark for resend, then
1022163953Srrs * increment the resend counter (after all the threshold management stuff of
1023163953Srrs * course).
1024163953Srrs */
1025163953Srrsint
1026163953Srrssctp_cookie_timer(struct sctp_inpcb *inp,
1027163953Srrs    struct sctp_tcb *stcb,
1028231038Stuexen    struct sctp_nets *net SCTP_UNUSED)
1029163953Srrs{
1030163953Srrs	struct sctp_nets *alt;
1031163953Srrs	struct sctp_tmit_chunk *cookie;
1032163953Srrs
1033163953Srrs	/* first before all else we must find the cookie */
1034163953Srrs	TAILQ_FOREACH(cookie, &stcb->asoc.control_send_queue, sctp_next) {
1035163953Srrs		if (cookie->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
1036163953Srrs			break;
1037163953Srrs		}
1038163953Srrs	}
1039163953Srrs	if (cookie == NULL) {
1040163953Srrs		if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_ECHOED) {
1041163953Srrs			/* FOOBAR! */
1042266181Stuexen			struct mbuf *op_err;
1043163953Srrs
1044266181Stuexen			op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION,
1045266181Stuexen			    "Cookie timer expired, but no cookie");
1046171440Srrs			inp->last_abort_code = SCTP_FROM_SCTP_TIMER + SCTP_LOC_4;
1047266181Stuexen			sctp_abort_an_association(inp, stcb, op_err, SCTP_SO_NOT_LOCKED);
1048163953Srrs		} else {
1049165220Srrs#ifdef INVARIANTS
1050163953Srrs			panic("Cookie timer expires in wrong state?");
1051163953Srrs#else
1052169420Srrs			SCTP_PRINTF("Strange in state %d not cookie-echoed yet c-e timer expires?\n", SCTP_GET_STATE(&stcb->asoc));
1053163953Srrs			return (0);
1054163953Srrs#endif
1055163953Srrs		}
1056163953Srrs		return (0);
1057163953Srrs	}
1058163953Srrs	/* Ok we found the cookie, threshold management next */
1059163953Srrs	if (sctp_threshold_management(inp, stcb, cookie->whoTo,
1060163953Srrs	    stcb->asoc.max_init_times)) {
1061163953Srrs		/* Assoc is over */
1062163953Srrs		return (1);
1063163953Srrs	}
1064163953Srrs	/*
1065163953Srrs	 * cleared theshold management now lets backoff the address & select
1066163953Srrs	 * an alternate
1067163953Srrs	 */
1068163953Srrs	stcb->asoc.dropped_special_cnt = 0;
1069210599Srrs	sctp_backoff_on_timeout(stcb, cookie->whoTo, 1, 0, 0);
1070163953Srrs	alt = sctp_find_alternate_net(stcb, cookie->whoTo, 0);
1071163953Srrs	if (alt != cookie->whoTo) {
1072163953Srrs		sctp_free_remote_addr(cookie->whoTo);
1073163953Srrs		cookie->whoTo = alt;
1074163953Srrs		atomic_add_int(&alt->ref_count, 1);
1075163953Srrs	}
1076163953Srrs	/* Now mark the retran info */
1077163953Srrs	if (cookie->sent != SCTP_DATAGRAM_RESEND) {
1078163953Srrs		sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
1079163953Srrs	}
1080163953Srrs	cookie->sent = SCTP_DATAGRAM_RESEND;
1081163953Srrs	/*
1082163953Srrs	 * Now call the output routine to kick out the cookie again, Note we
1083163953Srrs	 * don't mark any chunks for retran so that FR will need to kick in
1084163953Srrs	 * to move these (or a send timer).
1085163953Srrs	 */
1086163953Srrs	return (0);
1087163953Srrs}
1088163953Srrs
1089163953Srrsint
1090163953Srrssctp_strreset_timer(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
1091163953Srrs    struct sctp_nets *net)
1092163953Srrs{
1093163953Srrs	struct sctp_nets *alt;
1094163953Srrs	struct sctp_tmit_chunk *strrst = NULL, *chk = NULL;
1095163953Srrs
1096163953Srrs	if (stcb->asoc.stream_reset_outstanding == 0) {
1097163953Srrs		return (0);
1098163953Srrs	}
1099163953Srrs	/* find the existing STRRESET, we use the seq number we sent out on */
1100169420Srrs	(void)sctp_find_stream_reset(stcb, stcb->asoc.str_reset_seq_out, &strrst);
1101163953Srrs	if (strrst == NULL) {
1102163953Srrs		return (0);
1103163953Srrs	}
1104163953Srrs	/* do threshold management */
1105163953Srrs	if (sctp_threshold_management(inp, stcb, strrst->whoTo,
1106163953Srrs	    stcb->asoc.max_send_times)) {
1107163953Srrs		/* Assoc is over */
1108163953Srrs		return (1);
1109163953Srrs	}
1110163953Srrs	/*
1111163953Srrs	 * cleared theshold management now lets backoff the address & select
1112163953Srrs	 * an alternate
1113163953Srrs	 */
1114210599Srrs	sctp_backoff_on_timeout(stcb, strrst->whoTo, 1, 0, 0);
1115163953Srrs	alt = sctp_find_alternate_net(stcb, strrst->whoTo, 0);
1116163953Srrs	sctp_free_remote_addr(strrst->whoTo);
1117163953Srrs	strrst->whoTo = alt;
1118163953Srrs	atomic_add_int(&alt->ref_count, 1);
1119163953Srrs
1120163953Srrs	/* See if a ECN Echo is also stranded */
1121163953Srrs	TAILQ_FOREACH(chk, &stcb->asoc.control_send_queue, sctp_next) {
1122163953Srrs		if ((chk->whoTo == net) &&
1123163953Srrs		    (chk->rec.chunk_id.id == SCTP_ECN_ECHO)) {
1124163953Srrs			sctp_free_remote_addr(chk->whoTo);
1125163953Srrs			if (chk->sent != SCTP_DATAGRAM_RESEND) {
1126163953Srrs				chk->sent = SCTP_DATAGRAM_RESEND;
1127163953Srrs				sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
1128163953Srrs			}
1129163953Srrs			chk->whoTo = alt;
1130163953Srrs			atomic_add_int(&alt->ref_count, 1);
1131163953Srrs		}
1132163953Srrs	}
1133224641Stuexen	if (!(net->dest_state & SCTP_ADDR_REACHABLE)) {
1134163953Srrs		/*
1135163953Srrs		 * If the address went un-reachable, we need to move to
1136163953Srrs		 * alternates for ALL chk's in queue
1137163953Srrs		 */
1138212712Stuexen		sctp_move_chunks_from_net(stcb, net);
1139163953Srrs	}
1140163953Srrs	/* mark the retran info */
1141163953Srrs	if (strrst->sent != SCTP_DATAGRAM_RESEND)
1142163953Srrs		sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
1143163953Srrs	strrst->sent = SCTP_DATAGRAM_RESEND;
1144163953Srrs
1145163953Srrs	/* restart the timer */
1146163953Srrs	sctp_timer_start(SCTP_TIMER_TYPE_STRRESET, inp, stcb, strrst->whoTo);
1147163953Srrs	return (0);
1148163953Srrs}
1149163953Srrs
1150163953Srrsint
1151163953Srrssctp_asconf_timer(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
1152163953Srrs    struct sctp_nets *net)
1153163953Srrs{
1154163953Srrs	struct sctp_nets *alt;
1155216822Stuexen	struct sctp_tmit_chunk *asconf, *chk;
1156163953Srrs
1157171572Srrs	/* is this a first send, or a retransmission? */
1158179157Srrs	if (TAILQ_EMPTY(&stcb->asoc.asconf_send_queue)) {
1159163953Srrs		/* compose a new ASCONF chunk and send it */
1160172190Srrs		sctp_send_asconf(stcb, net, SCTP_ADDR_NOT_LOCKED);
1161163953Srrs	} else {
1162171572Srrs		/*
1163171572Srrs		 * Retransmission of the existing ASCONF is needed
1164171572Srrs		 */
1165163953Srrs
1166163953Srrs		/* find the existing ASCONF */
1167179157Srrs		asconf = TAILQ_FIRST(&stcb->asoc.asconf_send_queue);
1168163953Srrs		if (asconf == NULL) {
1169163953Srrs			return (0);
1170163953Srrs		}
1171163953Srrs		/* do threshold management */
1172163953Srrs		if (sctp_threshold_management(inp, stcb, asconf->whoTo,
1173163953Srrs		    stcb->asoc.max_send_times)) {
1174163953Srrs			/* Assoc is over */
1175163953Srrs			return (1);
1176163953Srrs		}
1177163953Srrs		if (asconf->snd_count > stcb->asoc.max_send_times) {
1178163953Srrs			/*
1179171572Srrs			 * Something is rotten: our peer is not responding
1180171572Srrs			 * to ASCONFs but apparently is to other chunks.
1181171572Srrs			 * i.e. it is not properly handling the chunk type
1182171572Srrs			 * upper bits. Mark this peer as ASCONF incapable
1183171572Srrs			 * and cleanup.
1184163953Srrs			 */
1185169420Srrs			SCTPDBG(SCTP_DEBUG_TIMER1, "asconf_timer: Peer has not responded to our repeated ASCONFs\n");
1186163953Srrs			sctp_asconf_cleanup(stcb, net);
1187163953Srrs			return (0);
1188163953Srrs		}
1189163953Srrs		/*
1190171572Srrs		 * cleared threshold management, so now backoff the net and
1191171572Srrs		 * select an alternate
1192163953Srrs		 */
1193210599Srrs		sctp_backoff_on_timeout(stcb, asconf->whoTo, 1, 0, 0);
1194163953Srrs		alt = sctp_find_alternate_net(stcb, asconf->whoTo, 0);
1195179157Srrs		if (asconf->whoTo != alt) {
1196179157Srrs			sctp_free_remote_addr(asconf->whoTo);
1197179157Srrs			asconf->whoTo = alt;
1198179157Srrs			atomic_add_int(&alt->ref_count, 1);
1199179157Srrs		}
1200171572Srrs		/* See if an ECN Echo is also stranded */
1201163953Srrs		TAILQ_FOREACH(chk, &stcb->asoc.control_send_queue, sctp_next) {
1202163953Srrs			if ((chk->whoTo == net) &&
1203163953Srrs			    (chk->rec.chunk_id.id == SCTP_ECN_ECHO)) {
1204163953Srrs				sctp_free_remote_addr(chk->whoTo);
1205163953Srrs				chk->whoTo = alt;
1206163953Srrs				if (chk->sent != SCTP_DATAGRAM_RESEND) {
1207163953Srrs					chk->sent = SCTP_DATAGRAM_RESEND;
1208163953Srrs					sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
1209163953Srrs				}
1210163953Srrs				atomic_add_int(&alt->ref_count, 1);
1211163953Srrs			}
1212163953Srrs		}
1213216822Stuexen		TAILQ_FOREACH(chk, &stcb->asoc.asconf_send_queue, sctp_next) {
1214179157Srrs			if (chk->whoTo != alt) {
1215179157Srrs				sctp_free_remote_addr(chk->whoTo);
1216179157Srrs				chk->whoTo = alt;
1217179157Srrs				atomic_add_int(&alt->ref_count, 1);
1218179157Srrs			}
1219179157Srrs			if (asconf->sent != SCTP_DATAGRAM_RESEND && chk->sent != SCTP_DATAGRAM_UNSENT)
1220179157Srrs				sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
1221179157Srrs			chk->sent = SCTP_DATAGRAM_RESEND;
1222179157Srrs		}
1223224641Stuexen		if (!(net->dest_state & SCTP_ADDR_REACHABLE)) {
1224163953Srrs			/*
1225163953Srrs			 * If the address went un-reachable, we need to move
1226171572Srrs			 * to the alternate for ALL chunks in queue
1227163953Srrs			 */
1228212712Stuexen			sctp_move_chunks_from_net(stcb, net);
1229163953Srrs		}
1230163953Srrs		/* mark the retran info */
1231163953Srrs		if (asconf->sent != SCTP_DATAGRAM_RESEND)
1232163953Srrs			sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
1233163953Srrs		asconf->sent = SCTP_DATAGRAM_RESEND;
1234179157Srrs
1235179157Srrs		/* send another ASCONF if any and we can do */
1236179157Srrs		sctp_send_asconf(stcb, alt, SCTP_ADDR_NOT_LOCKED);
1237163953Srrs	}
1238163953Srrs	return (0);
1239163953Srrs}
1240163953Srrs
1241172091Srrs/* Mobility adaptation */
1242172156Srrsvoid
1243172091Srrssctp_delete_prim_timer(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
1244231038Stuexen    struct sctp_nets *net SCTP_UNUSED)
1245172091Srrs{
1246172091Srrs	if (stcb->asoc.deleted_primary == NULL) {
1247172091Srrs		SCTPDBG(SCTP_DEBUG_ASCONF1, "delete_prim_timer: deleted_primary is not stored...\n");
1248172091Srrs		sctp_mobility_feature_off(inp, SCTP_MOBILITY_PRIM_DELETED);
1249172156Srrs		return;
1250172091Srrs	}
1251172091Srrs	SCTPDBG(SCTP_DEBUG_ASCONF1, "delete_prim_timer: finished to keep deleted primary ");
1252172091Srrs	SCTPDBG_ADDR(SCTP_DEBUG_ASCONF1, &stcb->asoc.deleted_primary->ro._l_addr.sa);
1253172091Srrs	sctp_free_remote_addr(stcb->asoc.deleted_primary);
1254172091Srrs	stcb->asoc.deleted_primary = NULL;
1255172091Srrs	sctp_mobility_feature_off(inp, SCTP_MOBILITY_PRIM_DELETED);
1256172156Srrs	return;
1257172091Srrs}
1258172091Srrs
1259163953Srrs/*
1260163953Srrs * For the shutdown and shutdown-ack, we do not keep one around on the
1261163953Srrs * control queue. This means we must generate a new one and call the general
1262163953Srrs * chunk output routine, AFTER having done threshold management.
1263214928Stuexen * It is assumed that net is non-NULL.
1264163953Srrs */
1265163953Srrsint
1266163953Srrssctp_shutdown_timer(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
1267163953Srrs    struct sctp_nets *net)
1268163953Srrs{
1269163953Srrs	struct sctp_nets *alt;
1270163953Srrs
1271163953Srrs	/* first threshold managment */
1272163953Srrs	if (sctp_threshold_management(inp, stcb, net, stcb->asoc.max_send_times)) {
1273163953Srrs		/* Assoc is over */
1274163953Srrs		return (1);
1275163953Srrs	}
1276214928Stuexen	sctp_backoff_on_timeout(stcb, net, 1, 0, 0);
1277163953Srrs	/* second select an alternative */
1278163953Srrs	alt = sctp_find_alternate_net(stcb, net, 0);
1279163953Srrs
1280163953Srrs	/* third generate a shutdown into the queue for out net */
1281214928Stuexen	sctp_send_shutdown(stcb, alt);
1282214928Stuexen
1283163953Srrs	/* fourth restart timer */
1284163953Srrs	sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, inp, stcb, alt);
1285163953Srrs	return (0);
1286163953Srrs}
1287163953Srrs
1288163953Srrsint
1289163953Srrssctp_shutdownack_timer(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
1290163953Srrs    struct sctp_nets *net)
1291163953Srrs{
1292163953Srrs	struct sctp_nets *alt;
1293163953Srrs
1294163953Srrs	/* first threshold managment */
1295163953Srrs	if (sctp_threshold_management(inp, stcb, net, stcb->asoc.max_send_times)) {
1296163953Srrs		/* Assoc is over */
1297163953Srrs		return (1);
1298163953Srrs	}
1299214928Stuexen	sctp_backoff_on_timeout(stcb, net, 1, 0, 0);
1300163953Srrs	/* second select an alternative */
1301163953Srrs	alt = sctp_find_alternate_net(stcb, net, 0);
1302163953Srrs
1303163953Srrs	/* third generate a shutdown into the queue for out net */
1304163953Srrs	sctp_send_shutdown_ack(stcb, alt);
1305163953Srrs
1306163953Srrs	/* fourth restart timer */
1307163953Srrs	sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNACK, inp, stcb, alt);
1308163953Srrs	return (0);
1309163953Srrs}
1310163953Srrs
1311163953Srrsstatic void
1312163953Srrssctp_audit_stream_queues_for_size(struct sctp_inpcb *inp,
1313163953Srrs    struct sctp_tcb *stcb)
1314163953Srrs{
1315163953Srrs	struct sctp_stream_queue_pending *sp;
1316217760Stuexen	unsigned int i, chks_in_queue = 0;
1317163953Srrs	int being_filled = 0;
1318163953Srrs
1319163953Srrs	/*
1320163953Srrs	 * This function is ONLY called when the send/sent queues are empty.
1321163953Srrs	 */
1322163953Srrs	if ((stcb == NULL) || (inp == NULL))
1323163953Srrs		return;
1324163953Srrs
1325163953Srrs	if (stcb->asoc.sent_queue_retran_cnt) {
1326169420Srrs		SCTP_PRINTF("Hmm, sent_queue_retran_cnt is non-zero %d\n",
1327163953Srrs		    stcb->asoc.sent_queue_retran_cnt);
1328163953Srrs		stcb->asoc.sent_queue_retran_cnt = 0;
1329163953Srrs	}
1330217760Stuexen	if (stcb->asoc.ss_functions.sctp_ss_is_empty(stcb, &stcb->asoc)) {
1331218241Stuexen		/* No stream scheduler information, initialize scheduler */
1332218241Stuexen		stcb->asoc.ss_functions.sctp_ss_init(stcb, &stcb->asoc, 0);
1333218241Stuexen		if (!stcb->asoc.ss_functions.sctp_ss_is_empty(stcb, &stcb->asoc)) {
1334218241Stuexen			/* yep, we lost a stream or two */
1335218241Stuexen			SCTP_PRINTF("Found additional streams NOT managed by scheduler, corrected\n");
1336163953Srrs		} else {
1337218241Stuexen			/* no streams lost */
1338163953Srrs			stcb->asoc.total_output_queue_size = 0;
1339163953Srrs		}
1340163953Srrs	}
1341163953Srrs	/* Check to see if some data queued, if so report it */
1342217760Stuexen	for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
1343217760Stuexen		if (!TAILQ_EMPTY(&stcb->asoc.strmout[i].outqueue)) {
1344217760Stuexen			TAILQ_FOREACH(sp, &stcb->asoc.strmout[i].outqueue, next) {
1345163953Srrs				if (sp->msg_is_complete)
1346163953Srrs					being_filled++;
1347163953Srrs				chks_in_queue++;
1348163953Srrs			}
1349163953Srrs		}
1350163953Srrs	}
1351163953Srrs	if (chks_in_queue != stcb->asoc.stream_queue_cnt) {
1352169420Srrs		SCTP_PRINTF("Hmm, stream queue cnt at %d I counted %d in stream out wheel\n",
1353163953Srrs		    stcb->asoc.stream_queue_cnt, chks_in_queue);
1354163953Srrs	}
1355163953Srrs	if (chks_in_queue) {
1356163953Srrs		/* call the output queue function */
1357172090Srrs		sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_T3, SCTP_SO_NOT_LOCKED);
1358163953Srrs		if ((TAILQ_EMPTY(&stcb->asoc.send_queue)) &&
1359163953Srrs		    (TAILQ_EMPTY(&stcb->asoc.sent_queue))) {
1360163953Srrs			/*
1361163953Srrs			 * Probably should go in and make it go back through
1362163953Srrs			 * and add fragments allowed
1363163953Srrs			 */
1364163953Srrs			if (being_filled == 0) {
1365169420Srrs				SCTP_PRINTF("Still nothing moved %d chunks are stuck\n",
1366163953Srrs				    chks_in_queue);
1367163953Srrs			}
1368163953Srrs		}
1369163953Srrs	} else {
1370169420Srrs		SCTP_PRINTF("Found no chunks on any queue tot:%lu\n",
1371163953Srrs		    (u_long)stcb->asoc.total_output_queue_size);
1372163953Srrs		stcb->asoc.total_output_queue_size = 0;
1373163953Srrs	}
1374163953Srrs}
1375163953Srrs
1376163953Srrsint
1377163953Srrssctp_heartbeat_timer(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
1378224641Stuexen    struct sctp_nets *net)
1379163953Srrs{
1380224641Stuexen	uint8_t net_was_pf;
1381171440Srrs
1382231039Stuexen	if (net->dest_state & SCTP_ADDR_PF) {
1383231039Stuexen		net_was_pf = 1;
1384231039Stuexen	} else {
1385231039Stuexen		net_was_pf = 0;
1386231039Stuexen	}
1387231039Stuexen	if (net->hb_responded == 0) {
1388231039Stuexen		if (net->ro._s_addr) {
1389231039Stuexen			/*
1390231039Stuexen			 * Invalidate the src address if we did not get a
1391231039Stuexen			 * response last time.
1392231039Stuexen			 */
1393231039Stuexen			sctp_free_ifa(net->ro._s_addr);
1394231039Stuexen			net->ro._s_addr = NULL;
1395231039Stuexen			net->src_addr_selected = 0;
1396224641Stuexen		}
1397231039Stuexen		sctp_backoff_on_timeout(stcb, net, 1, 0, 0);
1398231039Stuexen		if (sctp_threshold_management(inp, stcb, net, stcb->asoc.max_send_times)) {
1399231039Stuexen			/* Assoc is over */
1400231039Stuexen			return (1);
1401163953Srrs		}
1402163953Srrs	}
1403231039Stuexen	/* Zero PBA, if it needs it */
1404231039Stuexen	if (net->partial_bytes_acked) {
1405231039Stuexen		net->partial_bytes_acked = 0;
1406231039Stuexen	}
1407163953Srrs	if ((stcb->asoc.total_output_queue_size > 0) &&
1408163953Srrs	    (TAILQ_EMPTY(&stcb->asoc.send_queue)) &&
1409163953Srrs	    (TAILQ_EMPTY(&stcb->asoc.sent_queue))) {
1410163953Srrs		sctp_audit_stream_queues_for_size(inp, stcb);
1411163953Srrs	}
1412224641Stuexen	if (!(net->dest_state & SCTP_ADDR_NOHB) &&
1413224641Stuexen	    !((net_was_pf == 0) && (net->dest_state & SCTP_ADDR_PF))) {
1414163953Srrs		/*
1415224641Stuexen		 * when move to PF during threshold mangement, a HB has been
1416224641Stuexen		 * queued in that routine
1417163953Srrs		 */
1418234793Stuexen		uint32_t ms_gone_by;
1419234793Stuexen
1420234793Stuexen		if ((net->last_sent_time.tv_sec > 0) ||
1421234793Stuexen		    (net->last_sent_time.tv_usec > 0)) {
1422234793Stuexen			struct timeval diff;
1423234793Stuexen
1424234793Stuexen			SCTP_GETTIME_TIMEVAL(&diff);
1425234793Stuexen			timevalsub(&diff, &net->last_sent_time);
1426234793Stuexen			ms_gone_by = (uint32_t) (diff.tv_sec * 1000) +
1427234793Stuexen			    (uint32_t) (diff.tv_usec / 1000);
1428234793Stuexen		} else {
1429234793Stuexen			ms_gone_by = 0xffffffff;
1430234793Stuexen		}
1431234793Stuexen		if ((ms_gone_by >= net->heart_beat_delay) ||
1432234793Stuexen		    (net->dest_state & SCTP_ADDR_PF)) {
1433234793Stuexen			sctp_send_hb(stcb, net, SCTP_SO_NOT_LOCKED);
1434234793Stuexen		}
1435163953Srrs	}
1436163953Srrs	return (0);
1437163953Srrs}
1438163953Srrs
1439163953Srrsvoid
1440163953Srrssctp_pathmtu_timer(struct sctp_inpcb *inp,
1441163953Srrs    struct sctp_tcb *stcb,
1442163953Srrs    struct sctp_nets *net)
1443163953Srrs{
1444179157Srrs	uint32_t next_mtu, mtu;
1445163953Srrs
1446231038Stuexen	next_mtu = sctp_get_next_mtu(net->mtu);
1447169352Srrs
1448179157Srrs	if ((next_mtu > net->mtu) && (net->port == 0)) {
1449169352Srrs		if ((net->src_addr_selected == 0) ||
1450169352Srrs		    (net->ro._s_addr == NULL) ||
1451169352Srrs		    (net->ro._s_addr->localifa_flags & SCTP_BEING_DELETED)) {
1452169420Srrs			if ((net->ro._s_addr != NULL) && (net->ro._s_addr->localifa_flags & SCTP_BEING_DELETED)) {
1453169352Srrs				sctp_free_ifa(net->ro._s_addr);
1454169352Srrs				net->ro._s_addr = NULL;
1455169352Srrs				net->src_addr_selected = 0;
1456169420Srrs			} else if (net->ro._s_addr == NULL) {
1457179157Srrs#if defined(INET6) && defined(SCTP_EMBEDDED_V6_SCOPE)
1458179157Srrs				if (net->ro._l_addr.sa.sa_family == AF_INET6) {
1459179157Srrs					struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
1460179157Srrs
1461179157Srrs					/* KAME hack: embed scopeid */
1462197288Srrs					(void)sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone));
1463179157Srrs				}
1464179157Srrs#endif
1465179157Srrs
1466169420Srrs				net->ro._s_addr = sctp_source_address_selection(inp,
1467169420Srrs				    stcb,
1468169420Srrs				    (sctp_route_t *) & net->ro,
1469169420Srrs				    net, 0, stcb->asoc.vrf_id);
1470179157Srrs#if defined(INET6) && defined(SCTP_EMBEDDED_V6_SCOPE)
1471179157Srrs				if (net->ro._l_addr.sa.sa_family == AF_INET6) {
1472179157Srrs					struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
1473179157Srrs
1474179157Srrs					(void)sa6_recoverscope(sin6);
1475179157Srrs				}
1476179157Srrs#endif				/* INET6 */
1477169352Srrs			}
1478169352Srrs			if (net->ro._s_addr)
1479169352Srrs				net->src_addr_selected = 1;
1480169352Srrs		}
1481169352Srrs		if (net->ro._s_addr) {
1482169352Srrs			mtu = SCTP_GATHER_MTU_FROM_ROUTE(net->ro._s_addr, &net->ro._s_addr.sa, net->ro.ro_rt);
1483185694Srrs			if (net->port) {
1484185694Srrs				mtu -= sizeof(struct udphdr);
1485185694Srrs			}
1486169352Srrs			if (mtu > next_mtu) {
1487163953Srrs				net->mtu = next_mtu;
1488163953Srrs			}
1489163953Srrs		}
1490163953Srrs	}
1491163953Srrs	/* restart the timer */
1492163953Srrs	sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net);
1493163953Srrs}
1494163953Srrs
1495163953Srrsvoid
1496163953Srrssctp_autoclose_timer(struct sctp_inpcb *inp,
1497163953Srrs    struct sctp_tcb *stcb,
1498163953Srrs    struct sctp_nets *net)
1499163953Srrs{
1500163953Srrs	struct timeval tn, *tim_touse;
1501163953Srrs	struct sctp_association *asoc;
1502163953Srrs	int ticks_gone_by;
1503163953Srrs
1504169378Srrs	(void)SCTP_GETTIME_TIMEVAL(&tn);
1505163953Srrs	if (stcb->asoc.sctp_autoclose_ticks &&
1506163953Srrs	    sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE)) {
1507163953Srrs		/* Auto close is on */
1508163953Srrs		asoc = &stcb->asoc;
1509163953Srrs		/* pick the time to use */
1510163953Srrs		if (asoc->time_last_rcvd.tv_sec >
1511163953Srrs		    asoc->time_last_sent.tv_sec) {
1512163953Srrs			tim_touse = &asoc->time_last_rcvd;
1513163953Srrs		} else {
1514163953Srrs			tim_touse = &asoc->time_last_sent;
1515163953Srrs		}
1516163953Srrs		/* Now has long enough transpired to autoclose? */
1517163953Srrs		ticks_gone_by = SEC_TO_TICKS(tn.tv_sec - tim_touse->tv_sec);
1518163953Srrs		if ((ticks_gone_by > 0) &&
1519163953Srrs		    (ticks_gone_by >= (int)asoc->sctp_autoclose_ticks)) {
1520163953Srrs			/*
1521163953Srrs			 * autoclose time has hit, call the output routine,
1522163953Srrs			 * which should do nothing just to be SURE we don't
1523163953Srrs			 * have hanging data. We can then safely check the
1524163953Srrs			 * queues and know that we are clear to send
1525163953Srrs			 * shutdown
1526163953Srrs			 */
1527172090Srrs			sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_AUTOCLOSE_TMR, SCTP_SO_NOT_LOCKED);
1528163953Srrs			/* Are we clean? */
1529163953Srrs			if (TAILQ_EMPTY(&asoc->send_queue) &&
1530163953Srrs			    TAILQ_EMPTY(&asoc->sent_queue)) {
1531163953Srrs				/*
1532163953Srrs				 * there is nothing queued to send, so I'm
1533163953Srrs				 * done...
1534163953Srrs				 */
1535166675Srrs				if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) {
1536163953Srrs					/* only send SHUTDOWN 1st time thru */
1537224641Stuexen					struct sctp_nets *netp;
1538224641Stuexen
1539252960Stuexen					if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
1540252960Stuexen					    (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
1541252960Stuexen						SCTP_STAT_DECR_GAUGE32(sctps_currestab);
1542252960Stuexen					}
1543252960Stuexen					SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
1544252960Stuexen					SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
1545252960Stuexen					sctp_stop_timers_for_shutdown(stcb);
1546224641Stuexen					if (stcb->asoc.alternate) {
1547224641Stuexen						netp = stcb->asoc.alternate;
1548224641Stuexen					} else {
1549224641Stuexen						netp = stcb->asoc.primary_destination;
1550224641Stuexen					}
1551224641Stuexen					sctp_send_shutdown(stcb, netp);
1552163953Srrs					sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
1553163953Srrs					    stcb->sctp_ep, stcb,
1554224641Stuexen					    netp);
1555163953Srrs					sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
1556163953Srrs					    stcb->sctp_ep, stcb,
1557224641Stuexen					    netp);
1558163953Srrs				}
1559163953Srrs			}
1560163953Srrs		} else {
1561163953Srrs			/*
1562163953Srrs			 * No auto close at this time, reset t-o to check
1563163953Srrs			 * later
1564163953Srrs			 */
1565163953Srrs			int tmp;
1566163953Srrs
1567163953Srrs			/* fool the timer startup to use the time left */
1568163953Srrs			tmp = asoc->sctp_autoclose_ticks;
1569163953Srrs			asoc->sctp_autoclose_ticks -= ticks_gone_by;
1570163953Srrs			sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb,
1571163953Srrs			    net);
1572163953Srrs			/* restore the real tick value */
1573163953Srrs			asoc->sctp_autoclose_ticks = tmp;
1574163953Srrs		}
1575163953Srrs	}
1576163953Srrs}
1577