1163953Srrs/*-
2169382Srrs * Copyright (c) 2001-2007, by Cisco Systems, Inc. All rights reserved.
3235828Stuexen * Copyright (c) 2008-2012, by Randall Stewart. All rights reserved.
4235828Stuexen * 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,
10228653Stuexen *    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
14228653Stuexen *    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: releng/10.2/sys/netinet/sctp_timer.c 283822 2015-05-31 12:46:40Z 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>
52270350Stuexen#if defined(INET) || defined(INET6)
53185694Srrs#include <netinet/udp.h>
54270350Stuexen#endif
55163953Srrs
56163953Srrs
57163953Srrsvoid
58163953Srrssctp_audit_retranmission_queue(struct sctp_association *asoc)
59163953Srrs{
60163953Srrs	struct sctp_tmit_chunk *chk;
61163953Srrs
62169420Srrs	SCTPDBG(SCTP_DEBUG_TIMER4, "Audit invoked on send queue cnt:%d onqueue:%d\n",
63169420Srrs	    asoc->sent_queue_retran_cnt,
64169420Srrs	    asoc->sent_queue_cnt);
65163953Srrs	asoc->sent_queue_retran_cnt = 0;
66163953Srrs	asoc->sent_queue_cnt = 0;
67163953Srrs	TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
68163953Srrs		if (chk->sent == SCTP_DATAGRAM_RESEND) {
69163953Srrs			sctp_ucount_incr(asoc->sent_queue_retran_cnt);
70163953Srrs		}
71163953Srrs		asoc->sent_queue_cnt++;
72163953Srrs	}
73163953Srrs	TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
74163953Srrs		if (chk->sent == SCTP_DATAGRAM_RESEND) {
75163953Srrs			sctp_ucount_incr(asoc->sent_queue_retran_cnt);
76163953Srrs		}
77163953Srrs	}
78179157Srrs	TAILQ_FOREACH(chk, &asoc->asconf_send_queue, sctp_next) {
79179157Srrs		if (chk->sent == SCTP_DATAGRAM_RESEND) {
80179157Srrs			sctp_ucount_incr(asoc->sent_queue_retran_cnt);
81179157Srrs		}
82179157Srrs	}
83169420Srrs	SCTPDBG(SCTP_DEBUG_TIMER4, "Audit completes retran:%d onqueue:%d\n",
84169420Srrs	    asoc->sent_queue_retran_cnt,
85169420Srrs	    asoc->sent_queue_cnt);
86163953Srrs}
87163953Srrs
88163953Srrsint
89163953Srrssctp_threshold_management(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
90163953Srrs    struct sctp_nets *net, uint16_t threshold)
91163953Srrs{
92163953Srrs	if (net) {
93163953Srrs		net->error_count++;
94169420Srrs		SCTPDBG(SCTP_DEBUG_TIMER4, "Error count for %p now %d thresh:%d\n",
95240148Stuexen		    (void *)net, net->error_count,
96169420Srrs		    net->failure_threshold);
97163953Srrs		if (net->error_count > net->failure_threshold) {
98163953Srrs			/* We had a threshold failure */
99163953Srrs			if (net->dest_state & SCTP_ADDR_REACHABLE) {
100163953Srrs				net->dest_state &= ~SCTP_ADDR_REACHABLE;
101167598Srrs				net->dest_state &= ~SCTP_ADDR_REQ_PRIMARY;
102224641Stuexen				net->dest_state &= ~SCTP_ADDR_PF;
103163953Srrs				sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN,
104235414Stuexen				    stcb, 0,
105172090Srrs				    (void *)net, SCTP_SO_NOT_LOCKED);
106163953Srrs			}
107224641Stuexen		} else if ((net->pf_threshold < net->failure_threshold) &&
108224641Stuexen		    (net->error_count > net->pf_threshold)) {
109224641Stuexen			if (!(net->dest_state & SCTP_ADDR_PF)) {
110224641Stuexen				net->dest_state |= SCTP_ADDR_PF;
111224641Stuexen				net->last_active = sctp_get_tick_count();
112224641Stuexen				sctp_send_hb(stcb, net, SCTP_SO_NOT_LOCKED);
113283822Stuexen				sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT,
114283822Stuexen				    stcb->sctp_ep, stcb, net,
115283822Stuexen				    SCTP_FROM_SCTP_TIMER + SCTP_LOC_1);
116224641Stuexen				sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net);
117224641Stuexen			}
118163953Srrs		}
119163953Srrs	}
120163953Srrs	if (stcb == NULL)
121163953Srrs		return (0);
122163953Srrs
123163953Srrs	if (net) {
124163953Srrs		if ((net->dest_state & SCTP_ADDR_UNCONFIRMED) == 0) {
125179783Srrs			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
126171943Srrs				sctp_misc_ints(SCTP_THRESHOLD_INCR,
127171943Srrs				    stcb->asoc.overall_error_count,
128171943Srrs				    (stcb->asoc.overall_error_count + 1),
129171943Srrs				    SCTP_FROM_SCTP_TIMER,
130171943Srrs				    __LINE__);
131171943Srrs			}
132163953Srrs			stcb->asoc.overall_error_count++;
133163953Srrs		}
134163953Srrs	} else {
135179783Srrs		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
136171943Srrs			sctp_misc_ints(SCTP_THRESHOLD_INCR,
137171943Srrs			    stcb->asoc.overall_error_count,
138171943Srrs			    (stcb->asoc.overall_error_count + 1),
139171943Srrs			    SCTP_FROM_SCTP_TIMER,
140171943Srrs			    __LINE__);
141171943Srrs		}
142163953Srrs		stcb->asoc.overall_error_count++;
143163953Srrs	}
144169420Srrs	SCTPDBG(SCTP_DEBUG_TIMER4, "Overall error count for %p now %d thresh:%u state:%x\n",
145240148Stuexen	    (void *)&stcb->asoc, stcb->asoc.overall_error_count,
146169420Srrs	    (uint32_t) threshold,
147169420Srrs	    ((net == NULL) ? (uint32_t) 0 : (uint32_t) net->dest_state));
148163953Srrs	/*
149163953Srrs	 * We specifically do not do >= to give the assoc one more change
150163953Srrs	 * before we fail it.
151163953Srrs	 */
152163953Srrs	if (stcb->asoc.overall_error_count > threshold) {
153163953Srrs		/* Abort notification sends a ULP notify */
154267723Stuexen		struct mbuf *op_err;
155163953Srrs
156267723Stuexen		op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION,
157283723Stuexen		    "Association error counter exceeded");
158283822Stuexen		inp->last_abort_code = SCTP_FROM_SCTP_TIMER + SCTP_LOC_2;
159267723Stuexen		sctp_abort_an_association(inp, stcb, op_err, SCTP_SO_NOT_LOCKED);
160163953Srrs		return (1);
161163953Srrs	}
162163953Srrs	return (0);
163163953Srrs}
164163953Srrs
165214928Stuexen/*
166214928Stuexen * sctp_find_alternate_net() returns a non-NULL pointer as long
167214928Stuexen * the argument net is non-NULL.
168214928Stuexen */
169163953Srrsstruct sctp_nets *
170163953Srrssctp_find_alternate_net(struct sctp_tcb *stcb,
171163953Srrs    struct sctp_nets *net,
172171440Srrs    int mode)
173163953Srrs{
174163953Srrs	/* Find and return an alternate network if possible */
175171440Srrs	struct sctp_nets *alt, *mnet, *min_errors_net = NULL, *max_cwnd_net = NULL;
176163953Srrs	int once;
177163953Srrs
178171440Srrs	/* JRS 5/14/07 - Initialize min_errors to an impossible value. */
179171440Srrs	int min_errors = -1;
180171440Srrs	uint32_t max_cwnd = 0;
181171440Srrs
182163953Srrs	if (stcb->asoc.numnets == 1) {
183163953Srrs		/* No others but net */
184163953Srrs		return (TAILQ_FIRST(&stcb->asoc.nets));
185163953Srrs	}
186171440Srrs	/*
187171440Srrs	 * JRS 5/14/07 - If mode is set to 2, use the CMT PF find alternate
188171440Srrs	 * net algorithm. This algorithm chooses the active destination (not
189171440Srrs	 * in PF state) with the largest cwnd value. If all destinations are
190171440Srrs	 * in PF state, unreachable, or unconfirmed, choose the desination
191171440Srrs	 * that is in PF state with the lowest error count. In case of a
192171440Srrs	 * tie, choose the destination that was most recently active.
193171440Srrs	 */
194171440Srrs	if (mode == 2) {
195163953Srrs		TAILQ_FOREACH(mnet, &stcb->asoc.nets, sctp_next) {
196171440Srrs			/*
197171440Srrs			 * JRS 5/14/07 - If the destination is unreachable
198171440Srrs			 * or unconfirmed, skip it.
199171440Srrs			 */
200163953Srrs			if (((mnet->dest_state & SCTP_ADDR_REACHABLE) != SCTP_ADDR_REACHABLE) ||
201171440Srrs			    (mnet->dest_state & SCTP_ADDR_UNCONFIRMED)) {
202171440Srrs				continue;
203171440Srrs			}
204171440Srrs			/*
205171440Srrs			 * JRS 5/14/07 -  If the destination is reachable
206171440Srrs			 * but in PF state, compare the error count of the
207171440Srrs			 * destination to the minimum error count seen thus
208171440Srrs			 * far. Store the destination with the lower error
209171440Srrs			 * count.  If the error counts are equal, store the
210171440Srrs			 * destination that was most recently active.
211171440Srrs			 */
212171440Srrs			if (mnet->dest_state & SCTP_ADDR_PF) {
213171440Srrs				/*
214171440Srrs				 * JRS 5/14/07 - If the destination under
215171440Srrs				 * consideration is the current destination,
216171440Srrs				 * work as if the error count is one higher.
217171440Srrs				 * The actual error count will not be
218171440Srrs				 * incremented until later in the t3
219171440Srrs				 * handler.
220171440Srrs				 */
221171440Srrs				if (mnet == net) {
222171440Srrs					if (min_errors == -1) {
223171440Srrs						min_errors = mnet->error_count + 1;
224171440Srrs						min_errors_net = mnet;
225171440Srrs					} else if (mnet->error_count + 1 < min_errors) {
226171440Srrs						min_errors = mnet->error_count + 1;
227171440Srrs						min_errors_net = mnet;
228171440Srrs					} else if (mnet->error_count + 1 == min_errors
229171440Srrs					    && mnet->last_active > min_errors_net->last_active) {
230171440Srrs						min_errors_net = mnet;
231171440Srrs						min_errors = mnet->error_count + 1;
232171440Srrs					}
233171440Srrs					continue;
234171440Srrs				} else {
235171440Srrs					if (min_errors == -1) {
236171440Srrs						min_errors = mnet->error_count;
237171440Srrs						min_errors_net = mnet;
238171440Srrs					} else if (mnet->error_count < min_errors) {
239171440Srrs						min_errors = mnet->error_count;
240171440Srrs						min_errors_net = mnet;
241171440Srrs					} else if (mnet->error_count == min_errors
242171440Srrs					    && mnet->last_active > min_errors_net->last_active) {
243171440Srrs						min_errors_net = mnet;
244171440Srrs						min_errors = mnet->error_count;
245171440Srrs					}
246171440Srrs					continue;
247171440Srrs				}
248171440Srrs			}
249171440Srrs			/*
250171440Srrs			 * JRS 5/14/07 - If the destination is reachable and
251171440Srrs			 * not in PF state, compare the cwnd of the
252171440Srrs			 * destination to the highest cwnd seen thus far.
253171440Srrs			 * Store the destination with the higher cwnd value.
254171440Srrs			 * If the cwnd values are equal, randomly choose one
255171440Srrs			 * of the two destinations.
256171440Srrs			 */
257171440Srrs			if (max_cwnd < mnet->cwnd) {
258171440Srrs				max_cwnd_net = mnet;
259171440Srrs				max_cwnd = mnet->cwnd;
260171440Srrs			} else if (max_cwnd == mnet->cwnd) {
261171440Srrs				uint32_t rndval;
262171440Srrs				uint8_t this_random;
263171440Srrs
264171440Srrs				if (stcb->asoc.hb_random_idx > 3) {
265171440Srrs					rndval = sctp_select_initial_TSN(&stcb->sctp_ep->sctp_ep);
266171440Srrs					memcpy(stcb->asoc.hb_random_values, &rndval, sizeof(stcb->asoc.hb_random_values));
267171440Srrs					this_random = stcb->asoc.hb_random_values[0];
268171440Srrs					stcb->asoc.hb_random_idx++;
269171440Srrs					stcb->asoc.hb_ect_randombit = 0;
270171440Srrs				} else {
271171440Srrs					this_random = stcb->asoc.hb_random_values[stcb->asoc.hb_random_idx];
272171440Srrs					stcb->asoc.hb_random_idx++;
273171440Srrs					stcb->asoc.hb_ect_randombit = 0;
274171440Srrs				}
275171440Srrs				if (this_random % 2 == 1) {
276171440Srrs					max_cwnd_net = mnet;
277180387Srrs					max_cwnd = mnet->cwnd;	/* Useless? */
278171440Srrs				}
279171440Srrs			}
280171440Srrs		}
281171440Srrs		if (max_cwnd_net == NULL) {
282171440Srrs			if (min_errors_net == NULL) {
283171440Srrs				return (net);
284171440Srrs			}
285171440Srrs			return (min_errors_net);
286171440Srrs		} else {
287171440Srrs			return (max_cwnd_net);
288171440Srrs		}
289171440Srrs	}
290171440Srrs	/*
291171440Srrs	 * JRS 5/14/07 - If mode is set to 1, use the CMT policy for
292171440Srrs	 * choosing an alternate net.
293171440Srrs	 */
294171440Srrs	else if (mode == 1) {
295171440Srrs		TAILQ_FOREACH(mnet, &stcb->asoc.nets, sctp_next) {
296171440Srrs			if (((mnet->dest_state & SCTP_ADDR_REACHABLE) != SCTP_ADDR_REACHABLE) ||
297214928Stuexen			    (mnet->dest_state & SCTP_ADDR_UNCONFIRMED)) {
298163953Srrs				/*
299163953Srrs				 * will skip ones that are not-reachable or
300163953Srrs				 * unconfirmed
301163953Srrs				 */
302163953Srrs				continue;
303163953Srrs			}
304171440Srrs			if (max_cwnd < mnet->cwnd) {
305171440Srrs				max_cwnd_net = mnet;
306171440Srrs				max_cwnd = mnet->cwnd;
307171440Srrs			} else if (max_cwnd == mnet->cwnd) {
308163953Srrs				uint32_t rndval;
309163953Srrs				uint8_t this_random;
310163953Srrs
311163953Srrs				if (stcb->asoc.hb_random_idx > 3) {
312163953Srrs					rndval = sctp_select_initial_TSN(&stcb->sctp_ep->sctp_ep);
313163953Srrs					memcpy(stcb->asoc.hb_random_values, &rndval,
314163953Srrs					    sizeof(stcb->asoc.hb_random_values));
315163953Srrs					this_random = stcb->asoc.hb_random_values[0];
316163953Srrs					stcb->asoc.hb_random_idx = 0;
317163953Srrs					stcb->asoc.hb_ect_randombit = 0;
318163953Srrs				} else {
319163953Srrs					this_random = stcb->asoc.hb_random_values[stcb->asoc.hb_random_idx];
320163953Srrs					stcb->asoc.hb_random_idx++;
321163953Srrs					stcb->asoc.hb_ect_randombit = 0;
322163953Srrs				}
323163953Srrs				if (this_random % 2) {
324171440Srrs					max_cwnd_net = mnet;
325171440Srrs					max_cwnd = mnet->cwnd;
326163953Srrs				}
327163953Srrs			}
328163953Srrs		}
329171440Srrs		if (max_cwnd_net) {
330171440Srrs			return (max_cwnd_net);
331163953Srrs		}
332163953Srrs	}
333163953Srrs	mnet = net;
334163953Srrs	once = 0;
335163953Srrs
336163953Srrs	if (mnet == NULL) {
337163953Srrs		mnet = TAILQ_FIRST(&stcb->asoc.nets);
338212225Srrs		if (mnet == NULL) {
339212225Srrs			return (NULL);
340212225Srrs		}
341163953Srrs	}
342283720Stuexen	for (;;) {
343163953Srrs		alt = TAILQ_NEXT(mnet, sctp_next);
344163953Srrs		if (alt == NULL) {
345163953Srrs			once++;
346163953Srrs			if (once > 1) {
347163953Srrs				break;
348163953Srrs			}
349163953Srrs			alt = TAILQ_FIRST(&stcb->asoc.nets);
350212225Srrs			if (alt == NULL) {
351212225Srrs				return (NULL);
352212225Srrs			}
353163953Srrs		}
354163953Srrs		if (alt->ro.ro_rt == NULL) {
355167598Srrs			if (alt->ro._s_addr) {
356167598Srrs				sctp_free_ifa(alt->ro._s_addr);
357167598Srrs				alt->ro._s_addr = NULL;
358167598Srrs			}
359163953Srrs			alt->src_addr_selected = 0;
360163953Srrs		}
361214928Stuexen		if (((alt->dest_state & SCTP_ADDR_REACHABLE) == SCTP_ADDR_REACHABLE) &&
362163953Srrs		    (alt->ro.ro_rt != NULL) &&
363214928Stuexen		    (!(alt->dest_state & SCTP_ADDR_UNCONFIRMED))) {
364163953Srrs			/* Found a reachable address */
365163953Srrs			break;
366163953Srrs		}
367163953Srrs		mnet = alt;
368283720Stuexen	}
369163953Srrs
370163953Srrs	if (alt == NULL) {
371163953Srrs		/* Case where NO insv network exists (dormant state) */
372163953Srrs		/* we rotate destinations */
373163953Srrs		once = 0;
374163953Srrs		mnet = net;
375283720Stuexen		for (;;) {
376212225Srrs			if (mnet == NULL) {
377212225Srrs				return (TAILQ_FIRST(&stcb->asoc.nets));
378212225Srrs			}
379163953Srrs			alt = TAILQ_NEXT(mnet, sctp_next);
380163953Srrs			if (alt == NULL) {
381163953Srrs				once++;
382163953Srrs				if (once > 1) {
383163953Srrs					break;
384163953Srrs				}
385163953Srrs				alt = TAILQ_FIRST(&stcb->asoc.nets);
386283720Stuexen				if (alt == NULL) {
387283720Stuexen					break;
388283720Stuexen				}
389163953Srrs			}
390163953Srrs			if ((!(alt->dest_state & SCTP_ADDR_UNCONFIRMED)) &&
391163953Srrs			    (alt != net)) {
392163953Srrs				/* Found an alternate address */
393163953Srrs				break;
394163953Srrs			}
395163953Srrs			mnet = alt;
396283720Stuexen		}
397163953Srrs	}
398163953Srrs	if (alt == NULL) {
399163953Srrs		return (net);
400163953Srrs	}
401163953Srrs	return (alt);
402163953Srrs}
403163953Srrs
404163953Srrsstatic void
405163953Srrssctp_backoff_on_timeout(struct sctp_tcb *stcb,
406163953Srrs    struct sctp_nets *net,
407163953Srrs    int win_probe,
408210599Srrs    int num_marked, int num_abandoned)
409163953Srrs{
410170642Srrs	if (net->RTO == 0) {
411170642Srrs		net->RTO = stcb->asoc.minrto;
412170642Srrs	}
413163953Srrs	net->RTO <<= 1;
414163953Srrs	if (net->RTO > stcb->asoc.maxrto) {
415163953Srrs		net->RTO = stcb->asoc.maxrto;
416163953Srrs	}
417210599Srrs	if ((win_probe == 0) && (num_marked || num_abandoned)) {
418163953Srrs		/* We don't apply penalty to window probe scenarios */
419171440Srrs		/* JRS - Use the congestion control given in the CC module */
420171440Srrs		stcb->asoc.cc_functions.sctp_cwnd_update_after_timeout(stcb, net);
421163953Srrs	}
422163953Srrs}
423163953Srrs
424184333Srrs#ifndef INVARIANTS
425184333Srrsstatic void
426182367Srrssctp_recover_sent_list(struct sctp_tcb *stcb)
427182367Srrs{
428216822Stuexen	struct sctp_tmit_chunk *chk, *nchk;
429182367Srrs	struct sctp_association *asoc;
430182367Srrs
431182367Srrs	asoc = &stcb->asoc;
432216822Stuexen	TAILQ_FOREACH_SAFE(chk, &asoc->sent_queue, sctp_next, nchk) {
433216825Stuexen		if (SCTP_TSN_GE(asoc->last_acked_seq, chk->rec.data.TSN_seq)) {
434182367Srrs			SCTP_PRINTF("Found chk:%p tsn:%x <= last_acked_seq:%x\n",
435240148Stuexen			    (void *)chk, chk->rec.data.TSN_seq, asoc->last_acked_seq);
436243157Stuexen			if (chk->sent != SCTP_DATAGRAM_NR_ACKED) {
437242714Stuexen				if (asoc->strmout[chk->rec.data.stream_number].chunks_on_queues > 0) {
438242714Stuexen					asoc->strmout[chk->rec.data.stream_number].chunks_on_queues--;
439242714Stuexen				}
440242714Stuexen			}
441182367Srrs			TAILQ_REMOVE(&asoc->sent_queue, chk, sctp_next);
442255190Stuexen			if (PR_SCTP_ENABLED(chk->flags)) {
443182367Srrs				if (asoc->pr_sctp_cnt != 0)
444182367Srrs					asoc->pr_sctp_cnt--;
445182367Srrs			}
446182367Srrs			if (chk->data) {
447182367Srrs				/* sa_ignore NO_NULL_CHK */
448182367Srrs				sctp_free_bufspace(stcb, asoc, chk, 1);
449182367Srrs				sctp_m_freem(chk->data);
450216822Stuexen				chk->data = NULL;
451270357Stuexen				if (asoc->prsctp_supported && PR_SCTP_BUF_ENABLED(chk->flags)) {
452182367Srrs					asoc->sent_queue_cnt_removeable--;
453182367Srrs				}
454182367Srrs			}
455182367Srrs			asoc->sent_queue_cnt--;
456221627Stuexen			sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
457182367Srrs		}
458182367Srrs	}
459182367Srrs	SCTP_PRINTF("after recover order is as follows\n");
460216822Stuexen	TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
461240148Stuexen		SCTP_PRINTF("chk:%p TSN:%x\n", (void *)chk, chk->rec.data.TSN_seq);
462182367Srrs	}
463182367Srrs}
464182367Srrs
465184333Srrs#endif
466184333Srrs
467163953Srrsstatic int
468163953Srrssctp_mark_all_for_resend(struct sctp_tcb *stcb,
469163953Srrs    struct sctp_nets *net,
470163953Srrs    struct sctp_nets *alt,
471163953Srrs    int window_probe,
472210599Srrs    int *num_marked,
473210599Srrs    int *num_abandoned)
474163953Srrs{
475163953Srrs
476163953Srrs	/*
477163953Srrs	 * Mark all chunks (well not all) that were sent to *net for
478163953Srrs	 * retransmission. Move them to alt for there destination as well...
479163953Srrs	 * We only mark chunks that have been outstanding long enough to
480163953Srrs	 * have received feed-back.
481163953Srrs	 */
482216822Stuexen	struct sctp_tmit_chunk *chk, *nchk;
483163953Srrs	struct sctp_nets *lnets;
484163953Srrs	struct timeval now, min_wait, tv;
485219014Stuexen	int cur_rto;
486210599Srrs	int cnt_abandoned;
487168709Srrs	int audit_tf, num_mk, fir;
488163953Srrs	unsigned int cnt_mk;
489168709Srrs	uint32_t orig_flight, orig_tf;
490163953Srrs	uint32_t tsnlast, tsnfirst;
491182367Srrs	int recovery_cnt = 0;
492163953Srrs
493171440Srrs
494163953Srrs	/* none in flight now */
495163953Srrs	audit_tf = 0;
496163953Srrs	fir = 0;
497163953Srrs	/*
498163953Srrs	 * figure out how long a data chunk must be pending before we can
499163953Srrs	 * mark it ..
500163953Srrs	 */
501169378Srrs	(void)SCTP_GETTIME_TIMEVAL(&now);
502163953Srrs	/* get cur rto in micro-seconds */
503219014Stuexen	cur_rto = (net->lastsa >> SCTP_RTT_SHIFT) + net->lastsv;
504219014Stuexen	cur_rto *= 1000;
505224641Stuexen	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
506219014Stuexen		sctp_log_fr(cur_rto,
507170744Srrs		    stcb->asoc.peers_rwnd,
508170744Srrs		    window_probe,
509170744Srrs		    SCTP_FR_T3_MARK_TIME);
510224641Stuexen		sctp_log_fr(net->flight_size, 0, 0, SCTP_FR_CWND_REPORT);
511170744Srrs		sctp_log_fr(net->flight_size, net->cwnd, stcb->asoc.total_flight, SCTP_FR_CWND_REPORT);
512170744Srrs	}
513219014Stuexen	tv.tv_sec = cur_rto / 1000000;
514219014Stuexen	tv.tv_usec = cur_rto % 1000000;
515163953Srrs	min_wait = now;
516163953Srrs	timevalsub(&min_wait, &tv);
517163953Srrs	if (min_wait.tv_sec < 0 || min_wait.tv_usec < 0) {
518163953Srrs		/*
519163953Srrs		 * if we hit here, we don't have enough seconds on the clock
520163953Srrs		 * to account for the RTO. We just let the lower seconds be
521163953Srrs		 * the bounds and don't worry about it. This may mean we
522163953Srrs		 * will mark a lot more than we should.
523163953Srrs		 */
524163953Srrs		min_wait.tv_sec = min_wait.tv_usec = 0;
525163953Srrs	}
526224641Stuexen	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
527219014Stuexen		sctp_log_fr(cur_rto, now.tv_sec, now.tv_usec, SCTP_FR_T3_MARK_TIME);
528170744Srrs		sctp_log_fr(0, min_wait.tv_sec, min_wait.tv_usec, SCTP_FR_T3_MARK_TIME);
529170744Srrs	}
530163953Srrs	/*
531163953Srrs	 * Our rwnd will be incorrect here since we are not adding back the
532163953Srrs	 * cnt * mbuf but we will fix that down below.
533163953Srrs	 */
534163953Srrs	orig_flight = net->flight_size;
535168709Srrs	orig_tf = stcb->asoc.total_flight;
536168709Srrs
537163953Srrs	net->fast_retran_ip = 0;
538163953Srrs	/* Now on to each chunk */
539210599Srrs	cnt_abandoned = 0;
540163953Srrs	num_mk = cnt_mk = 0;
541163953Srrs	tsnfirst = tsnlast = 0;
542184333Srrs#ifndef INVARIANTS
543182367Srrsstart_again:
544184333Srrs#endif
545216822Stuexen	TAILQ_FOREACH_SAFE(chk, &stcb->asoc.sent_queue, sctp_next, nchk) {
546216825Stuexen		if (SCTP_TSN_GE(stcb->asoc.last_acked_seq, chk->rec.data.TSN_seq)) {
547163953Srrs			/* Strange case our list got out of order? */
548258454Stuexen			SCTP_PRINTF("Our list is out of order? last_acked:%x chk:%x\n",
549182367Srrs			    (unsigned int)stcb->asoc.last_acked_seq, (unsigned int)chk->rec.data.TSN_seq);
550182367Srrs			recovery_cnt++;
551182367Srrs#ifdef INVARIANTS
552182367Srrs			panic("last acked >= chk on sent-Q");
553182367Srrs#else
554182367Srrs			SCTP_PRINTF("Recover attempts a restart cnt:%d\n", recovery_cnt);
555182367Srrs			sctp_recover_sent_list(stcb);
556182367Srrs			if (recovery_cnt < 10) {
557182367Srrs				goto start_again;
558182367Srrs			} else {
559182367Srrs				SCTP_PRINTF("Recovery fails %d times??\n", recovery_cnt);
560182367Srrs			}
561182367Srrs#endif
562163953Srrs		}
563163953Srrs		if ((chk->whoTo == net) && (chk->sent < SCTP_DATAGRAM_ACKED)) {
564163953Srrs			/*
565163953Srrs			 * found one to mark: If it is less than
566163953Srrs			 * DATAGRAM_ACKED it MUST not be a skipped or marked
567163953Srrs			 * TSN but instead one that is either already set
568163953Srrs			 * for retransmission OR one that needs
569163953Srrs			 * retransmission.
570163953Srrs			 */
571163953Srrs
572163953Srrs			/* validate its been outstanding long enough */
573224641Stuexen			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
574170744Srrs				sctp_log_fr(chk->rec.data.TSN_seq,
575170744Srrs				    chk->sent_rcv_time.tv_sec,
576170744Srrs				    chk->sent_rcv_time.tv_usec,
577170744Srrs				    SCTP_FR_T3_MARK_TIME);
578170744Srrs			}
579163953Srrs			if ((chk->sent_rcv_time.tv_sec > min_wait.tv_sec) && (window_probe == 0)) {
580163953Srrs				/*
581163953Srrs				 * we have reached a chunk that was sent
582163953Srrs				 * some seconds past our min.. forget it we
583163953Srrs				 * will find no more to send.
584163953Srrs				 */
585224641Stuexen				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
586170744Srrs					sctp_log_fr(0,
587170744Srrs					    chk->sent_rcv_time.tv_sec,
588170744Srrs					    chk->sent_rcv_time.tv_usec,
589170744Srrs					    SCTP_FR_T3_STOPPED);
590170744Srrs				}
591163953Srrs				continue;
592163953Srrs			} else if ((chk->sent_rcv_time.tv_sec == min_wait.tv_sec) &&
593163953Srrs			    (window_probe == 0)) {
594163953Srrs				/*
595163953Srrs				 * we must look at the micro seconds to
596163953Srrs				 * know.
597163953Srrs				 */
598163953Srrs				if (chk->sent_rcv_time.tv_usec >= min_wait.tv_usec) {
599163953Srrs					/*
600163953Srrs					 * ok it was sent after our boundary
601163953Srrs					 * time.
602163953Srrs					 */
603163953Srrs					continue;
604163953Srrs				}
605163953Srrs			}
606270357Stuexen			if (stcb->asoc.prsctp_supported && PR_SCTP_TTL_ENABLED(chk->flags)) {
607163953Srrs				/* Is it expired? */
608212801Stuexen				if (timevalcmp(&now, &chk->rec.data.timetodrop, >)) {
609163953Srrs					/* Yes so drop it */
610163953Srrs					if (chk->data) {
611169420Srrs						(void)sctp_release_pr_sctp_chunk(stcb,
612163953Srrs						    chk,
613235416Stuexen						    1,
614189790Srrs						    SCTP_SO_NOT_LOCKED);
615210599Srrs						cnt_abandoned++;
616163953Srrs					}
617185694Srrs					continue;
618163953Srrs				}
619163953Srrs			}
620270357Stuexen			if (stcb->asoc.prsctp_supported && PR_SCTP_RTX_ENABLED(chk->flags)) {
621163953Srrs				/* Has it been retransmitted tv_sec times? */
622163953Srrs				if (chk->snd_count > chk->rec.data.timetodrop.tv_sec) {
623163953Srrs					if (chk->data) {
624169420Srrs						(void)sctp_release_pr_sctp_chunk(stcb,
625163953Srrs						    chk,
626235416Stuexen						    1,
627189790Srrs						    SCTP_SO_NOT_LOCKED);
628210599Srrs						cnt_abandoned++;
629163953Srrs					}
630185694Srrs					continue;
631163953Srrs				}
632163953Srrs			}
633168709Srrs			if (chk->sent < SCTP_DATAGRAM_RESEND) {
634163953Srrs				sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
635163953Srrs				num_mk++;
636163953Srrs				if (fir == 0) {
637163953Srrs					fir = 1;
638163953Srrs					tsnfirst = chk->rec.data.TSN_seq;
639163953Srrs				}
640163953Srrs				tsnlast = chk->rec.data.TSN_seq;
641224641Stuexen				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
642170744Srrs					sctp_log_fr(chk->rec.data.TSN_seq, chk->snd_count,
643170744Srrs					    0, SCTP_FR_T3_MARKED);
644170744Srrs				}
645168709Srrs				if (chk->rec.data.chunk_was_revoked) {
646168709Srrs					/* deflate the cwnd */
647168709Srrs					chk->whoTo->cwnd -= chk->book_size;
648168709Srrs					chk->rec.data.chunk_was_revoked = 0;
649168709Srrs				}
650168709Srrs				net->marked_retrans++;
651168709Srrs				stcb->asoc.marked_retrans++;
652179783Srrs				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
653170744Srrs					sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_RSND_TO,
654170744Srrs					    chk->whoTo->flight_size,
655170744Srrs					    chk->book_size,
656170744Srrs					    (uintptr_t) chk->whoTo,
657170744Srrs					    chk->rec.data.TSN_seq);
658170744Srrs				}
659168709Srrs				sctp_flight_size_decrease(chk);
660168709Srrs				sctp_total_flight_decrease(stcb, chk);
661168709Srrs				stcb->asoc.peers_rwnd += chk->send_size;
662179783Srrs				stcb->asoc.peers_rwnd += SCTP_BASE_SYSCTL(sctp_peer_chunk_oh);
663163953Srrs			}
664163953Srrs			chk->sent = SCTP_DATAGRAM_RESEND;
665163953Srrs			SCTP_STAT_INCR(sctps_markedretrans);
666165220Srrs
667163953Srrs			/* reset the TSN for striking and other FR stuff */
668163953Srrs			chk->rec.data.doing_fast_retransmit = 0;
669163953Srrs			/* Clear any time so NO RTT is being done */
670219397Srrs
671219397Srrs			if (chk->do_rtt) {
672219397Srrs				if (chk->whoTo->rto_needed == 0) {
673219397Srrs					chk->whoTo->rto_needed = 1;
674219397Srrs				}
675219397Srrs			}
676163953Srrs			chk->do_rtt = 0;
677163953Srrs			if (alt != net) {
678163953Srrs				sctp_free_remote_addr(chk->whoTo);
679163953Srrs				chk->no_fr_allowed = 1;
680163953Srrs				chk->whoTo = alt;
681163953Srrs				atomic_add_int(&alt->ref_count, 1);
682163953Srrs			} else {
683163953Srrs				chk->no_fr_allowed = 0;
684163953Srrs				if (TAILQ_EMPTY(&stcb->asoc.send_queue)) {
685163953Srrs					chk->rec.data.fast_retran_tsn = stcb->asoc.sending_seq;
686163953Srrs				} else {
687163953Srrs					chk->rec.data.fast_retran_tsn = (TAILQ_FIRST(&stcb->asoc.send_queue))->rec.data.TSN_seq;
688163953Srrs				}
689163953Srrs			}
690170181Srrs			/*
691170181Srrs			 * CMT: Do not allow FRs on retransmitted TSNs.
692170181Srrs			 */
693216669Stuexen			if (stcb->asoc.sctp_cmt_on_off > 0) {
694163953Srrs				chk->no_fr_allowed = 1;
695163953Srrs			}
696210599Srrs#ifdef THIS_SHOULD_NOT_BE_DONE
697163953Srrs		} else if (chk->sent == SCTP_DATAGRAM_ACKED) {
698163953Srrs			/* remember highest acked one */
699163953Srrs			could_be_sent = chk;
700210599Srrs#endif
701163953Srrs		}
702163953Srrs		if (chk->sent == SCTP_DATAGRAM_RESEND) {
703163953Srrs			cnt_mk++;
704163953Srrs		}
705163953Srrs	}
706168709Srrs	if ((orig_flight - net->flight_size) != (orig_tf - stcb->asoc.total_flight)) {
707168709Srrs		/* we did not subtract the same things? */
708168709Srrs		audit_tf = 1;
709168709Srrs	}
710224641Stuexen	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
711170744Srrs		sctp_log_fr(tsnfirst, tsnlast, num_mk, SCTP_FR_T3_TIMEOUT);
712170744Srrs	}
713163953Srrs#ifdef SCTP_DEBUG
714169420Srrs	if (num_mk) {
715169420Srrs		SCTPDBG(SCTP_DEBUG_TIMER1, "LAST TSN marked was %x\n",
716169420Srrs		    tsnlast);
717169420Srrs		SCTPDBG(SCTP_DEBUG_TIMER1, "Num marked for retransmission was %d peer-rwd:%ld\n",
718169420Srrs		    num_mk, (u_long)stcb->asoc.peers_rwnd);
719169420Srrs		SCTPDBG(SCTP_DEBUG_TIMER1, "LAST TSN marked was %x\n",
720169420Srrs		    tsnlast);
721169420Srrs		SCTPDBG(SCTP_DEBUG_TIMER1, "Num marked for retransmission was %d peer-rwd:%d\n",
722169420Srrs		    num_mk,
723169420Srrs		    (int)stcb->asoc.peers_rwnd);
724163953Srrs	}
725163953Srrs#endif
726163953Srrs	*num_marked = num_mk;
727210599Srrs	*num_abandoned = cnt_abandoned;
728210493Srrs	/*
729210493Srrs	 * Now check for a ECN Echo that may be stranded And include the
730210493Srrs	 * cnt_mk'd to have all resends in the control queue.
731210493Srrs	 */
732210493Srrs	TAILQ_FOREACH(chk, &stcb->asoc.control_send_queue, sctp_next) {
733210493Srrs		if (chk->sent == SCTP_DATAGRAM_RESEND) {
734210493Srrs			cnt_mk++;
735210493Srrs		}
736210493Srrs		if ((chk->whoTo == net) &&
737210493Srrs		    (chk->rec.chunk_id.id == SCTP_ECN_ECHO)) {
738210493Srrs			sctp_free_remote_addr(chk->whoTo);
739210493Srrs			chk->whoTo = alt;
740210493Srrs			if (chk->sent != SCTP_DATAGRAM_RESEND) {
741210493Srrs				chk->sent = SCTP_DATAGRAM_RESEND;
742210493Srrs				sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
743210493Srrs				cnt_mk++;
744210493Srrs			}
745210493Srrs			atomic_add_int(&alt->ref_count, 1);
746210493Srrs		}
747210493Srrs	}
748210599Srrs#ifdef THIS_SHOULD_NOT_BE_DONE
749163953Srrs	if ((stcb->asoc.sent_queue_retran_cnt == 0) && (could_be_sent)) {
750163953Srrs		/* fix it so we retransmit the highest acked anyway */
751163953Srrs		sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
752163953Srrs		cnt_mk++;
753163953Srrs		could_be_sent->sent = SCTP_DATAGRAM_RESEND;
754163953Srrs	}
755210599Srrs#endif
756163953Srrs	if (stcb->asoc.sent_queue_retran_cnt != cnt_mk) {
757165220Srrs#ifdef INVARIANTS
758171477Srrs		SCTP_PRINTF("Local Audit says there are %d for retran asoc cnt:%d we marked:%d this time\n",
759171477Srrs		    cnt_mk, stcb->asoc.sent_queue_retran_cnt, num_mk);
760163953Srrs#endif
761163953Srrs#ifndef SCTP_AUDITING_ENABLED
762163953Srrs		stcb->asoc.sent_queue_retran_cnt = cnt_mk;
763163953Srrs#endif
764163953Srrs	}
765163953Srrs	if (audit_tf) {
766169420Srrs		SCTPDBG(SCTP_DEBUG_TIMER4,
767169420Srrs		    "Audit total flight due to negative value net:%p\n",
768240148Stuexen		    (void *)net);
769163953Srrs		stcb->asoc.total_flight = 0;
770163953Srrs		stcb->asoc.total_flight_count = 0;
771163953Srrs		/* Clear all networks flight size */
772163953Srrs		TAILQ_FOREACH(lnets, &stcb->asoc.nets, sctp_next) {
773163953Srrs			lnets->flight_size = 0;
774169420Srrs			SCTPDBG(SCTP_DEBUG_TIMER4,
775169420Srrs			    "Net:%p c-f cwnd:%d ssthresh:%d\n",
776240148Stuexen			    (void *)lnets, lnets->cwnd, lnets->ssthresh);
777163953Srrs		}
778163953Srrs		TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
779163953Srrs			if (chk->sent < SCTP_DATAGRAM_RESEND) {
780179783Srrs				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
781170744Srrs					sctp_misc_ints(SCTP_FLIGHT_LOG_UP,
782170744Srrs					    chk->whoTo->flight_size,
783170744Srrs					    chk->book_size,
784170744Srrs					    (uintptr_t) chk->whoTo,
785170744Srrs					    chk->rec.data.TSN_seq);
786170744Srrs				}
787168709Srrs				sctp_flight_size_increase(chk);
788168709Srrs				sctp_total_flight_increase(stcb, chk);
789163953Srrs			}
790163953Srrs		}
791163953Srrs	}
792163953Srrs	/* We return 1 if we only have a window probe outstanding */
793163953Srrs	return (0);
794163953Srrs}
795163953Srrs
796163953Srrs
797163953Srrsint
798163953Srrssctp_t3rxt_timer(struct sctp_inpcb *inp,
799163953Srrs    struct sctp_tcb *stcb,
800163953Srrs    struct sctp_nets *net)
801163953Srrs{
802163953Srrs	struct sctp_nets *alt;
803210599Srrs	int win_probe, num_mk, num_abandoned;
804163953Srrs
805179783Srrs	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
806170744Srrs		sctp_log_fr(0, 0, 0, SCTP_FR_T3_TIMEOUT);
807170744Srrs	}
808179783Srrs	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
809163953Srrs		struct sctp_nets *lnet;
810163953Srrs
811163953Srrs		TAILQ_FOREACH(lnet, &stcb->asoc.nets, sctp_next) {
812163953Srrs			if (net == lnet) {
813163953Srrs				sctp_log_cwnd(stcb, lnet, 1, SCTP_CWND_LOG_FROM_T3);
814163953Srrs			} else {
815163953Srrs				sctp_log_cwnd(stcb, lnet, 0, SCTP_CWND_LOG_FROM_T3);
816163953Srrs			}
817163953Srrs		}
818163953Srrs	}
819163953Srrs	/* Find an alternate and mark those for retransmission */
820163953Srrs	if ((stcb->asoc.peers_rwnd == 0) &&
821163953Srrs	    (stcb->asoc.total_flight < net->mtu)) {
822163953Srrs		SCTP_STAT_INCR(sctps_timowindowprobe);
823163953Srrs		win_probe = 1;
824163953Srrs	} else {
825163953Srrs		win_probe = 0;
826163953Srrs	}
827168709Srrs
828163953Srrs	if (win_probe == 0) {
829163953Srrs		/* We don't do normal threshold management on window probes */
830163953Srrs		if (sctp_threshold_management(inp, stcb, net,
831163953Srrs		    stcb->asoc.max_send_times)) {
832163953Srrs			/* Association was destroyed */
833163953Srrs			return (1);
834163953Srrs		} else {
835163953Srrs			if (net != stcb->asoc.primary_destination) {
836163953Srrs				/* send a immediate HB if our RTO is stale */
837163953Srrs				struct timeval now;
838163953Srrs				unsigned int ms_goneby;
839163953Srrs
840169378Srrs				(void)SCTP_GETTIME_TIMEVAL(&now);
841163953Srrs				if (net->last_sent_time.tv_sec) {
842163953Srrs					ms_goneby = (now.tv_sec - net->last_sent_time.tv_sec) * 1000;
843163953Srrs				} else {
844163953Srrs					ms_goneby = 0;
845163953Srrs				}
846224641Stuexen				if ((net->dest_state & SCTP_ADDR_PF) == 0) {
847224641Stuexen					if ((ms_goneby > net->RTO) || (net->RTO == 0)) {
848185694Srrs						/*
849224641Stuexen						 * no recent feed back in an
850224641Stuexen						 * RTO or more, request a
851224641Stuexen						 * RTT update
852185694Srrs						 */
853224641Stuexen						sctp_send_hb(stcb, net, SCTP_SO_NOT_LOCKED);
854224641Stuexen					}
855163953Srrs				}
856163953Srrs			}
857163953Srrs		}
858163953Srrs	} else {
859163953Srrs		/*
860163953Srrs		 * For a window probe we don't penalize the net's but only
861163953Srrs		 * the association. This may fail it if SACKs are not coming
862163953Srrs		 * back. If sack's are coming with rwnd locked at 0, we will
863163953Srrs		 * continue to hold things waiting for rwnd to raise
864163953Srrs		 */
865163953Srrs		if (sctp_threshold_management(inp, stcb, NULL,
866163953Srrs		    stcb->asoc.max_send_times)) {
867163953Srrs			/* Association was destroyed */
868163953Srrs			return (1);
869163953Srrs		}
870163953Srrs	}
871224641Stuexen	if (stcb->asoc.sctp_cmt_on_off > 0) {
872224641Stuexen		if (net->pf_threshold < net->failure_threshold) {
873224641Stuexen			alt = sctp_find_alternate_net(stcb, net, 2);
874224641Stuexen		} else {
875224641Stuexen			/*
876224641Stuexen			 * CMT: Using RTX_SSTHRESH policy for CMT. If CMT is
877224641Stuexen			 * being used, then pick dest with largest ssthresh
878224641Stuexen			 * for any retransmission.
879224641Stuexen			 */
880224641Stuexen			alt = sctp_find_alternate_net(stcb, net, 1);
881224641Stuexen			/*
882224641Stuexen			 * CUCv2: If a different dest is picked for the
883224641Stuexen			 * retransmission, then new (rtx-)pseudo_cumack
884224641Stuexen			 * needs to be tracked for orig dest. Let CUCv2
885224641Stuexen			 * track new (rtx-) pseudo-cumack always.
886224641Stuexen			 */
887224641Stuexen			net->find_pseudo_cumack = 1;
888224641Stuexen			net->find_rtx_pseudo_cumack = 1;
889224641Stuexen		}
890224641Stuexen	} else {
891224641Stuexen		alt = sctp_find_alternate_net(stcb, net, 0);
892224641Stuexen	}
893224641Stuexen
894224641Stuexen	num_mk = 0;
895224641Stuexen	num_abandoned = 0;
896224641Stuexen	(void)sctp_mark_all_for_resend(stcb, net, alt, win_probe,
897224641Stuexen	    &num_mk, &num_abandoned);
898224641Stuexen	/* FR Loss recovery just ended with the T3. */
899224641Stuexen	stcb->asoc.fast_retran_loss_recovery = 0;
900224641Stuexen
901224641Stuexen	/* CMT FR loss recovery ended with the T3 */
902224641Stuexen	net->fast_retran_loss_recovery = 0;
903224641Stuexen	if ((stcb->asoc.cc_functions.sctp_cwnd_new_transmission_begins) &&
904224641Stuexen	    (net->flight_size == 0)) {
905224641Stuexen		(*stcb->asoc.cc_functions.sctp_cwnd_new_transmission_begins) (stcb, net);
906224641Stuexen	}
907224641Stuexen	/*
908224641Stuexen	 * setup the sat loss recovery that prevents satellite cwnd advance.
909224641Stuexen	 */
910224641Stuexen	stcb->asoc.sat_t3_loss_recovery = 1;
911224641Stuexen	stcb->asoc.sat_t3_recovery_tsn = stcb->asoc.sending_seq;
912224641Stuexen
913224641Stuexen	/* Backoff the timer and cwnd */
914224641Stuexen	sctp_backoff_on_timeout(stcb, net, win_probe, num_mk, num_abandoned);
915224641Stuexen	if ((!(net->dest_state & SCTP_ADDR_REACHABLE)) ||
916224641Stuexen	    (net->dest_state & SCTP_ADDR_PF)) {
917163953Srrs		/* Move all pending over too */
918212712Stuexen		sctp_move_chunks_from_net(stcb, net);
919169352Srrs
920169352Srrs		/*
921169352Srrs		 * Get the address that failed, to force a new src address
922169352Srrs		 * selecton and a route allocation.
923169352Srrs		 */
924169352Srrs		if (net->ro._s_addr) {
925169352Srrs			sctp_free_ifa(net->ro._s_addr);
926169352Srrs			net->ro._s_addr = NULL;
927169352Srrs		}
928169352Srrs		net->src_addr_selected = 0;
929169352Srrs
930169352Srrs		/* Force a route allocation too */
931169352Srrs		if (net->ro.ro_rt) {
932169352Srrs			RTFREE(net->ro.ro_rt);
933169352Srrs			net->ro.ro_rt = NULL;
934169352Srrs		}
935163953Srrs		/* Was it our primary? */
936163953Srrs		if ((stcb->asoc.primary_destination == net) && (alt != net)) {
937163953Srrs			/*
938163953Srrs			 * Yes, note it as such and find an alternate note:
939163953Srrs			 * this means HB code must use this to resent the
940163953Srrs			 * primary if it goes active AND if someone does a
941163953Srrs			 * change-primary then this flag must be cleared
942163953Srrs			 * from any net structures.
943163953Srrs			 */
944224641Stuexen			if (stcb->asoc.alternate) {
945224641Stuexen				sctp_free_remote_addr(stcb->asoc.alternate);
946163953Srrs			}
947224641Stuexen			stcb->asoc.alternate = alt;
948224641Stuexen			atomic_add_int(&stcb->asoc.alternate->ref_count, 1);
949163953Srrs		}
950163953Srrs	}
951163953Srrs	/*
952163953Srrs	 * Special case for cookie-echo'ed case, we don't do output but must
953163953Srrs	 * await the COOKIE-ACK before retransmission
954163953Srrs	 */
955163953Srrs	if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_ECHOED) {
956163953Srrs		/*
957163953Srrs		 * Here we just reset the timer and start again since we
958163953Srrs		 * have not established the asoc
959163953Srrs		 */
960163953Srrs		sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
961163953Srrs		return (0);
962163953Srrs	}
963270357Stuexen	if (stcb->asoc.prsctp_supported) {
964163953Srrs		struct sctp_tmit_chunk *lchk;
965163953Srrs
966163953Srrs		lchk = sctp_try_advance_peer_ack_point(stcb, &stcb->asoc);
967163953Srrs		/* C3. See if we need to send a Fwd-TSN */
968216825Stuexen		if (SCTP_TSN_GT(stcb->asoc.advanced_peer_ack_point, stcb->asoc.last_acked_seq)) {
969163953Srrs			send_forward_tsn(stcb, &stcb->asoc);
970163953Srrs			if (lchk) {
971163953Srrs				/* Assure a timer is up */
972163953Srrs				sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, stcb, lchk->whoTo);
973163953Srrs			}
974163953Srrs		}
975163953Srrs	}
976179783Srrs	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_MONITOR_ENABLE) {
977170744Srrs		sctp_log_cwnd(stcb, net, net->cwnd, SCTP_CWND_LOG_FROM_RTX);
978170744Srrs	}
979163953Srrs	return (0);
980163953Srrs}
981163953Srrs
982163953Srrsint
983163953Srrssctp_t1init_timer(struct sctp_inpcb *inp,
984163953Srrs    struct sctp_tcb *stcb,
985163953Srrs    struct sctp_nets *net)
986163953Srrs{
987163953Srrs	/* bump the thresholds */
988163953Srrs	if (stcb->asoc.delayed_connection) {
989163953Srrs		/*
990163953Srrs		 * special hook for delayed connection. The library did NOT
991163953Srrs		 * complete the rest of its sends.
992163953Srrs		 */
993163953Srrs		stcb->asoc.delayed_connection = 0;
994172090Srrs		sctp_send_initiate(inp, stcb, SCTP_SO_NOT_LOCKED);
995163953Srrs		return (0);
996163953Srrs	}
997163953Srrs	if (SCTP_GET_STATE((&stcb->asoc)) != SCTP_STATE_COOKIE_WAIT) {
998163953Srrs		return (0);
999163953Srrs	}
1000163953Srrs	if (sctp_threshold_management(inp, stcb, net,
1001163953Srrs	    stcb->asoc.max_init_times)) {
1002163953Srrs		/* Association was destroyed */
1003163953Srrs		return (1);
1004163953Srrs	}
1005163953Srrs	stcb->asoc.dropped_special_cnt = 0;
1006210599Srrs	sctp_backoff_on_timeout(stcb, stcb->asoc.primary_destination, 1, 0, 0);
1007163953Srrs	if (stcb->asoc.initial_init_rto_max < net->RTO) {
1008163953Srrs		net->RTO = stcb->asoc.initial_init_rto_max;
1009163953Srrs	}
1010163953Srrs	if (stcb->asoc.numnets > 1) {
1011163953Srrs		/* If we have more than one addr use it */
1012163953Srrs		struct sctp_nets *alt;
1013163953Srrs
1014163953Srrs		alt = sctp_find_alternate_net(stcb, stcb->asoc.primary_destination, 0);
1015214928Stuexen		if (alt != stcb->asoc.primary_destination) {
1016212712Stuexen			sctp_move_chunks_from_net(stcb, stcb->asoc.primary_destination);
1017163953Srrs			stcb->asoc.primary_destination = alt;
1018163953Srrs		}
1019163953Srrs	}
1020163953Srrs	/* Send out a new init */
1021172090Srrs	sctp_send_initiate(inp, stcb, SCTP_SO_NOT_LOCKED);
1022163953Srrs	return (0);
1023163953Srrs}
1024163953Srrs
1025163953Srrs/*
1026163953Srrs * For cookie and asconf we actually need to find and mark for resend, then
1027163953Srrs * increment the resend counter (after all the threshold management stuff of
1028163953Srrs * course).
1029163953Srrs */
1030163953Srrsint
1031163953Srrssctp_cookie_timer(struct sctp_inpcb *inp,
1032163953Srrs    struct sctp_tcb *stcb,
1033228653Stuexen    struct sctp_nets *net SCTP_UNUSED)
1034163953Srrs{
1035163953Srrs	struct sctp_nets *alt;
1036163953Srrs	struct sctp_tmit_chunk *cookie;
1037163953Srrs
1038163953Srrs	/* first before all else we must find the cookie */
1039163953Srrs	TAILQ_FOREACH(cookie, &stcb->asoc.control_send_queue, sctp_next) {
1040163953Srrs		if (cookie->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
1041163953Srrs			break;
1042163953Srrs		}
1043163953Srrs	}
1044163953Srrs	if (cookie == NULL) {
1045163953Srrs		if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_ECHOED) {
1046163953Srrs			/* FOOBAR! */
1047267723Stuexen			struct mbuf *op_err;
1048163953Srrs
1049267723Stuexen			op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION,
1050267723Stuexen			    "Cookie timer expired, but no cookie");
1051283822Stuexen			inp->last_abort_code = SCTP_FROM_SCTP_TIMER + SCTP_LOC_3;
1052267723Stuexen			sctp_abort_an_association(inp, stcb, op_err, SCTP_SO_NOT_LOCKED);
1053163953Srrs		} else {
1054165220Srrs#ifdef INVARIANTS
1055163953Srrs			panic("Cookie timer expires in wrong state?");
1056163953Srrs#else
1057169420Srrs			SCTP_PRINTF("Strange in state %d not cookie-echoed yet c-e timer expires?\n", SCTP_GET_STATE(&stcb->asoc));
1058163953Srrs			return (0);
1059163953Srrs#endif
1060163953Srrs		}
1061163953Srrs		return (0);
1062163953Srrs	}
1063163953Srrs	/* Ok we found the cookie, threshold management next */
1064163953Srrs	if (sctp_threshold_management(inp, stcb, cookie->whoTo,
1065163953Srrs	    stcb->asoc.max_init_times)) {
1066163953Srrs		/* Assoc is over */
1067163953Srrs		return (1);
1068163953Srrs	}
1069163953Srrs	/*
1070163953Srrs	 * cleared theshold management now lets backoff the address & select
1071163953Srrs	 * an alternate
1072163953Srrs	 */
1073163953Srrs	stcb->asoc.dropped_special_cnt = 0;
1074210599Srrs	sctp_backoff_on_timeout(stcb, cookie->whoTo, 1, 0, 0);
1075163953Srrs	alt = sctp_find_alternate_net(stcb, cookie->whoTo, 0);
1076163953Srrs	if (alt != cookie->whoTo) {
1077163953Srrs		sctp_free_remote_addr(cookie->whoTo);
1078163953Srrs		cookie->whoTo = alt;
1079163953Srrs		atomic_add_int(&alt->ref_count, 1);
1080163953Srrs	}
1081163953Srrs	/* Now mark the retran info */
1082163953Srrs	if (cookie->sent != SCTP_DATAGRAM_RESEND) {
1083163953Srrs		sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
1084163953Srrs	}
1085163953Srrs	cookie->sent = SCTP_DATAGRAM_RESEND;
1086163953Srrs	/*
1087163953Srrs	 * Now call the output routine to kick out the cookie again, Note we
1088163953Srrs	 * don't mark any chunks for retran so that FR will need to kick in
1089163953Srrs	 * to move these (or a send timer).
1090163953Srrs	 */
1091163953Srrs	return (0);
1092163953Srrs}
1093163953Srrs
1094163953Srrsint
1095163953Srrssctp_strreset_timer(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
1096163953Srrs    struct sctp_nets *net)
1097163953Srrs{
1098163953Srrs	struct sctp_nets *alt;
1099163953Srrs	struct sctp_tmit_chunk *strrst = NULL, *chk = NULL;
1100163953Srrs
1101163953Srrs	if (stcb->asoc.stream_reset_outstanding == 0) {
1102163953Srrs		return (0);
1103163953Srrs	}
1104163953Srrs	/* find the existing STRRESET, we use the seq number we sent out on */
1105169420Srrs	(void)sctp_find_stream_reset(stcb, stcb->asoc.str_reset_seq_out, &strrst);
1106163953Srrs	if (strrst == NULL) {
1107163953Srrs		return (0);
1108163953Srrs	}
1109163953Srrs	/* do threshold management */
1110163953Srrs	if (sctp_threshold_management(inp, stcb, strrst->whoTo,
1111163953Srrs	    stcb->asoc.max_send_times)) {
1112163953Srrs		/* Assoc is over */
1113163953Srrs		return (1);
1114163953Srrs	}
1115163953Srrs	/*
1116163953Srrs	 * cleared theshold management now lets backoff the address & select
1117163953Srrs	 * an alternate
1118163953Srrs	 */
1119210599Srrs	sctp_backoff_on_timeout(stcb, strrst->whoTo, 1, 0, 0);
1120163953Srrs	alt = sctp_find_alternate_net(stcb, strrst->whoTo, 0);
1121163953Srrs	sctp_free_remote_addr(strrst->whoTo);
1122163953Srrs	strrst->whoTo = alt;
1123163953Srrs	atomic_add_int(&alt->ref_count, 1);
1124163953Srrs
1125163953Srrs	/* See if a ECN Echo is also stranded */
1126163953Srrs	TAILQ_FOREACH(chk, &stcb->asoc.control_send_queue, sctp_next) {
1127163953Srrs		if ((chk->whoTo == net) &&
1128163953Srrs		    (chk->rec.chunk_id.id == SCTP_ECN_ECHO)) {
1129163953Srrs			sctp_free_remote_addr(chk->whoTo);
1130163953Srrs			if (chk->sent != SCTP_DATAGRAM_RESEND) {
1131163953Srrs				chk->sent = SCTP_DATAGRAM_RESEND;
1132163953Srrs				sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
1133163953Srrs			}
1134163953Srrs			chk->whoTo = alt;
1135163953Srrs			atomic_add_int(&alt->ref_count, 1);
1136163953Srrs		}
1137163953Srrs	}
1138224641Stuexen	if (!(net->dest_state & SCTP_ADDR_REACHABLE)) {
1139163953Srrs		/*
1140163953Srrs		 * If the address went un-reachable, we need to move to
1141163953Srrs		 * alternates for ALL chk's in queue
1142163953Srrs		 */
1143212712Stuexen		sctp_move_chunks_from_net(stcb, net);
1144163953Srrs	}
1145163953Srrs	/* mark the retran info */
1146163953Srrs	if (strrst->sent != SCTP_DATAGRAM_RESEND)
1147163953Srrs		sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
1148163953Srrs	strrst->sent = SCTP_DATAGRAM_RESEND;
1149163953Srrs
1150163953Srrs	/* restart the timer */
1151163953Srrs	sctp_timer_start(SCTP_TIMER_TYPE_STRRESET, inp, stcb, strrst->whoTo);
1152163953Srrs	return (0);
1153163953Srrs}
1154163953Srrs
1155163953Srrsint
1156163953Srrssctp_asconf_timer(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
1157163953Srrs    struct sctp_nets *net)
1158163953Srrs{
1159163953Srrs	struct sctp_nets *alt;
1160216822Stuexen	struct sctp_tmit_chunk *asconf, *chk;
1161163953Srrs
1162171572Srrs	/* is this a first send, or a retransmission? */
1163179157Srrs	if (TAILQ_EMPTY(&stcb->asoc.asconf_send_queue)) {
1164163953Srrs		/* compose a new ASCONF chunk and send it */
1165172190Srrs		sctp_send_asconf(stcb, net, SCTP_ADDR_NOT_LOCKED);
1166163953Srrs	} else {
1167171572Srrs		/*
1168171572Srrs		 * Retransmission of the existing ASCONF is needed
1169171572Srrs		 */
1170163953Srrs
1171163953Srrs		/* find the existing ASCONF */
1172179157Srrs		asconf = TAILQ_FIRST(&stcb->asoc.asconf_send_queue);
1173163953Srrs		if (asconf == NULL) {
1174163953Srrs			return (0);
1175163953Srrs		}
1176163953Srrs		/* do threshold management */
1177163953Srrs		if (sctp_threshold_management(inp, stcb, asconf->whoTo,
1178163953Srrs		    stcb->asoc.max_send_times)) {
1179163953Srrs			/* Assoc is over */
1180163953Srrs			return (1);
1181163953Srrs		}
1182163953Srrs		if (asconf->snd_count > stcb->asoc.max_send_times) {
1183163953Srrs			/*
1184171572Srrs			 * Something is rotten: our peer is not responding
1185171572Srrs			 * to ASCONFs but apparently is to other chunks.
1186171572Srrs			 * i.e. it is not properly handling the chunk type
1187171572Srrs			 * upper bits. Mark this peer as ASCONF incapable
1188171572Srrs			 * and cleanup.
1189163953Srrs			 */
1190169420Srrs			SCTPDBG(SCTP_DEBUG_TIMER1, "asconf_timer: Peer has not responded to our repeated ASCONFs\n");
1191163953Srrs			sctp_asconf_cleanup(stcb, net);
1192163953Srrs			return (0);
1193163953Srrs		}
1194163953Srrs		/*
1195171572Srrs		 * cleared threshold management, so now backoff the net and
1196171572Srrs		 * select an alternate
1197163953Srrs		 */
1198210599Srrs		sctp_backoff_on_timeout(stcb, asconf->whoTo, 1, 0, 0);
1199163953Srrs		alt = sctp_find_alternate_net(stcb, asconf->whoTo, 0);
1200179157Srrs		if (asconf->whoTo != alt) {
1201179157Srrs			sctp_free_remote_addr(asconf->whoTo);
1202179157Srrs			asconf->whoTo = alt;
1203179157Srrs			atomic_add_int(&alt->ref_count, 1);
1204179157Srrs		}
1205171572Srrs		/* See if an ECN Echo is also stranded */
1206163953Srrs		TAILQ_FOREACH(chk, &stcb->asoc.control_send_queue, sctp_next) {
1207163953Srrs			if ((chk->whoTo == net) &&
1208163953Srrs			    (chk->rec.chunk_id.id == SCTP_ECN_ECHO)) {
1209163953Srrs				sctp_free_remote_addr(chk->whoTo);
1210163953Srrs				chk->whoTo = alt;
1211163953Srrs				if (chk->sent != SCTP_DATAGRAM_RESEND) {
1212163953Srrs					chk->sent = SCTP_DATAGRAM_RESEND;
1213163953Srrs					sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
1214163953Srrs				}
1215163953Srrs				atomic_add_int(&alt->ref_count, 1);
1216163953Srrs			}
1217163953Srrs		}
1218216822Stuexen		TAILQ_FOREACH(chk, &stcb->asoc.asconf_send_queue, sctp_next) {
1219179157Srrs			if (chk->whoTo != alt) {
1220179157Srrs				sctp_free_remote_addr(chk->whoTo);
1221179157Srrs				chk->whoTo = alt;
1222179157Srrs				atomic_add_int(&alt->ref_count, 1);
1223179157Srrs			}
1224179157Srrs			if (asconf->sent != SCTP_DATAGRAM_RESEND && chk->sent != SCTP_DATAGRAM_UNSENT)
1225179157Srrs				sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
1226179157Srrs			chk->sent = SCTP_DATAGRAM_RESEND;
1227179157Srrs		}
1228224641Stuexen		if (!(net->dest_state & SCTP_ADDR_REACHABLE)) {
1229163953Srrs			/*
1230163953Srrs			 * If the address went un-reachable, we need to move
1231171572Srrs			 * to the alternate for ALL chunks in queue
1232163953Srrs			 */
1233212712Stuexen			sctp_move_chunks_from_net(stcb, net);
1234163953Srrs		}
1235163953Srrs		/* mark the retran info */
1236163953Srrs		if (asconf->sent != SCTP_DATAGRAM_RESEND)
1237163953Srrs			sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
1238163953Srrs		asconf->sent = SCTP_DATAGRAM_RESEND;
1239179157Srrs
1240179157Srrs		/* send another ASCONF if any and we can do */
1241179157Srrs		sctp_send_asconf(stcb, alt, SCTP_ADDR_NOT_LOCKED);
1242163953Srrs	}
1243163953Srrs	return (0);
1244163953Srrs}
1245163953Srrs
1246172091Srrs/* Mobility adaptation */
1247172156Srrsvoid
1248172091Srrssctp_delete_prim_timer(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
1249228653Stuexen    struct sctp_nets *net SCTP_UNUSED)
1250172091Srrs{
1251172091Srrs	if (stcb->asoc.deleted_primary == NULL) {
1252172091Srrs		SCTPDBG(SCTP_DEBUG_ASCONF1, "delete_prim_timer: deleted_primary is not stored...\n");
1253172091Srrs		sctp_mobility_feature_off(inp, SCTP_MOBILITY_PRIM_DELETED);
1254172156Srrs		return;
1255172091Srrs	}
1256172091Srrs	SCTPDBG(SCTP_DEBUG_ASCONF1, "delete_prim_timer: finished to keep deleted primary ");
1257172091Srrs	SCTPDBG_ADDR(SCTP_DEBUG_ASCONF1, &stcb->asoc.deleted_primary->ro._l_addr.sa);
1258172091Srrs	sctp_free_remote_addr(stcb->asoc.deleted_primary);
1259172091Srrs	stcb->asoc.deleted_primary = NULL;
1260172091Srrs	sctp_mobility_feature_off(inp, SCTP_MOBILITY_PRIM_DELETED);
1261172156Srrs	return;
1262172091Srrs}
1263172091Srrs
1264163953Srrs/*
1265163953Srrs * For the shutdown and shutdown-ack, we do not keep one around on the
1266163953Srrs * control queue. This means we must generate a new one and call the general
1267163953Srrs * chunk output routine, AFTER having done threshold management.
1268214928Stuexen * It is assumed that net is non-NULL.
1269163953Srrs */
1270163953Srrsint
1271163953Srrssctp_shutdown_timer(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
1272163953Srrs    struct sctp_nets *net)
1273163953Srrs{
1274163953Srrs	struct sctp_nets *alt;
1275163953Srrs
1276163953Srrs	/* first threshold managment */
1277163953Srrs	if (sctp_threshold_management(inp, stcb, net, stcb->asoc.max_send_times)) {
1278163953Srrs		/* Assoc is over */
1279163953Srrs		return (1);
1280163953Srrs	}
1281214928Stuexen	sctp_backoff_on_timeout(stcb, net, 1, 0, 0);
1282163953Srrs	/* second select an alternative */
1283163953Srrs	alt = sctp_find_alternate_net(stcb, net, 0);
1284163953Srrs
1285163953Srrs	/* third generate a shutdown into the queue for out net */
1286214928Stuexen	sctp_send_shutdown(stcb, alt);
1287214928Stuexen
1288163953Srrs	/* fourth restart timer */
1289163953Srrs	sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, inp, stcb, alt);
1290163953Srrs	return (0);
1291163953Srrs}
1292163953Srrs
1293163953Srrsint
1294163953Srrssctp_shutdownack_timer(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
1295163953Srrs    struct sctp_nets *net)
1296163953Srrs{
1297163953Srrs	struct sctp_nets *alt;
1298163953Srrs
1299163953Srrs	/* first threshold managment */
1300163953Srrs	if (sctp_threshold_management(inp, stcb, net, stcb->asoc.max_send_times)) {
1301163953Srrs		/* Assoc is over */
1302163953Srrs		return (1);
1303163953Srrs	}
1304214928Stuexen	sctp_backoff_on_timeout(stcb, net, 1, 0, 0);
1305163953Srrs	/* second select an alternative */
1306163953Srrs	alt = sctp_find_alternate_net(stcb, net, 0);
1307163953Srrs
1308163953Srrs	/* third generate a shutdown into the queue for out net */
1309163953Srrs	sctp_send_shutdown_ack(stcb, alt);
1310163953Srrs
1311163953Srrs	/* fourth restart timer */
1312163953Srrs	sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNACK, inp, stcb, alt);
1313163953Srrs	return (0);
1314163953Srrs}
1315163953Srrs
1316163953Srrsstatic void
1317163953Srrssctp_audit_stream_queues_for_size(struct sctp_inpcb *inp,
1318163953Srrs    struct sctp_tcb *stcb)
1319163953Srrs{
1320163953Srrs	struct sctp_stream_queue_pending *sp;
1321217760Stuexen	unsigned int i, chks_in_queue = 0;
1322163953Srrs	int being_filled = 0;
1323163953Srrs
1324163953Srrs	/*
1325163953Srrs	 * This function is ONLY called when the send/sent queues are empty.
1326163953Srrs	 */
1327163953Srrs	if ((stcb == NULL) || (inp == NULL))
1328163953Srrs		return;
1329163953Srrs
1330163953Srrs	if (stcb->asoc.sent_queue_retran_cnt) {
1331169420Srrs		SCTP_PRINTF("Hmm, sent_queue_retran_cnt is non-zero %d\n",
1332163953Srrs		    stcb->asoc.sent_queue_retran_cnt);
1333163953Srrs		stcb->asoc.sent_queue_retran_cnt = 0;
1334163953Srrs	}
1335217760Stuexen	if (stcb->asoc.ss_functions.sctp_ss_is_empty(stcb, &stcb->asoc)) {
1336218241Stuexen		/* No stream scheduler information, initialize scheduler */
1337218241Stuexen		stcb->asoc.ss_functions.sctp_ss_init(stcb, &stcb->asoc, 0);
1338218241Stuexen		if (!stcb->asoc.ss_functions.sctp_ss_is_empty(stcb, &stcb->asoc)) {
1339218241Stuexen			/* yep, we lost a stream or two */
1340218241Stuexen			SCTP_PRINTF("Found additional streams NOT managed by scheduler, corrected\n");
1341163953Srrs		} else {
1342218241Stuexen			/* no streams lost */
1343163953Srrs			stcb->asoc.total_output_queue_size = 0;
1344163953Srrs		}
1345163953Srrs	}
1346163953Srrs	/* Check to see if some data queued, if so report it */
1347217760Stuexen	for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
1348217760Stuexen		if (!TAILQ_EMPTY(&stcb->asoc.strmout[i].outqueue)) {
1349217760Stuexen			TAILQ_FOREACH(sp, &stcb->asoc.strmout[i].outqueue, next) {
1350163953Srrs				if (sp->msg_is_complete)
1351163953Srrs					being_filled++;
1352163953Srrs				chks_in_queue++;
1353163953Srrs			}
1354163953Srrs		}
1355163953Srrs	}
1356163953Srrs	if (chks_in_queue != stcb->asoc.stream_queue_cnt) {
1357169420Srrs		SCTP_PRINTF("Hmm, stream queue cnt at %d I counted %d in stream out wheel\n",
1358163953Srrs		    stcb->asoc.stream_queue_cnt, chks_in_queue);
1359163953Srrs	}
1360163953Srrs	if (chks_in_queue) {
1361163953Srrs		/* call the output queue function */
1362172090Srrs		sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_T3, SCTP_SO_NOT_LOCKED);
1363163953Srrs		if ((TAILQ_EMPTY(&stcb->asoc.send_queue)) &&
1364163953Srrs		    (TAILQ_EMPTY(&stcb->asoc.sent_queue))) {
1365163953Srrs			/*
1366163953Srrs			 * Probably should go in and make it go back through
1367163953Srrs			 * and add fragments allowed
1368163953Srrs			 */
1369163953Srrs			if (being_filled == 0) {
1370169420Srrs				SCTP_PRINTF("Still nothing moved %d chunks are stuck\n",
1371163953Srrs				    chks_in_queue);
1372163953Srrs			}
1373163953Srrs		}
1374163953Srrs	} else {
1375169420Srrs		SCTP_PRINTF("Found no chunks on any queue tot:%lu\n",
1376163953Srrs		    (u_long)stcb->asoc.total_output_queue_size);
1377163953Srrs		stcb->asoc.total_output_queue_size = 0;
1378163953Srrs	}
1379163953Srrs}
1380163953Srrs
1381163953Srrsint
1382163953Srrssctp_heartbeat_timer(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
1383224641Stuexen    struct sctp_nets *net)
1384163953Srrs{
1385224641Stuexen	uint8_t net_was_pf;
1386171440Srrs
1387228907Stuexen	if (net->dest_state & SCTP_ADDR_PF) {
1388228907Stuexen		net_was_pf = 1;
1389228907Stuexen	} else {
1390228907Stuexen		net_was_pf = 0;
1391228907Stuexen	}
1392228907Stuexen	if (net->hb_responded == 0) {
1393228907Stuexen		if (net->ro._s_addr) {
1394228907Stuexen			/*
1395228907Stuexen			 * Invalidate the src address if we did not get a
1396228907Stuexen			 * response last time.
1397228907Stuexen			 */
1398228907Stuexen			sctp_free_ifa(net->ro._s_addr);
1399228907Stuexen			net->ro._s_addr = NULL;
1400228907Stuexen			net->src_addr_selected = 0;
1401224641Stuexen		}
1402228907Stuexen		sctp_backoff_on_timeout(stcb, net, 1, 0, 0);
1403228907Stuexen		if (sctp_threshold_management(inp, stcb, net, stcb->asoc.max_send_times)) {
1404228907Stuexen			/* Assoc is over */
1405228907Stuexen			return (1);
1406163953Srrs		}
1407163953Srrs	}
1408228907Stuexen	/* Zero PBA, if it needs it */
1409228907Stuexen	if (net->partial_bytes_acked) {
1410228907Stuexen		net->partial_bytes_acked = 0;
1411228907Stuexen	}
1412163953Srrs	if ((stcb->asoc.total_output_queue_size > 0) &&
1413163953Srrs	    (TAILQ_EMPTY(&stcb->asoc.send_queue)) &&
1414163953Srrs	    (TAILQ_EMPTY(&stcb->asoc.sent_queue))) {
1415163953Srrs		sctp_audit_stream_queues_for_size(inp, stcb);
1416163953Srrs	}
1417224641Stuexen	if (!(net->dest_state & SCTP_ADDR_NOHB) &&
1418224641Stuexen	    !((net_was_pf == 0) && (net->dest_state & SCTP_ADDR_PF))) {
1419163953Srrs		/*
1420224641Stuexen		 * when move to PF during threshold mangement, a HB has been
1421224641Stuexen		 * queued in that routine
1422163953Srrs		 */
1423234296Stuexen		uint32_t ms_gone_by;
1424234296Stuexen
1425234296Stuexen		if ((net->last_sent_time.tv_sec > 0) ||
1426234296Stuexen		    (net->last_sent_time.tv_usec > 0)) {
1427234296Stuexen			struct timeval diff;
1428234296Stuexen
1429234296Stuexen			SCTP_GETTIME_TIMEVAL(&diff);
1430234296Stuexen			timevalsub(&diff, &net->last_sent_time);
1431234296Stuexen			ms_gone_by = (uint32_t) (diff.tv_sec * 1000) +
1432234296Stuexen			    (uint32_t) (diff.tv_usec / 1000);
1433234296Stuexen		} else {
1434234296Stuexen			ms_gone_by = 0xffffffff;
1435234296Stuexen		}
1436234297Stuexen		if ((ms_gone_by >= net->heart_beat_delay) ||
1437234297Stuexen		    (net->dest_state & SCTP_ADDR_PF)) {
1438234296Stuexen			sctp_send_hb(stcb, net, SCTP_SO_NOT_LOCKED);
1439234296Stuexen		}
1440163953Srrs	}
1441163953Srrs	return (0);
1442163953Srrs}
1443163953Srrs
1444163953Srrsvoid
1445163953Srrssctp_pathmtu_timer(struct sctp_inpcb *inp,
1446163953Srrs    struct sctp_tcb *stcb,
1447163953Srrs    struct sctp_nets *net)
1448163953Srrs{
1449179157Srrs	uint32_t next_mtu, mtu;
1450163953Srrs
1451228653Stuexen	next_mtu = sctp_get_next_mtu(net->mtu);
1452169352Srrs
1453179157Srrs	if ((next_mtu > net->mtu) && (net->port == 0)) {
1454169352Srrs		if ((net->src_addr_selected == 0) ||
1455169352Srrs		    (net->ro._s_addr == NULL) ||
1456169352Srrs		    (net->ro._s_addr->localifa_flags & SCTP_BEING_DELETED)) {
1457169420Srrs			if ((net->ro._s_addr != NULL) && (net->ro._s_addr->localifa_flags & SCTP_BEING_DELETED)) {
1458169352Srrs				sctp_free_ifa(net->ro._s_addr);
1459169352Srrs				net->ro._s_addr = NULL;
1460169352Srrs				net->src_addr_selected = 0;
1461169420Srrs			} else if (net->ro._s_addr == NULL) {
1462179157Srrs#if defined(INET6) && defined(SCTP_EMBEDDED_V6_SCOPE)
1463179157Srrs				if (net->ro._l_addr.sa.sa_family == AF_INET6) {
1464179157Srrs					struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
1465179157Srrs
1466179157Srrs					/* KAME hack: embed scopeid */
1467197288Srrs					(void)sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone));
1468179157Srrs				}
1469179157Srrs#endif
1470179157Srrs
1471169420Srrs				net->ro._s_addr = sctp_source_address_selection(inp,
1472169420Srrs				    stcb,
1473169420Srrs				    (sctp_route_t *) & net->ro,
1474169420Srrs				    net, 0, stcb->asoc.vrf_id);
1475179157Srrs#if defined(INET6) && defined(SCTP_EMBEDDED_V6_SCOPE)
1476179157Srrs				if (net->ro._l_addr.sa.sa_family == AF_INET6) {
1477179157Srrs					struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
1478179157Srrs
1479179157Srrs					(void)sa6_recoverscope(sin6);
1480179157Srrs				}
1481179157Srrs#endif				/* INET6 */
1482169352Srrs			}
1483169352Srrs			if (net->ro._s_addr)
1484169352Srrs				net->src_addr_selected = 1;
1485169352Srrs		}
1486169352Srrs		if (net->ro._s_addr) {
1487169352Srrs			mtu = SCTP_GATHER_MTU_FROM_ROUTE(net->ro._s_addr, &net->ro._s_addr.sa, net->ro.ro_rt);
1488270350Stuexen#if defined(INET) || defined(INET6)
1489185694Srrs			if (net->port) {
1490185694Srrs				mtu -= sizeof(struct udphdr);
1491185694Srrs			}
1492270350Stuexen#endif
1493169352Srrs			if (mtu > next_mtu) {
1494163953Srrs				net->mtu = next_mtu;
1495163953Srrs			}
1496163953Srrs		}
1497163953Srrs	}
1498163953Srrs	/* restart the timer */
1499163953Srrs	sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net);
1500163953Srrs}
1501163953Srrs
1502163953Srrsvoid
1503163953Srrssctp_autoclose_timer(struct sctp_inpcb *inp,
1504163953Srrs    struct sctp_tcb *stcb,
1505163953Srrs    struct sctp_nets *net)
1506163953Srrs{
1507163953Srrs	struct timeval tn, *tim_touse;
1508163953Srrs	struct sctp_association *asoc;
1509163953Srrs	int ticks_gone_by;
1510163953Srrs
1511169378Srrs	(void)SCTP_GETTIME_TIMEVAL(&tn);
1512163953Srrs	if (stcb->asoc.sctp_autoclose_ticks &&
1513163953Srrs	    sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE)) {
1514163953Srrs		/* Auto close is on */
1515163953Srrs		asoc = &stcb->asoc;
1516163953Srrs		/* pick the time to use */
1517163953Srrs		if (asoc->time_last_rcvd.tv_sec >
1518163953Srrs		    asoc->time_last_sent.tv_sec) {
1519163953Srrs			tim_touse = &asoc->time_last_rcvd;
1520163953Srrs		} else {
1521163953Srrs			tim_touse = &asoc->time_last_sent;
1522163953Srrs		}
1523163953Srrs		/* Now has long enough transpired to autoclose? */
1524163953Srrs		ticks_gone_by = SEC_TO_TICKS(tn.tv_sec - tim_touse->tv_sec);
1525163953Srrs		if ((ticks_gone_by > 0) &&
1526163953Srrs		    (ticks_gone_by >= (int)asoc->sctp_autoclose_ticks)) {
1527163953Srrs			/*
1528163953Srrs			 * autoclose time has hit, call the output routine,
1529163953Srrs			 * which should do nothing just to be SURE we don't
1530163953Srrs			 * have hanging data. We can then safely check the
1531163953Srrs			 * queues and know that we are clear to send
1532163953Srrs			 * shutdown
1533163953Srrs			 */
1534172090Srrs			sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_AUTOCLOSE_TMR, SCTP_SO_NOT_LOCKED);
1535163953Srrs			/* Are we clean? */
1536163953Srrs			if (TAILQ_EMPTY(&asoc->send_queue) &&
1537163953Srrs			    TAILQ_EMPTY(&asoc->sent_queue)) {
1538163953Srrs				/*
1539163953Srrs				 * there is nothing queued to send, so I'm
1540163953Srrs				 * done...
1541163953Srrs				 */
1542166675Srrs				if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) {
1543163953Srrs					/* only send SHUTDOWN 1st time thru */
1544224641Stuexen					struct sctp_nets *netp;
1545224641Stuexen
1546246588Stuexen					if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
1547246588Stuexen					    (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
1548246588Stuexen						SCTP_STAT_DECR_GAUGE32(sctps_currestab);
1549246588Stuexen					}
1550246588Stuexen					SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
1551246588Stuexen					SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
1552246588Stuexen					sctp_stop_timers_for_shutdown(stcb);
1553224641Stuexen					if (stcb->asoc.alternate) {
1554224641Stuexen						netp = stcb->asoc.alternate;
1555224641Stuexen					} else {
1556224641Stuexen						netp = stcb->asoc.primary_destination;
1557224641Stuexen					}
1558224641Stuexen					sctp_send_shutdown(stcb, netp);
1559163953Srrs					sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
1560163953Srrs					    stcb->sctp_ep, stcb,
1561224641Stuexen					    netp);
1562163953Srrs					sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
1563163953Srrs					    stcb->sctp_ep, stcb,
1564224641Stuexen					    netp);
1565163953Srrs				}
1566163953Srrs			}
1567163953Srrs		} else {
1568163953Srrs			/*
1569163953Srrs			 * No auto close at this time, reset t-o to check
1570163953Srrs			 * later
1571163953Srrs			 */
1572163953Srrs			int tmp;
1573163953Srrs
1574163953Srrs			/* fool the timer startup to use the time left */
1575163953Srrs			tmp = asoc->sctp_autoclose_ticks;
1576163953Srrs			asoc->sctp_autoclose_ticks -= ticks_gone_by;
1577163953Srrs			sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb,
1578163953Srrs			    net);
1579163953Srrs			/* restore the real tick value */
1580163953Srrs			asoc->sctp_autoclose_ticks = tmp;
1581163953Srrs		}
1582163953Srrs	}
1583163953Srrs}
1584