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: stable/11/sys/netinet/sctp_timer.c 361471 2020-05-25 20:07:23Z 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>
52268526Stuexen#if defined(INET) || defined(INET6)
53185694Srrs#include <netinet/udp.h>
54268526Stuexen#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
88295709Stuexenstatic int
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);
113283650Stuexen				sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT,
114295709Stuexen				    inp, stcb, net,
115283650Stuexen				    SCTP_FROM_SCTP_TIMER + SCTP_LOC_1);
116295709Stuexen				sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp, 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,
146310773Stuexen	    (uint32_t)threshold,
147310773Stuexen	    ((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 */
154263237Stuexen		struct mbuf *op_err;
155163953Srrs
156285925Stuexen		op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
157279841Stuexen		    "Association error counter exceeded");
158283650Stuexen		inp->last_abort_code = SCTP_FROM_SCTP_TIMER + SCTP_LOC_2;
159263237Stuexen		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) {
196310773Stuexen			/*
197310773Stuexen			 * JRS 5/14/07 - If the destination is unreachable
198310773Stuexen			 * or unconfirmed, skip it.
199310773Stuexen			 */
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		}
289310218Stuexen	}			/* JRS 5/14/07 - If mode is set to 1, use the
290310773Stuexen				 * CMT policy for choosing an alternate net. */
291171440Srrs	else if (mode == 1) {
292171440Srrs		TAILQ_FOREACH(mnet, &stcb->asoc.nets, sctp_next) {
293171440Srrs			if (((mnet->dest_state & SCTP_ADDR_REACHABLE) != SCTP_ADDR_REACHABLE) ||
294214928Stuexen			    (mnet->dest_state & SCTP_ADDR_UNCONFIRMED)) {
295163953Srrs				/*
296163953Srrs				 * will skip ones that are not-reachable or
297163953Srrs				 * unconfirmed
298163953Srrs				 */
299163953Srrs				continue;
300163953Srrs			}
301171440Srrs			if (max_cwnd < mnet->cwnd) {
302171440Srrs				max_cwnd_net = mnet;
303171440Srrs				max_cwnd = mnet->cwnd;
304171440Srrs			} else if (max_cwnd == mnet->cwnd) {
305163953Srrs				uint32_t rndval;
306163953Srrs				uint8_t this_random;
307163953Srrs
308163953Srrs				if (stcb->asoc.hb_random_idx > 3) {
309163953Srrs					rndval = sctp_select_initial_TSN(&stcb->sctp_ep->sctp_ep);
310163953Srrs					memcpy(stcb->asoc.hb_random_values, &rndval,
311163953Srrs					    sizeof(stcb->asoc.hb_random_values));
312163953Srrs					this_random = stcb->asoc.hb_random_values[0];
313163953Srrs					stcb->asoc.hb_random_idx = 0;
314163953Srrs					stcb->asoc.hb_ect_randombit = 0;
315163953Srrs				} else {
316163953Srrs					this_random = stcb->asoc.hb_random_values[stcb->asoc.hb_random_idx];
317163953Srrs					stcb->asoc.hb_random_idx++;
318163953Srrs					stcb->asoc.hb_ect_randombit = 0;
319163953Srrs				}
320163953Srrs				if (this_random % 2) {
321171440Srrs					max_cwnd_net = mnet;
322171440Srrs					max_cwnd = mnet->cwnd;
323163953Srrs				}
324163953Srrs			}
325163953Srrs		}
326171440Srrs		if (max_cwnd_net) {
327171440Srrs			return (max_cwnd_net);
328163953Srrs		}
329163953Srrs	}
330163953Srrs	mnet = net;
331163953Srrs	once = 0;
332163953Srrs
333163953Srrs	if (mnet == NULL) {
334163953Srrs		mnet = TAILQ_FIRST(&stcb->asoc.nets);
335212225Srrs		if (mnet == NULL) {
336212225Srrs			return (NULL);
337212225Srrs		}
338163953Srrs	}
339277380Stuexen	for (;;) {
340163953Srrs		alt = TAILQ_NEXT(mnet, sctp_next);
341163953Srrs		if (alt == NULL) {
342163953Srrs			once++;
343163953Srrs			if (once > 1) {
344163953Srrs				break;
345163953Srrs			}
346163953Srrs			alt = TAILQ_FIRST(&stcb->asoc.nets);
347212225Srrs			if (alt == NULL) {
348212225Srrs				return (NULL);
349212225Srrs			}
350163953Srrs		}
351163953Srrs		if (alt->ro.ro_rt == NULL) {
352167598Srrs			if (alt->ro._s_addr) {
353167598Srrs				sctp_free_ifa(alt->ro._s_addr);
354167598Srrs				alt->ro._s_addr = NULL;
355167598Srrs			}
356163953Srrs			alt->src_addr_selected = 0;
357163953Srrs		}
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;
365277380Stuexen	}
366163953Srrs
367163953Srrs	if (alt == NULL) {
368163953Srrs		/* Case where NO insv network exists (dormant state) */
369163953Srrs		/* we rotate destinations */
370163953Srrs		once = 0;
371163953Srrs		mnet = net;
372277380Stuexen		for (;;) {
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);
383277380Stuexen				if (alt == NULL) {
384277380Stuexen					break;
385277380Stuexen				}
386163953Srrs			}
387163953Srrs			if ((!(alt->dest_state & SCTP_ADDR_UNCONFIRMED)) &&
388163953Srrs			    (alt != net)) {
389163953Srrs				/* Found an alternate address */
390163953Srrs				break;
391163953Srrs			}
392163953Srrs			mnet = alt;
393277380Stuexen		}
394163953Srrs	}
395163953Srrs	if (alt == NULL) {
396163953Srrs		return (net);
397163953Srrs	}
398163953Srrs	return (alt);
399163953Srrs}
400163953Srrs
401163953Srrsstatic void
402163953Srrssctp_backoff_on_timeout(struct sctp_tcb *stcb,
403163953Srrs    struct sctp_nets *net,
404163953Srrs    int win_probe,
405210599Srrs    int num_marked, int num_abandoned)
406163953Srrs{
407170642Srrs	if (net->RTO == 0) {
408289240Stuexen		if (net->RTO_measured) {
409289240Stuexen			net->RTO = stcb->asoc.minrto;
410289240Stuexen		} else {
411289240Stuexen			net->RTO = stcb->asoc.initial_rto;
412289240Stuexen		}
413170642Srrs	}
414163953Srrs	net->RTO <<= 1;
415163953Srrs	if (net->RTO > stcb->asoc.maxrto) {
416163953Srrs		net->RTO = stcb->asoc.maxrto;
417163953Srrs	}
418210599Srrs	if ((win_probe == 0) && (num_marked || num_abandoned)) {
419163953Srrs		/* We don't apply penalty to window probe scenarios */
420171440Srrs		/* JRS - Use the congestion control given in the CC module */
421171440Srrs		stcb->asoc.cc_functions.sctp_cwnd_update_after_timeout(stcb, net);
422163953Srrs	}
423163953Srrs}
424163953Srrs
425184333Srrs#ifndef INVARIANTS
426184333Srrsstatic void
427182367Srrssctp_recover_sent_list(struct sctp_tcb *stcb)
428182367Srrs{
429216822Stuexen	struct sctp_tmit_chunk *chk, *nchk;
430182367Srrs	struct sctp_association *asoc;
431182367Srrs
432182367Srrs	asoc = &stcb->asoc;
433216822Stuexen	TAILQ_FOREACH_SAFE(chk, &asoc->sent_queue, sctp_next, nchk) {
434310219Stuexen		if (SCTP_TSN_GE(asoc->last_acked_seq, chk->rec.data.tsn)) {
435182367Srrs			SCTP_PRINTF("Found chk:%p tsn:%x <= last_acked_seq:%x\n",
436310219Stuexen			    (void *)chk, chk->rec.data.tsn, asoc->last_acked_seq);
437243157Stuexen			if (chk->sent != SCTP_DATAGRAM_NR_ACKED) {
438310219Stuexen				if (asoc->strmout[chk->rec.data.sid].chunks_on_queues > 0) {
439310219Stuexen					asoc->strmout[chk->rec.data.sid].chunks_on_queues--;
440242714Stuexen				}
441242714Stuexen			}
442310219Stuexen			if ((asoc->strmout[chk->rec.data.sid].chunks_on_queues == 0) &&
443310219Stuexen			    (asoc->strmout[chk->rec.data.sid].state == SCTP_STREAM_RESET_PENDING) &&
444310219Stuexen			    TAILQ_EMPTY(&asoc->strmout[chk->rec.data.sid].outqueue)) {
445291700Stuexen				asoc->trigger_reset = 1;
446291700Stuexen			}
447182367Srrs			TAILQ_REMOVE(&asoc->sent_queue, chk, sctp_next);
448255190Stuexen			if (PR_SCTP_ENABLED(chk->flags)) {
449182367Srrs				if (asoc->pr_sctp_cnt != 0)
450182367Srrs					asoc->pr_sctp_cnt--;
451182367Srrs			}
452182367Srrs			if (chk->data) {
453182367Srrs				/* sa_ignore NO_NULL_CHK */
454182367Srrs				sctp_free_bufspace(stcb, asoc, chk, 1);
455182367Srrs				sctp_m_freem(chk->data);
456216822Stuexen				chk->data = NULL;
457269448Stuexen				if (asoc->prsctp_supported && PR_SCTP_BUF_ENABLED(chk->flags)) {
458182367Srrs					asoc->sent_queue_cnt_removeable--;
459182367Srrs				}
460182367Srrs			}
461182367Srrs			asoc->sent_queue_cnt--;
462221627Stuexen			sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
463182367Srrs		}
464182367Srrs	}
465182367Srrs	SCTP_PRINTF("after recover order is as follows\n");
466216822Stuexen	TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
467310219Stuexen		SCTP_PRINTF("chk:%p TSN:%x\n", (void *)chk, chk->rec.data.tsn);
468182367Srrs	}
469182367Srrs}
470184333Srrs#endif
471184333Srrs
472163953Srrsstatic int
473163953Srrssctp_mark_all_for_resend(struct sctp_tcb *stcb,
474163953Srrs    struct sctp_nets *net,
475163953Srrs    struct sctp_nets *alt,
476163953Srrs    int window_probe,
477210599Srrs    int *num_marked,
478210599Srrs    int *num_abandoned)
479163953Srrs{
480163953Srrs
481163953Srrs	/*
482163953Srrs	 * Mark all chunks (well not all) that were sent to *net for
483163953Srrs	 * retransmission. Move them to alt for there destination as well...
484163953Srrs	 * We only mark chunks that have been outstanding long enough to
485163953Srrs	 * have received feed-back.
486163953Srrs	 */
487216822Stuexen	struct sctp_tmit_chunk *chk, *nchk;
488163953Srrs	struct sctp_nets *lnets;
489163953Srrs	struct timeval now, min_wait, tv;
490219014Stuexen	int cur_rto;
491210599Srrs	int cnt_abandoned;
492168709Srrs	int audit_tf, num_mk, fir;
493163953Srrs	unsigned int cnt_mk;
494168709Srrs	uint32_t orig_flight, orig_tf;
495163953Srrs	uint32_t tsnlast, tsnfirst;
496182367Srrs	int recovery_cnt = 0;
497163953Srrs
498171440Srrs
499163953Srrs	/* none in flight now */
500163953Srrs	audit_tf = 0;
501163953Srrs	fir = 0;
502163953Srrs	/*
503163953Srrs	 * figure out how long a data chunk must be pending before we can
504163953Srrs	 * mark it ..
505163953Srrs	 */
506169378Srrs	(void)SCTP_GETTIME_TIMEVAL(&now);
507163953Srrs	/* get cur rto in micro-seconds */
508219014Stuexen	cur_rto = (net->lastsa >> SCTP_RTT_SHIFT) + net->lastsv;
509219014Stuexen	cur_rto *= 1000;
510224641Stuexen	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
511219014Stuexen		sctp_log_fr(cur_rto,
512170744Srrs		    stcb->asoc.peers_rwnd,
513170744Srrs		    window_probe,
514170744Srrs		    SCTP_FR_T3_MARK_TIME);
515224641Stuexen		sctp_log_fr(net->flight_size, 0, 0, SCTP_FR_CWND_REPORT);
516170744Srrs		sctp_log_fr(net->flight_size, net->cwnd, stcb->asoc.total_flight, SCTP_FR_CWND_REPORT);
517170744Srrs	}
518219014Stuexen	tv.tv_sec = cur_rto / 1000000;
519219014Stuexen	tv.tv_usec = cur_rto % 1000000;
520163953Srrs	min_wait = now;
521163953Srrs	timevalsub(&min_wait, &tv);
522163953Srrs	if (min_wait.tv_sec < 0 || min_wait.tv_usec < 0) {
523163953Srrs		/*
524163953Srrs		 * if we hit here, we don't have enough seconds on the clock
525163953Srrs		 * to account for the RTO. We just let the lower seconds be
526163953Srrs		 * the bounds and don't worry about it. This may mean we
527163953Srrs		 * will mark a lot more than we should.
528163953Srrs		 */
529163953Srrs		min_wait.tv_sec = min_wait.tv_usec = 0;
530163953Srrs	}
531224641Stuexen	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
532219014Stuexen		sctp_log_fr(cur_rto, now.tv_sec, now.tv_usec, SCTP_FR_T3_MARK_TIME);
533170744Srrs		sctp_log_fr(0, min_wait.tv_sec, min_wait.tv_usec, SCTP_FR_T3_MARK_TIME);
534170744Srrs	}
535163953Srrs	/*
536163953Srrs	 * Our rwnd will be incorrect here since we are not adding back the
537163953Srrs	 * cnt * mbuf but we will fix that down below.
538163953Srrs	 */
539163953Srrs	orig_flight = net->flight_size;
540168709Srrs	orig_tf = stcb->asoc.total_flight;
541168709Srrs
542163953Srrs	net->fast_retran_ip = 0;
543163953Srrs	/* Now on to each chunk */
544210599Srrs	cnt_abandoned = 0;
545163953Srrs	num_mk = cnt_mk = 0;
546163953Srrs	tsnfirst = tsnlast = 0;
547184333Srrs#ifndef INVARIANTS
548182367Srrsstart_again:
549184333Srrs#endif
550216822Stuexen	TAILQ_FOREACH_SAFE(chk, &stcb->asoc.sent_queue, sctp_next, nchk) {
551310219Stuexen		if (SCTP_TSN_GE(stcb->asoc.last_acked_seq, chk->rec.data.tsn)) {
552163953Srrs			/* Strange case our list got out of order? */
553257359Stuexen			SCTP_PRINTF("Our list is out of order? last_acked:%x chk:%x\n",
554310219Stuexen			    (unsigned int)stcb->asoc.last_acked_seq, (unsigned int)chk->rec.data.tsn);
555182367Srrs			recovery_cnt++;
556182367Srrs#ifdef INVARIANTS
557182367Srrs			panic("last acked >= chk on sent-Q");
558182367Srrs#else
559182367Srrs			SCTP_PRINTF("Recover attempts a restart cnt:%d\n", recovery_cnt);
560182367Srrs			sctp_recover_sent_list(stcb);
561182367Srrs			if (recovery_cnt < 10) {
562182367Srrs				goto start_again;
563182367Srrs			} else {
564182367Srrs				SCTP_PRINTF("Recovery fails %d times??\n", recovery_cnt);
565182367Srrs			}
566182367Srrs#endif
567163953Srrs		}
568163953Srrs		if ((chk->whoTo == net) && (chk->sent < SCTP_DATAGRAM_ACKED)) {
569163953Srrs			/*
570163953Srrs			 * found one to mark: If it is less than
571163953Srrs			 * DATAGRAM_ACKED it MUST not be a skipped or marked
572163953Srrs			 * TSN but instead one that is either already set
573163953Srrs			 * for retransmission OR one that needs
574163953Srrs			 * retransmission.
575163953Srrs			 */
576163953Srrs
577163953Srrs			/* validate its been outstanding long enough */
578224641Stuexen			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
579310219Stuexen				sctp_log_fr(chk->rec.data.tsn,
580170744Srrs				    chk->sent_rcv_time.tv_sec,
581170744Srrs				    chk->sent_rcv_time.tv_usec,
582170744Srrs				    SCTP_FR_T3_MARK_TIME);
583170744Srrs			}
584163953Srrs			if ((chk->sent_rcv_time.tv_sec > min_wait.tv_sec) && (window_probe == 0)) {
585163953Srrs				/*
586163953Srrs				 * we have reached a chunk that was sent
587163953Srrs				 * some seconds past our min.. forget it we
588163953Srrs				 * will find no more to send.
589163953Srrs				 */
590224641Stuexen				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
591170744Srrs					sctp_log_fr(0,
592170744Srrs					    chk->sent_rcv_time.tv_sec,
593170744Srrs					    chk->sent_rcv_time.tv_usec,
594170744Srrs					    SCTP_FR_T3_STOPPED);
595170744Srrs				}
596163953Srrs				continue;
597163953Srrs			} else if ((chk->sent_rcv_time.tv_sec == min_wait.tv_sec) &&
598163953Srrs			    (window_probe == 0)) {
599163953Srrs				/*
600163953Srrs				 * we must look at the micro seconds to
601163953Srrs				 * know.
602163953Srrs				 */
603163953Srrs				if (chk->sent_rcv_time.tv_usec >= min_wait.tv_usec) {
604163953Srrs					/*
605163953Srrs					 * ok it was sent after our boundary
606163953Srrs					 * time.
607163953Srrs					 */
608163953Srrs					continue;
609163953Srrs				}
610163953Srrs			}
611269448Stuexen			if (stcb->asoc.prsctp_supported && PR_SCTP_TTL_ENABLED(chk->flags)) {
612163953Srrs				/* Is it expired? */
613212801Stuexen				if (timevalcmp(&now, &chk->rec.data.timetodrop, >)) {
614163953Srrs					/* Yes so drop it */
615163953Srrs					if (chk->data) {
616169420Srrs						(void)sctp_release_pr_sctp_chunk(stcb,
617163953Srrs						    chk,
618235416Stuexen						    1,
619189790Srrs						    SCTP_SO_NOT_LOCKED);
620210599Srrs						cnt_abandoned++;
621163953Srrs					}
622185694Srrs					continue;
623163953Srrs				}
624163953Srrs			}
625269448Stuexen			if (stcb->asoc.prsctp_supported && PR_SCTP_RTX_ENABLED(chk->flags)) {
626163953Srrs				/* Has it been retransmitted tv_sec times? */
627163953Srrs				if (chk->snd_count > chk->rec.data.timetodrop.tv_sec) {
628163953Srrs					if (chk->data) {
629169420Srrs						(void)sctp_release_pr_sctp_chunk(stcb,
630163953Srrs						    chk,
631235416Stuexen						    1,
632189790Srrs						    SCTP_SO_NOT_LOCKED);
633210599Srrs						cnt_abandoned++;
634163953Srrs					}
635185694Srrs					continue;
636163953Srrs				}
637163953Srrs			}
638168709Srrs			if (chk->sent < SCTP_DATAGRAM_RESEND) {
639163953Srrs				sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
640163953Srrs				num_mk++;
641163953Srrs				if (fir == 0) {
642163953Srrs					fir = 1;
643310219Stuexen					tsnfirst = chk->rec.data.tsn;
644163953Srrs				}
645310219Stuexen				tsnlast = chk->rec.data.tsn;
646224641Stuexen				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
647310219Stuexen					sctp_log_fr(chk->rec.data.tsn, chk->snd_count,
648170744Srrs					    0, SCTP_FR_T3_MARKED);
649170744Srrs				}
650347154Stuexen
651168709Srrs				if (chk->rec.data.chunk_was_revoked) {
652168709Srrs					/* deflate the cwnd */
653168709Srrs					chk->whoTo->cwnd -= chk->book_size;
654168709Srrs					chk->rec.data.chunk_was_revoked = 0;
655168709Srrs				}
656168709Srrs				net->marked_retrans++;
657168709Srrs				stcb->asoc.marked_retrans++;
658179783Srrs				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
659170744Srrs					sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_RSND_TO,
660170744Srrs					    chk->whoTo->flight_size,
661170744Srrs					    chk->book_size,
662310773Stuexen					    (uint32_t)(uintptr_t)chk->whoTo,
663310219Stuexen					    chk->rec.data.tsn);
664170744Srrs				}
665168709Srrs				sctp_flight_size_decrease(chk);
666168709Srrs				sctp_total_flight_decrease(stcb, chk);
667168709Srrs				stcb->asoc.peers_rwnd += chk->send_size;
668179783Srrs				stcb->asoc.peers_rwnd += SCTP_BASE_SYSCTL(sctp_peer_chunk_oh);
669163953Srrs			}
670163953Srrs			chk->sent = SCTP_DATAGRAM_RESEND;
671319407Stuexen			chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
672163953Srrs			SCTP_STAT_INCR(sctps_markedretrans);
673165220Srrs
674163953Srrs			/* reset the TSN for striking and other FR stuff */
675163953Srrs			chk->rec.data.doing_fast_retransmit = 0;
676163953Srrs			/* Clear any time so NO RTT is being done */
677219397Srrs
678219397Srrs			if (chk->do_rtt) {
679219397Srrs				if (chk->whoTo->rto_needed == 0) {
680219397Srrs					chk->whoTo->rto_needed = 1;
681219397Srrs				}
682219397Srrs			}
683163953Srrs			chk->do_rtt = 0;
684163953Srrs			if (alt != net) {
685163953Srrs				sctp_free_remote_addr(chk->whoTo);
686163953Srrs				chk->no_fr_allowed = 1;
687163953Srrs				chk->whoTo = alt;
688163953Srrs				atomic_add_int(&alt->ref_count, 1);
689163953Srrs			} else {
690163953Srrs				chk->no_fr_allowed = 0;
691163953Srrs				if (TAILQ_EMPTY(&stcb->asoc.send_queue)) {
692163953Srrs					chk->rec.data.fast_retran_tsn = stcb->asoc.sending_seq;
693163953Srrs				} else {
694310219Stuexen					chk->rec.data.fast_retran_tsn = (TAILQ_FIRST(&stcb->asoc.send_queue))->rec.data.tsn;
695163953Srrs				}
696163953Srrs			}
697170181Srrs			/*
698170181Srrs			 * CMT: Do not allow FRs on retransmitted TSNs.
699170181Srrs			 */
700216669Stuexen			if (stcb->asoc.sctp_cmt_on_off > 0) {
701163953Srrs				chk->no_fr_allowed = 1;
702163953Srrs			}
703210599Srrs#ifdef THIS_SHOULD_NOT_BE_DONE
704163953Srrs		} else if (chk->sent == SCTP_DATAGRAM_ACKED) {
705163953Srrs			/* remember highest acked one */
706163953Srrs			could_be_sent = chk;
707210599Srrs#endif
708163953Srrs		}
709163953Srrs		if (chk->sent == SCTP_DATAGRAM_RESEND) {
710163953Srrs			cnt_mk++;
711163953Srrs		}
712163953Srrs	}
713168709Srrs	if ((orig_flight - net->flight_size) != (orig_tf - stcb->asoc.total_flight)) {
714168709Srrs		/* we did not subtract the same things? */
715168709Srrs		audit_tf = 1;
716168709Srrs	}
717347154Stuexen
718224641Stuexen	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
719170744Srrs		sctp_log_fr(tsnfirst, tsnlast, num_mk, SCTP_FR_T3_TIMEOUT);
720170744Srrs	}
721163953Srrs#ifdef SCTP_DEBUG
722169420Srrs	if (num_mk) {
723169420Srrs		SCTPDBG(SCTP_DEBUG_TIMER1, "LAST TSN marked was %x\n",
724169420Srrs		    tsnlast);
725319394Stuexen		SCTPDBG(SCTP_DEBUG_TIMER1, "Num marked for retransmission was %d peer-rwd:%u\n",
726169420Srrs		    num_mk,
727319394Stuexen		    stcb->asoc.peers_rwnd);
728163953Srrs	}
729163953Srrs#endif
730163953Srrs	*num_marked = num_mk;
731210599Srrs	*num_abandoned = cnt_abandoned;
732210493Srrs	/*
733210493Srrs	 * Now check for a ECN Echo that may be stranded And include the
734210493Srrs	 * cnt_mk'd to have all resends in the control queue.
735210493Srrs	 */
736210493Srrs	TAILQ_FOREACH(chk, &stcb->asoc.control_send_queue, sctp_next) {
737210493Srrs		if (chk->sent == SCTP_DATAGRAM_RESEND) {
738210493Srrs			cnt_mk++;
739210493Srrs		}
740210493Srrs		if ((chk->whoTo == net) &&
741210493Srrs		    (chk->rec.chunk_id.id == SCTP_ECN_ECHO)) {
742210493Srrs			sctp_free_remote_addr(chk->whoTo);
743210493Srrs			chk->whoTo = alt;
744210493Srrs			if (chk->sent != SCTP_DATAGRAM_RESEND) {
745210493Srrs				chk->sent = SCTP_DATAGRAM_RESEND;
746319407Stuexen				chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
747210493Srrs				sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
748210493Srrs				cnt_mk++;
749210493Srrs			}
750210493Srrs			atomic_add_int(&alt->ref_count, 1);
751210493Srrs		}
752210493Srrs	}
753210599Srrs#ifdef THIS_SHOULD_NOT_BE_DONE
754163953Srrs	if ((stcb->asoc.sent_queue_retran_cnt == 0) && (could_be_sent)) {
755163953Srrs		/* fix it so we retransmit the highest acked anyway */
756163953Srrs		sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
757163953Srrs		cnt_mk++;
758163953Srrs		could_be_sent->sent = SCTP_DATAGRAM_RESEND;
759163953Srrs	}
760210599Srrs#endif
761163953Srrs	if (stcb->asoc.sent_queue_retran_cnt != cnt_mk) {
762165220Srrs#ifdef INVARIANTS
763171477Srrs		SCTP_PRINTF("Local Audit says there are %d for retran asoc cnt:%d we marked:%d this time\n",
764171477Srrs		    cnt_mk, stcb->asoc.sent_queue_retran_cnt, num_mk);
765163953Srrs#endif
766163953Srrs#ifndef SCTP_AUDITING_ENABLED
767163953Srrs		stcb->asoc.sent_queue_retran_cnt = cnt_mk;
768163953Srrs#endif
769163953Srrs	}
770163953Srrs	if (audit_tf) {
771169420Srrs		SCTPDBG(SCTP_DEBUG_TIMER4,
772169420Srrs		    "Audit total flight due to negative value net:%p\n",
773240148Stuexen		    (void *)net);
774163953Srrs		stcb->asoc.total_flight = 0;
775163953Srrs		stcb->asoc.total_flight_count = 0;
776163953Srrs		/* Clear all networks flight size */
777163953Srrs		TAILQ_FOREACH(lnets, &stcb->asoc.nets, sctp_next) {
778163953Srrs			lnets->flight_size = 0;
779169420Srrs			SCTPDBG(SCTP_DEBUG_TIMER4,
780169420Srrs			    "Net:%p c-f cwnd:%d ssthresh:%d\n",
781240148Stuexen			    (void *)lnets, lnets->cwnd, lnets->ssthresh);
782163953Srrs		}
783163953Srrs		TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
784163953Srrs			if (chk->sent < SCTP_DATAGRAM_RESEND) {
785179783Srrs				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
786170744Srrs					sctp_misc_ints(SCTP_FLIGHT_LOG_UP,
787170744Srrs					    chk->whoTo->flight_size,
788170744Srrs					    chk->book_size,
789310773Stuexen					    (uint32_t)(uintptr_t)chk->whoTo,
790310219Stuexen					    chk->rec.data.tsn);
791170744Srrs				}
792347154Stuexen
793168709Srrs				sctp_flight_size_increase(chk);
794168709Srrs				sctp_total_flight_increase(stcb, chk);
795163953Srrs			}
796163953Srrs		}
797163953Srrs	}
798163953Srrs	/* We return 1 if we only have a window probe outstanding */
799163953Srrs	return (0);
800163953Srrs}
801163953Srrs
802163953Srrs
803163953Srrsint
804163953Srrssctp_t3rxt_timer(struct sctp_inpcb *inp,
805163953Srrs    struct sctp_tcb *stcb,
806163953Srrs    struct sctp_nets *net)
807163953Srrs{
808163953Srrs	struct sctp_nets *alt;
809210599Srrs	int win_probe, num_mk, num_abandoned;
810163953Srrs
811179783Srrs	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
812170744Srrs		sctp_log_fr(0, 0, 0, SCTP_FR_T3_TIMEOUT);
813170744Srrs	}
814179783Srrs	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
815163953Srrs		struct sctp_nets *lnet;
816163953Srrs
817163953Srrs		TAILQ_FOREACH(lnet, &stcb->asoc.nets, sctp_next) {
818163953Srrs			if (net == lnet) {
819163953Srrs				sctp_log_cwnd(stcb, lnet, 1, SCTP_CWND_LOG_FROM_T3);
820163953Srrs			} else {
821163953Srrs				sctp_log_cwnd(stcb, lnet, 0, SCTP_CWND_LOG_FROM_T3);
822163953Srrs			}
823163953Srrs		}
824163953Srrs	}
825163953Srrs	/* Find an alternate and mark those for retransmission */
826163953Srrs	if ((stcb->asoc.peers_rwnd == 0) &&
827163953Srrs	    (stcb->asoc.total_flight < net->mtu)) {
828163953Srrs		SCTP_STAT_INCR(sctps_timowindowprobe);
829163953Srrs		win_probe = 1;
830163953Srrs	} else {
831163953Srrs		win_probe = 0;
832163953Srrs	}
833168709Srrs
834163953Srrs	if (win_probe == 0) {
835163953Srrs		/* We don't do normal threshold management on window probes */
836163953Srrs		if (sctp_threshold_management(inp, stcb, net,
837163953Srrs		    stcb->asoc.max_send_times)) {
838163953Srrs			/* Association was destroyed */
839163953Srrs			return (1);
840163953Srrs		} else {
841163953Srrs			if (net != stcb->asoc.primary_destination) {
842163953Srrs				/* send a immediate HB if our RTO is stale */
843163953Srrs				struct timeval now;
844163953Srrs				unsigned int ms_goneby;
845163953Srrs
846169378Srrs				(void)SCTP_GETTIME_TIMEVAL(&now);
847163953Srrs				if (net->last_sent_time.tv_sec) {
848163953Srrs					ms_goneby = (now.tv_sec - net->last_sent_time.tv_sec) * 1000;
849163953Srrs				} else {
850163953Srrs					ms_goneby = 0;
851163953Srrs				}
852224641Stuexen				if ((net->dest_state & SCTP_ADDR_PF) == 0) {
853224641Stuexen					if ((ms_goneby > net->RTO) || (net->RTO == 0)) {
854185694Srrs						/*
855224641Stuexen						 * no recent feed back in an
856224641Stuexen						 * RTO or more, request a
857224641Stuexen						 * RTT update
858185694Srrs						 */
859224641Stuexen						sctp_send_hb(stcb, net, SCTP_SO_NOT_LOCKED);
860224641Stuexen					}
861163953Srrs				}
862163953Srrs			}
863163953Srrs		}
864163953Srrs	} else {
865163953Srrs		/*
866163953Srrs		 * For a window probe we don't penalize the net's but only
867163953Srrs		 * the association. This may fail it if SACKs are not coming
868163953Srrs		 * back. If sack's are coming with rwnd locked at 0, we will
869163953Srrs		 * continue to hold things waiting for rwnd to raise
870163953Srrs		 */
871163953Srrs		if (sctp_threshold_management(inp, stcb, NULL,
872163953Srrs		    stcb->asoc.max_send_times)) {
873163953Srrs			/* Association was destroyed */
874163953Srrs			return (1);
875163953Srrs		}
876163953Srrs	}
877224641Stuexen	if (stcb->asoc.sctp_cmt_on_off > 0) {
878224641Stuexen		if (net->pf_threshold < net->failure_threshold) {
879224641Stuexen			alt = sctp_find_alternate_net(stcb, net, 2);
880224641Stuexen		} else {
881224641Stuexen			/*
882224641Stuexen			 * CMT: Using RTX_SSTHRESH policy for CMT. If CMT is
883224641Stuexen			 * being used, then pick dest with largest ssthresh
884224641Stuexen			 * for any retransmission.
885224641Stuexen			 */
886224641Stuexen			alt = sctp_find_alternate_net(stcb, net, 1);
887224641Stuexen			/*
888224641Stuexen			 * CUCv2: If a different dest is picked for the
889224641Stuexen			 * retransmission, then new (rtx-)pseudo_cumack
890224641Stuexen			 * needs to be tracked for orig dest. Let CUCv2
891224641Stuexen			 * track new (rtx-) pseudo-cumack always.
892224641Stuexen			 */
893224641Stuexen			net->find_pseudo_cumack = 1;
894224641Stuexen			net->find_rtx_pseudo_cumack = 1;
895224641Stuexen		}
896224641Stuexen	} else {
897224641Stuexen		alt = sctp_find_alternate_net(stcb, net, 0);
898224641Stuexen	}
899224641Stuexen
900224641Stuexen	num_mk = 0;
901224641Stuexen	num_abandoned = 0;
902224641Stuexen	(void)sctp_mark_all_for_resend(stcb, net, alt, win_probe,
903224641Stuexen	    &num_mk, &num_abandoned);
904224641Stuexen	/* FR Loss recovery just ended with the T3. */
905224641Stuexen	stcb->asoc.fast_retran_loss_recovery = 0;
906224641Stuexen
907224641Stuexen	/* CMT FR loss recovery ended with the T3 */
908224641Stuexen	net->fast_retran_loss_recovery = 0;
909224641Stuexen	if ((stcb->asoc.cc_functions.sctp_cwnd_new_transmission_begins) &&
910224641Stuexen	    (net->flight_size == 0)) {
911224641Stuexen		(*stcb->asoc.cc_functions.sctp_cwnd_new_transmission_begins) (stcb, net);
912224641Stuexen	}
913347154Stuexen
914224641Stuexen	/*
915224641Stuexen	 * setup the sat loss recovery that prevents satellite cwnd advance.
916224641Stuexen	 */
917224641Stuexen	stcb->asoc.sat_t3_loss_recovery = 1;
918224641Stuexen	stcb->asoc.sat_t3_recovery_tsn = stcb->asoc.sending_seq;
919224641Stuexen
920224641Stuexen	/* Backoff the timer and cwnd */
921224641Stuexen	sctp_backoff_on_timeout(stcb, net, win_probe, num_mk, num_abandoned);
922224641Stuexen	if ((!(net->dest_state & SCTP_ADDR_REACHABLE)) ||
923224641Stuexen	    (net->dest_state & SCTP_ADDR_PF)) {
924163953Srrs		/* Move all pending over too */
925212712Stuexen		sctp_move_chunks_from_net(stcb, net);
926169352Srrs
927169352Srrs		/*
928169352Srrs		 * Get the address that failed, to force a new src address
929169352Srrs		 * selecton and a route allocation.
930169352Srrs		 */
931169352Srrs		if (net->ro._s_addr) {
932169352Srrs			sctp_free_ifa(net->ro._s_addr);
933169352Srrs			net->ro._s_addr = NULL;
934169352Srrs		}
935169352Srrs		net->src_addr_selected = 0;
936169352Srrs
937169352Srrs		/* Force a route allocation too */
938169352Srrs		if (net->ro.ro_rt) {
939169352Srrs			RTFREE(net->ro.ro_rt);
940169352Srrs			net->ro.ro_rt = NULL;
941169352Srrs		}
942347154Stuexen
943163953Srrs		/* Was it our primary? */
944163953Srrs		if ((stcb->asoc.primary_destination == net) && (alt != net)) {
945163953Srrs			/*
946163953Srrs			 * Yes, note it as such and find an alternate note:
947163953Srrs			 * this means HB code must use this to resent the
948163953Srrs			 * primary if it goes active AND if someone does a
949163953Srrs			 * change-primary then this flag must be cleared
950163953Srrs			 * from any net structures.
951163953Srrs			 */
952224641Stuexen			if (stcb->asoc.alternate) {
953224641Stuexen				sctp_free_remote_addr(stcb->asoc.alternate);
954163953Srrs			}
955224641Stuexen			stcb->asoc.alternate = alt;
956224641Stuexen			atomic_add_int(&stcb->asoc.alternate->ref_count, 1);
957163953Srrs		}
958163953Srrs	}
959163953Srrs	/*
960163953Srrs	 * Special case for cookie-echo'ed case, we don't do output but must
961163953Srrs	 * await the COOKIE-ACK before retransmission
962163953Srrs	 */
963347165Stuexen	if (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED) {
964163953Srrs		/*
965163953Srrs		 * Here we just reset the timer and start again since we
966163953Srrs		 * have not established the asoc
967163953Srrs		 */
968163953Srrs		sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
969163953Srrs		return (0);
970163953Srrs	}
971269448Stuexen	if (stcb->asoc.prsctp_supported) {
972163953Srrs		struct sctp_tmit_chunk *lchk;
973163953Srrs
974163953Srrs		lchk = sctp_try_advance_peer_ack_point(stcb, &stcb->asoc);
975163953Srrs		/* C3. See if we need to send a Fwd-TSN */
976216825Stuexen		if (SCTP_TSN_GT(stcb->asoc.advanced_peer_ack_point, stcb->asoc.last_acked_seq)) {
977163953Srrs			send_forward_tsn(stcb, &stcb->asoc);
978361471Stuexen			for (; lchk != NULL; lchk = TAILQ_NEXT(lchk, sctp_next)) {
979361471Stuexen				if (lchk->whoTo != NULL) {
980361471Stuexen					break;
981361471Stuexen				}
982361471Stuexen			}
983361471Stuexen			if (lchk != NULL) {
984163953Srrs				/* Assure a timer is up */
985163953Srrs				sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, stcb, lchk->whoTo);
986163953Srrs			}
987163953Srrs		}
988163953Srrs	}
989179783Srrs	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_MONITOR_ENABLE) {
990170744Srrs		sctp_log_cwnd(stcb, net, net->cwnd, SCTP_CWND_LOG_FROM_RTX);
991170744Srrs	}
992163953Srrs	return (0);
993163953Srrs}
994163953Srrs
995163953Srrsint
996163953Srrssctp_t1init_timer(struct sctp_inpcb *inp,
997163953Srrs    struct sctp_tcb *stcb,
998163953Srrs    struct sctp_nets *net)
999163953Srrs{
1000163953Srrs	/* bump the thresholds */
1001163953Srrs	if (stcb->asoc.delayed_connection) {
1002163953Srrs		/*
1003163953Srrs		 * special hook for delayed connection. The library did NOT
1004163953Srrs		 * complete the rest of its sends.
1005163953Srrs		 */
1006163953Srrs		stcb->asoc.delayed_connection = 0;
1007172090Srrs		sctp_send_initiate(inp, stcb, SCTP_SO_NOT_LOCKED);
1008163953Srrs		return (0);
1009163953Srrs	}
1010347165Stuexen	if (SCTP_GET_STATE(stcb) != SCTP_STATE_COOKIE_WAIT) {
1011163953Srrs		return (0);
1012163953Srrs	}
1013163953Srrs	if (sctp_threshold_management(inp, stcb, net,
1014163953Srrs	    stcb->asoc.max_init_times)) {
1015163953Srrs		/* Association was destroyed */
1016163953Srrs		return (1);
1017163953Srrs	}
1018163953Srrs	stcb->asoc.dropped_special_cnt = 0;
1019210599Srrs	sctp_backoff_on_timeout(stcb, stcb->asoc.primary_destination, 1, 0, 0);
1020163953Srrs	if (stcb->asoc.initial_init_rto_max < net->RTO) {
1021163953Srrs		net->RTO = stcb->asoc.initial_init_rto_max;
1022163953Srrs	}
1023163953Srrs	if (stcb->asoc.numnets > 1) {
1024163953Srrs		/* If we have more than one addr use it */
1025163953Srrs		struct sctp_nets *alt;
1026163953Srrs
1027163953Srrs		alt = sctp_find_alternate_net(stcb, stcb->asoc.primary_destination, 0);
1028214928Stuexen		if (alt != stcb->asoc.primary_destination) {
1029212712Stuexen			sctp_move_chunks_from_net(stcb, stcb->asoc.primary_destination);
1030163953Srrs			stcb->asoc.primary_destination = alt;
1031163953Srrs		}
1032163953Srrs	}
1033163953Srrs	/* Send out a new init */
1034172090Srrs	sctp_send_initiate(inp, stcb, SCTP_SO_NOT_LOCKED);
1035163953Srrs	return (0);
1036163953Srrs}
1037163953Srrs
1038163953Srrs/*
1039163953Srrs * For cookie and asconf we actually need to find and mark for resend, then
1040163953Srrs * increment the resend counter (after all the threshold management stuff of
1041163953Srrs * course).
1042163953Srrs */
1043163953Srrsint
1044163953Srrssctp_cookie_timer(struct sctp_inpcb *inp,
1045163953Srrs    struct sctp_tcb *stcb,
1046228653Stuexen    struct sctp_nets *net SCTP_UNUSED)
1047163953Srrs{
1048163953Srrs	struct sctp_nets *alt;
1049163953Srrs	struct sctp_tmit_chunk *cookie;
1050163953Srrs
1051163953Srrs	/* first before all else we must find the cookie */
1052163953Srrs	TAILQ_FOREACH(cookie, &stcb->asoc.control_send_queue, sctp_next) {
1053163953Srrs		if (cookie->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
1054163953Srrs			break;
1055163953Srrs		}
1056163953Srrs	}
1057163953Srrs	if (cookie == NULL) {
1058347165Stuexen		if (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED) {
1059163953Srrs			/* FOOBAR! */
1060263237Stuexen			struct mbuf *op_err;
1061163953Srrs
1062285925Stuexen			op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
1063263237Stuexen			    "Cookie timer expired, but no cookie");
1064283650Stuexen			inp->last_abort_code = SCTP_FROM_SCTP_TIMER + SCTP_LOC_3;
1065263237Stuexen			sctp_abort_an_association(inp, stcb, op_err, SCTP_SO_NOT_LOCKED);
1066163953Srrs		} else {
1067165220Srrs#ifdef INVARIANTS
1068163953Srrs			panic("Cookie timer expires in wrong state?");
1069163953Srrs#else
1070347165Stuexen			SCTP_PRINTF("Strange in state %d not cookie-echoed yet c-e timer expires?\n", SCTP_GET_STATE(stcb));
1071163953Srrs			return (0);
1072163953Srrs#endif
1073163953Srrs		}
1074163953Srrs		return (0);
1075163953Srrs	}
1076163953Srrs	/* Ok we found the cookie, threshold management next */
1077163953Srrs	if (sctp_threshold_management(inp, stcb, cookie->whoTo,
1078163953Srrs	    stcb->asoc.max_init_times)) {
1079163953Srrs		/* Assoc is over */
1080163953Srrs		return (1);
1081163953Srrs	}
1082163953Srrs	/*
1083299273Stuexen	 * Cleared threshold management, now lets backoff the address and
1084299273Stuexen	 * select an alternate
1085163953Srrs	 */
1086163953Srrs	stcb->asoc.dropped_special_cnt = 0;
1087210599Srrs	sctp_backoff_on_timeout(stcb, cookie->whoTo, 1, 0, 0);
1088163953Srrs	alt = sctp_find_alternate_net(stcb, cookie->whoTo, 0);
1089163953Srrs	if (alt != cookie->whoTo) {
1090163953Srrs		sctp_free_remote_addr(cookie->whoTo);
1091163953Srrs		cookie->whoTo = alt;
1092163953Srrs		atomic_add_int(&alt->ref_count, 1);
1093163953Srrs	}
1094163953Srrs	/* Now mark the retran info */
1095163953Srrs	if (cookie->sent != SCTP_DATAGRAM_RESEND) {
1096163953Srrs		sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
1097163953Srrs	}
1098163953Srrs	cookie->sent = SCTP_DATAGRAM_RESEND;
1099319407Stuexen	cookie->flags |= CHUNK_FLAGS_FRAGMENT_OK;
1100163953Srrs	/*
1101163953Srrs	 * Now call the output routine to kick out the cookie again, Note we
1102163953Srrs	 * don't mark any chunks for retran so that FR will need to kick in
1103163953Srrs	 * to move these (or a send timer).
1104163953Srrs	 */
1105163953Srrs	return (0);
1106163953Srrs}
1107163953Srrs
1108163953Srrsint
1109163953Srrssctp_strreset_timer(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
1110163953Srrs    struct sctp_nets *net)
1111163953Srrs{
1112163953Srrs	struct sctp_nets *alt;
1113163953Srrs	struct sctp_tmit_chunk *strrst = NULL, *chk = NULL;
1114163953Srrs
1115163953Srrs	if (stcb->asoc.stream_reset_outstanding == 0) {
1116163953Srrs		return (0);
1117163953Srrs	}
1118163953Srrs	/* find the existing STRRESET, we use the seq number we sent out on */
1119169420Srrs	(void)sctp_find_stream_reset(stcb, stcb->asoc.str_reset_seq_out, &strrst);
1120163953Srrs	if (strrst == NULL) {
1121163953Srrs		return (0);
1122163953Srrs	}
1123163953Srrs	/* do threshold management */
1124163953Srrs	if (sctp_threshold_management(inp, stcb, strrst->whoTo,
1125163953Srrs	    stcb->asoc.max_send_times)) {
1126163953Srrs		/* Assoc is over */
1127163953Srrs		return (1);
1128163953Srrs	}
1129163953Srrs	/*
1130299273Stuexen	 * Cleared threshold management, now lets backoff the address and
1131299273Stuexen	 * select an alternate
1132163953Srrs	 */
1133210599Srrs	sctp_backoff_on_timeout(stcb, strrst->whoTo, 1, 0, 0);
1134163953Srrs	alt = sctp_find_alternate_net(stcb, strrst->whoTo, 0);
1135163953Srrs	sctp_free_remote_addr(strrst->whoTo);
1136163953Srrs	strrst->whoTo = alt;
1137163953Srrs	atomic_add_int(&alt->ref_count, 1);
1138163953Srrs
1139163953Srrs	/* See if a ECN Echo is also stranded */
1140163953Srrs	TAILQ_FOREACH(chk, &stcb->asoc.control_send_queue, sctp_next) {
1141163953Srrs		if ((chk->whoTo == net) &&
1142163953Srrs		    (chk->rec.chunk_id.id == SCTP_ECN_ECHO)) {
1143163953Srrs			sctp_free_remote_addr(chk->whoTo);
1144163953Srrs			if (chk->sent != SCTP_DATAGRAM_RESEND) {
1145163953Srrs				chk->sent = SCTP_DATAGRAM_RESEND;
1146319407Stuexen				chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
1147163953Srrs				sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
1148163953Srrs			}
1149163953Srrs			chk->whoTo = alt;
1150163953Srrs			atomic_add_int(&alt->ref_count, 1);
1151163953Srrs		}
1152163953Srrs	}
1153224641Stuexen	if (!(net->dest_state & SCTP_ADDR_REACHABLE)) {
1154163953Srrs		/*
1155163953Srrs		 * If the address went un-reachable, we need to move to
1156163953Srrs		 * alternates for ALL chk's in queue
1157163953Srrs		 */
1158212712Stuexen		sctp_move_chunks_from_net(stcb, net);
1159163953Srrs	}
1160163953Srrs	/* mark the retran info */
1161163953Srrs	if (strrst->sent != SCTP_DATAGRAM_RESEND)
1162163953Srrs		sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
1163163953Srrs	strrst->sent = SCTP_DATAGRAM_RESEND;
1164319407Stuexen	strrst->flags |= CHUNK_FLAGS_FRAGMENT_OK;
1165163953Srrs
1166163953Srrs	/* restart the timer */
1167163953Srrs	sctp_timer_start(SCTP_TIMER_TYPE_STRRESET, inp, stcb, strrst->whoTo);
1168163953Srrs	return (0);
1169163953Srrs}
1170163953Srrs
1171163953Srrsint
1172163953Srrssctp_asconf_timer(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
1173163953Srrs    struct sctp_nets *net)
1174163953Srrs{
1175163953Srrs	struct sctp_nets *alt;
1176216822Stuexen	struct sctp_tmit_chunk *asconf, *chk;
1177163953Srrs
1178171572Srrs	/* is this a first send, or a retransmission? */
1179179157Srrs	if (TAILQ_EMPTY(&stcb->asoc.asconf_send_queue)) {
1180163953Srrs		/* compose a new ASCONF chunk and send it */
1181172190Srrs		sctp_send_asconf(stcb, net, SCTP_ADDR_NOT_LOCKED);
1182163953Srrs	} else {
1183171572Srrs		/*
1184171572Srrs		 * Retransmission of the existing ASCONF is needed
1185171572Srrs		 */
1186163953Srrs
1187163953Srrs		/* find the existing ASCONF */
1188179157Srrs		asconf = TAILQ_FIRST(&stcb->asoc.asconf_send_queue);
1189163953Srrs		if (asconf == NULL) {
1190163953Srrs			return (0);
1191163953Srrs		}
1192163953Srrs		/* do threshold management */
1193163953Srrs		if (sctp_threshold_management(inp, stcb, asconf->whoTo,
1194163953Srrs		    stcb->asoc.max_send_times)) {
1195163953Srrs			/* Assoc is over */
1196163953Srrs			return (1);
1197163953Srrs		}
1198163953Srrs		if (asconf->snd_count > stcb->asoc.max_send_times) {
1199163953Srrs			/*
1200171572Srrs			 * Something is rotten: our peer is not responding
1201171572Srrs			 * to ASCONFs but apparently is to other chunks.
1202171572Srrs			 * i.e. it is not properly handling the chunk type
1203171572Srrs			 * upper bits. Mark this peer as ASCONF incapable
1204171572Srrs			 * and cleanup.
1205163953Srrs			 */
1206169420Srrs			SCTPDBG(SCTP_DEBUG_TIMER1, "asconf_timer: Peer has not responded to our repeated ASCONFs\n");
1207163953Srrs			sctp_asconf_cleanup(stcb, net);
1208163953Srrs			return (0);
1209163953Srrs		}
1210163953Srrs		/*
1211171572Srrs		 * cleared threshold management, so now backoff the net and
1212171572Srrs		 * select an alternate
1213163953Srrs		 */
1214210599Srrs		sctp_backoff_on_timeout(stcb, asconf->whoTo, 1, 0, 0);
1215163953Srrs		alt = sctp_find_alternate_net(stcb, asconf->whoTo, 0);
1216179157Srrs		if (asconf->whoTo != alt) {
1217179157Srrs			sctp_free_remote_addr(asconf->whoTo);
1218179157Srrs			asconf->whoTo = alt;
1219179157Srrs			atomic_add_int(&alt->ref_count, 1);
1220179157Srrs		}
1221347154Stuexen
1222171572Srrs		/* See if an ECN Echo is also stranded */
1223163953Srrs		TAILQ_FOREACH(chk, &stcb->asoc.control_send_queue, sctp_next) {
1224163953Srrs			if ((chk->whoTo == net) &&
1225163953Srrs			    (chk->rec.chunk_id.id == SCTP_ECN_ECHO)) {
1226163953Srrs				sctp_free_remote_addr(chk->whoTo);
1227163953Srrs				chk->whoTo = alt;
1228163953Srrs				if (chk->sent != SCTP_DATAGRAM_RESEND) {
1229163953Srrs					chk->sent = SCTP_DATAGRAM_RESEND;
1230319407Stuexen					chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
1231163953Srrs					sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
1232163953Srrs				}
1233163953Srrs				atomic_add_int(&alt->ref_count, 1);
1234163953Srrs			}
1235163953Srrs		}
1236216822Stuexen		TAILQ_FOREACH(chk, &stcb->asoc.asconf_send_queue, sctp_next) {
1237179157Srrs			if (chk->whoTo != alt) {
1238179157Srrs				sctp_free_remote_addr(chk->whoTo);
1239179157Srrs				chk->whoTo = alt;
1240179157Srrs				atomic_add_int(&alt->ref_count, 1);
1241179157Srrs			}
1242179157Srrs			if (asconf->sent != SCTP_DATAGRAM_RESEND && chk->sent != SCTP_DATAGRAM_UNSENT)
1243179157Srrs				sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
1244179157Srrs			chk->sent = SCTP_DATAGRAM_RESEND;
1245319407Stuexen			chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
1246179157Srrs		}
1247224641Stuexen		if (!(net->dest_state & SCTP_ADDR_REACHABLE)) {
1248163953Srrs			/*
1249163953Srrs			 * If the address went un-reachable, we need to move
1250171572Srrs			 * to the alternate for ALL chunks in queue
1251163953Srrs			 */
1252212712Stuexen			sctp_move_chunks_from_net(stcb, net);
1253163953Srrs		}
1254163953Srrs		/* mark the retran info */
1255163953Srrs		if (asconf->sent != SCTP_DATAGRAM_RESEND)
1256163953Srrs			sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
1257163953Srrs		asconf->sent = SCTP_DATAGRAM_RESEND;
1258319407Stuexen		asconf->flags |= CHUNK_FLAGS_FRAGMENT_OK;
1259179157Srrs
1260179157Srrs		/* send another ASCONF if any and we can do */
1261179157Srrs		sctp_send_asconf(stcb, alt, SCTP_ADDR_NOT_LOCKED);
1262163953Srrs	}
1263163953Srrs	return (0);
1264163953Srrs}
1265163953Srrs
1266172091Srrs/* Mobility adaptation */
1267172156Srrsvoid
1268172091Srrssctp_delete_prim_timer(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
1269228653Stuexen    struct sctp_nets *net SCTP_UNUSED)
1270172091Srrs{
1271172091Srrs	if (stcb->asoc.deleted_primary == NULL) {
1272172091Srrs		SCTPDBG(SCTP_DEBUG_ASCONF1, "delete_prim_timer: deleted_primary is not stored...\n");
1273172091Srrs		sctp_mobility_feature_off(inp, SCTP_MOBILITY_PRIM_DELETED);
1274172156Srrs		return;
1275172091Srrs	}
1276172091Srrs	SCTPDBG(SCTP_DEBUG_ASCONF1, "delete_prim_timer: finished to keep deleted primary ");
1277172091Srrs	SCTPDBG_ADDR(SCTP_DEBUG_ASCONF1, &stcb->asoc.deleted_primary->ro._l_addr.sa);
1278172091Srrs	sctp_free_remote_addr(stcb->asoc.deleted_primary);
1279172091Srrs	stcb->asoc.deleted_primary = NULL;
1280172091Srrs	sctp_mobility_feature_off(inp, SCTP_MOBILITY_PRIM_DELETED);
1281172156Srrs	return;
1282172091Srrs}
1283172091Srrs
1284163953Srrs/*
1285163953Srrs * For the shutdown and shutdown-ack, we do not keep one around on the
1286163953Srrs * control queue. This means we must generate a new one and call the general
1287163953Srrs * chunk output routine, AFTER having done threshold management.
1288214928Stuexen * It is assumed that net is non-NULL.
1289163953Srrs */
1290163953Srrsint
1291163953Srrssctp_shutdown_timer(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
1292163953Srrs    struct sctp_nets *net)
1293163953Srrs{
1294163953Srrs	struct sctp_nets *alt;
1295163953Srrs
1296298942Spfg	/* first threshold management */
1297163953Srrs	if (sctp_threshold_management(inp, stcb, net, stcb->asoc.max_send_times)) {
1298163953Srrs		/* Assoc is over */
1299163953Srrs		return (1);
1300163953Srrs	}
1301214928Stuexen	sctp_backoff_on_timeout(stcb, net, 1, 0, 0);
1302163953Srrs	/* second select an alternative */
1303163953Srrs	alt = sctp_find_alternate_net(stcb, net, 0);
1304163953Srrs
1305163953Srrs	/* third generate a shutdown into the queue for out net */
1306214928Stuexen	sctp_send_shutdown(stcb, alt);
1307214928Stuexen
1308163953Srrs	/* fourth restart timer */
1309163953Srrs	sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, inp, stcb, alt);
1310163953Srrs	return (0);
1311163953Srrs}
1312163953Srrs
1313163953Srrsint
1314163953Srrssctp_shutdownack_timer(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
1315163953Srrs    struct sctp_nets *net)
1316163953Srrs{
1317163953Srrs	struct sctp_nets *alt;
1318163953Srrs
1319298942Spfg	/* first threshold management */
1320163953Srrs	if (sctp_threshold_management(inp, stcb, net, stcb->asoc.max_send_times)) {
1321163953Srrs		/* Assoc is over */
1322163953Srrs		return (1);
1323163953Srrs	}
1324214928Stuexen	sctp_backoff_on_timeout(stcb, net, 1, 0, 0);
1325163953Srrs	/* second select an alternative */
1326163953Srrs	alt = sctp_find_alternate_net(stcb, net, 0);
1327163953Srrs
1328163953Srrs	/* third generate a shutdown into the queue for out net */
1329163953Srrs	sctp_send_shutdown_ack(stcb, alt);
1330163953Srrs
1331163953Srrs	/* fourth restart timer */
1332163953Srrs	sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNACK, inp, stcb, alt);
1333163953Srrs	return (0);
1334163953Srrs}
1335163953Srrs
1336163953Srrsstatic void
1337163953Srrssctp_audit_stream_queues_for_size(struct sctp_inpcb *inp,
1338163953Srrs    struct sctp_tcb *stcb)
1339163953Srrs{
1340163953Srrs	struct sctp_stream_queue_pending *sp;
1341217760Stuexen	unsigned int i, chks_in_queue = 0;
1342163953Srrs	int being_filled = 0;
1343163953Srrs
1344163953Srrs	/*
1345163953Srrs	 * This function is ONLY called when the send/sent queues are empty.
1346163953Srrs	 */
1347163953Srrs	if ((stcb == NULL) || (inp == NULL))
1348163953Srrs		return;
1349163953Srrs
1350163953Srrs	if (stcb->asoc.sent_queue_retran_cnt) {
1351169420Srrs		SCTP_PRINTF("Hmm, sent_queue_retran_cnt is non-zero %d\n",
1352163953Srrs		    stcb->asoc.sent_queue_retran_cnt);
1353163953Srrs		stcb->asoc.sent_queue_retran_cnt = 0;
1354163953Srrs	}
1355217760Stuexen	if (stcb->asoc.ss_functions.sctp_ss_is_empty(stcb, &stcb->asoc)) {
1356218241Stuexen		/* No stream scheduler information, initialize scheduler */
1357218241Stuexen		stcb->asoc.ss_functions.sctp_ss_init(stcb, &stcb->asoc, 0);
1358218241Stuexen		if (!stcb->asoc.ss_functions.sctp_ss_is_empty(stcb, &stcb->asoc)) {
1359218241Stuexen			/* yep, we lost a stream or two */
1360218241Stuexen			SCTP_PRINTF("Found additional streams NOT managed by scheduler, corrected\n");
1361163953Srrs		} else {
1362218241Stuexen			/* no streams lost */
1363163953Srrs			stcb->asoc.total_output_queue_size = 0;
1364163953Srrs		}
1365163953Srrs	}
1366163953Srrs	/* Check to see if some data queued, if so report it */
1367217760Stuexen	for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
1368217760Stuexen		if (!TAILQ_EMPTY(&stcb->asoc.strmout[i].outqueue)) {
1369217760Stuexen			TAILQ_FOREACH(sp, &stcb->asoc.strmout[i].outqueue, next) {
1370163953Srrs				if (sp->msg_is_complete)
1371163953Srrs					being_filled++;
1372163953Srrs				chks_in_queue++;
1373163953Srrs			}
1374163953Srrs		}
1375163953Srrs	}
1376163953Srrs	if (chks_in_queue != stcb->asoc.stream_queue_cnt) {
1377169420Srrs		SCTP_PRINTF("Hmm, stream queue cnt at %d I counted %d in stream out wheel\n",
1378163953Srrs		    stcb->asoc.stream_queue_cnt, chks_in_queue);
1379163953Srrs	}
1380163953Srrs	if (chks_in_queue) {
1381163953Srrs		/* call the output queue function */
1382172090Srrs		sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_T3, SCTP_SO_NOT_LOCKED);
1383163953Srrs		if ((TAILQ_EMPTY(&stcb->asoc.send_queue)) &&
1384163953Srrs		    (TAILQ_EMPTY(&stcb->asoc.sent_queue))) {
1385163953Srrs			/*
1386163953Srrs			 * Probably should go in and make it go back through
1387163953Srrs			 * and add fragments allowed
1388163953Srrs			 */
1389163953Srrs			if (being_filled == 0) {
1390169420Srrs				SCTP_PRINTF("Still nothing moved %d chunks are stuck\n",
1391163953Srrs				    chks_in_queue);
1392163953Srrs			}
1393163953Srrs		}
1394163953Srrs	} else {
1395169420Srrs		SCTP_PRINTF("Found no chunks on any queue tot:%lu\n",
1396163953Srrs		    (u_long)stcb->asoc.total_output_queue_size);
1397163953Srrs		stcb->asoc.total_output_queue_size = 0;
1398163953Srrs	}
1399163953Srrs}
1400163953Srrs
1401163953Srrsint
1402163953Srrssctp_heartbeat_timer(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
1403224641Stuexen    struct sctp_nets *net)
1404163953Srrs{
1405224641Stuexen	uint8_t net_was_pf;
1406171440Srrs
1407228907Stuexen	if (net->dest_state & SCTP_ADDR_PF) {
1408228907Stuexen		net_was_pf = 1;
1409228907Stuexen	} else {
1410228907Stuexen		net_was_pf = 0;
1411228907Stuexen	}
1412228907Stuexen	if (net->hb_responded == 0) {
1413228907Stuexen		if (net->ro._s_addr) {
1414228907Stuexen			/*
1415228907Stuexen			 * Invalidate the src address if we did not get a
1416228907Stuexen			 * response last time.
1417228907Stuexen			 */
1418228907Stuexen			sctp_free_ifa(net->ro._s_addr);
1419228907Stuexen			net->ro._s_addr = NULL;
1420228907Stuexen			net->src_addr_selected = 0;
1421224641Stuexen		}
1422228907Stuexen		sctp_backoff_on_timeout(stcb, net, 1, 0, 0);
1423228907Stuexen		if (sctp_threshold_management(inp, stcb, net, stcb->asoc.max_send_times)) {
1424228907Stuexen			/* Assoc is over */
1425228907Stuexen			return (1);
1426163953Srrs		}
1427163953Srrs	}
1428228907Stuexen	/* Zero PBA, if it needs it */
1429228907Stuexen	if (net->partial_bytes_acked) {
1430228907Stuexen		net->partial_bytes_acked = 0;
1431228907Stuexen	}
1432163953Srrs	if ((stcb->asoc.total_output_queue_size > 0) &&
1433163953Srrs	    (TAILQ_EMPTY(&stcb->asoc.send_queue)) &&
1434163953Srrs	    (TAILQ_EMPTY(&stcb->asoc.sent_queue))) {
1435163953Srrs		sctp_audit_stream_queues_for_size(inp, stcb);
1436163953Srrs	}
1437224641Stuexen	if (!(net->dest_state & SCTP_ADDR_NOHB) &&
1438224641Stuexen	    !((net_was_pf == 0) && (net->dest_state & SCTP_ADDR_PF))) {
1439163953Srrs		/*
1440224641Stuexen		 * when move to PF during threshold mangement, a HB has been
1441224641Stuexen		 * queued in that routine
1442163953Srrs		 */
1443234296Stuexen		uint32_t ms_gone_by;
1444234296Stuexen
1445234296Stuexen		if ((net->last_sent_time.tv_sec > 0) ||
1446234296Stuexen		    (net->last_sent_time.tv_usec > 0)) {
1447234296Stuexen			struct timeval diff;
1448234296Stuexen
1449234296Stuexen			SCTP_GETTIME_TIMEVAL(&diff);
1450234296Stuexen			timevalsub(&diff, &net->last_sent_time);
1451310773Stuexen			ms_gone_by = (uint32_t)(diff.tv_sec * 1000) +
1452310773Stuexen			    (uint32_t)(diff.tv_usec / 1000);
1453234296Stuexen		} else {
1454234296Stuexen			ms_gone_by = 0xffffffff;
1455234296Stuexen		}
1456234297Stuexen		if ((ms_gone_by >= net->heart_beat_delay) ||
1457234297Stuexen		    (net->dest_state & SCTP_ADDR_PF)) {
1458234296Stuexen			sctp_send_hb(stcb, net, SCTP_SO_NOT_LOCKED);
1459234296Stuexen		}
1460163953Srrs	}
1461163953Srrs	return (0);
1462163953Srrs}
1463163953Srrs
1464163953Srrsvoid
1465163953Srrssctp_pathmtu_timer(struct sctp_inpcb *inp,
1466163953Srrs    struct sctp_tcb *stcb,
1467163953Srrs    struct sctp_nets *net)
1468163953Srrs{
1469179157Srrs	uint32_t next_mtu, mtu;
1470163953Srrs
1471228653Stuexen	next_mtu = sctp_get_next_mtu(net->mtu);
1472169352Srrs
1473179157Srrs	if ((next_mtu > net->mtu) && (net->port == 0)) {
1474169352Srrs		if ((net->src_addr_selected == 0) ||
1475169352Srrs		    (net->ro._s_addr == NULL) ||
1476169352Srrs		    (net->ro._s_addr->localifa_flags & SCTP_BEING_DELETED)) {
1477169420Srrs			if ((net->ro._s_addr != NULL) && (net->ro._s_addr->localifa_flags & SCTP_BEING_DELETED)) {
1478169352Srrs				sctp_free_ifa(net->ro._s_addr);
1479169352Srrs				net->ro._s_addr = NULL;
1480169352Srrs				net->src_addr_selected = 0;
1481169420Srrs			} else if (net->ro._s_addr == NULL) {
1482179157Srrs#if defined(INET6) && defined(SCTP_EMBEDDED_V6_SCOPE)
1483179157Srrs				if (net->ro._l_addr.sa.sa_family == AF_INET6) {
1484179157Srrs					struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
1485179157Srrs
1486179157Srrs					/* KAME hack: embed scopeid */
1487197288Srrs					(void)sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone));
1488179157Srrs				}
1489179157Srrs#endif
1490179157Srrs
1491169420Srrs				net->ro._s_addr = sctp_source_address_selection(inp,
1492169420Srrs				    stcb,
1493310773Stuexen				    (sctp_route_t *)&net->ro,
1494169420Srrs				    net, 0, stcb->asoc.vrf_id);
1495179157Srrs#if defined(INET6) && defined(SCTP_EMBEDDED_V6_SCOPE)
1496179157Srrs				if (net->ro._l_addr.sa.sa_family == AF_INET6) {
1497179157Srrs					struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
1498179157Srrs
1499179157Srrs					(void)sa6_recoverscope(sin6);
1500179157Srrs				}
1501179157Srrs#endif				/* INET6 */
1502169352Srrs			}
1503169352Srrs			if (net->ro._s_addr)
1504169352Srrs				net->src_addr_selected = 1;
1505169352Srrs		}
1506169352Srrs		if (net->ro._s_addr) {
1507169352Srrs			mtu = SCTP_GATHER_MTU_FROM_ROUTE(net->ro._s_addr, &net->ro._s_addr.sa, net->ro.ro_rt);
1508268526Stuexen#if defined(INET) || defined(INET6)
1509185694Srrs			if (net->port) {
1510185694Srrs				mtu -= sizeof(struct udphdr);
1511185694Srrs			}
1512268526Stuexen#endif
1513169352Srrs			if (mtu > next_mtu) {
1514163953Srrs				net->mtu = next_mtu;
1515286781Stuexen			} else {
1516286781Stuexen				net->mtu = mtu;
1517163953Srrs			}
1518163953Srrs		}
1519163953Srrs	}
1520163953Srrs	/* restart the timer */
1521163953Srrs	sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net);
1522163953Srrs}
1523163953Srrs
1524163953Srrsvoid
1525163953Srrssctp_autoclose_timer(struct sctp_inpcb *inp,
1526163953Srrs    struct sctp_tcb *stcb,
1527163953Srrs    struct sctp_nets *net)
1528163953Srrs{
1529163953Srrs	struct timeval tn, *tim_touse;
1530163953Srrs	struct sctp_association *asoc;
1531163953Srrs	int ticks_gone_by;
1532163953Srrs
1533169378Srrs	(void)SCTP_GETTIME_TIMEVAL(&tn);
1534163953Srrs	if (stcb->asoc.sctp_autoclose_ticks &&
1535163953Srrs	    sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE)) {
1536163953Srrs		/* Auto close is on */
1537163953Srrs		asoc = &stcb->asoc;
1538163953Srrs		/* pick the time to use */
1539163953Srrs		if (asoc->time_last_rcvd.tv_sec >
1540163953Srrs		    asoc->time_last_sent.tv_sec) {
1541163953Srrs			tim_touse = &asoc->time_last_rcvd;
1542163953Srrs		} else {
1543163953Srrs			tim_touse = &asoc->time_last_sent;
1544163953Srrs		}
1545163953Srrs		/* Now has long enough transpired to autoclose? */
1546163953Srrs		ticks_gone_by = SEC_TO_TICKS(tn.tv_sec - tim_touse->tv_sec);
1547163953Srrs		if ((ticks_gone_by > 0) &&
1548163953Srrs		    (ticks_gone_by >= (int)asoc->sctp_autoclose_ticks)) {
1549163953Srrs			/*
1550163953Srrs			 * autoclose time has hit, call the output routine,
1551163953Srrs			 * which should do nothing just to be SURE we don't
1552163953Srrs			 * have hanging data. We can then safely check the
1553163953Srrs			 * queues and know that we are clear to send
1554163953Srrs			 * shutdown
1555163953Srrs			 */
1556172090Srrs			sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_AUTOCLOSE_TMR, SCTP_SO_NOT_LOCKED);
1557163953Srrs			/* Are we clean? */
1558163953Srrs			if (TAILQ_EMPTY(&asoc->send_queue) &&
1559163953Srrs			    TAILQ_EMPTY(&asoc->sent_queue)) {
1560163953Srrs				/*
1561163953Srrs				 * there is nothing queued to send, so I'm
1562163953Srrs				 * done...
1563163953Srrs				 */
1564347165Stuexen				if (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_SENT) {
1565163953Srrs					/* only send SHUTDOWN 1st time thru */
1566224641Stuexen					struct sctp_nets *netp;
1567224641Stuexen
1568347165Stuexen					if ((SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) ||
1569347165Stuexen					    (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
1570246588Stuexen						SCTP_STAT_DECR_GAUGE32(sctps_currestab);
1571246588Stuexen					}
1572347165Stuexen					SCTP_SET_STATE(stcb, SCTP_STATE_SHUTDOWN_SENT);
1573347165Stuexen					SCTP_CLEAR_SUBSTATE(stcb, SCTP_STATE_SHUTDOWN_PENDING);
1574246588Stuexen					sctp_stop_timers_for_shutdown(stcb);
1575224641Stuexen					if (stcb->asoc.alternate) {
1576224641Stuexen						netp = stcb->asoc.alternate;
1577224641Stuexen					} else {
1578224641Stuexen						netp = stcb->asoc.primary_destination;
1579224641Stuexen					}
1580224641Stuexen					sctp_send_shutdown(stcb, netp);
1581163953Srrs					sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
1582163953Srrs					    stcb->sctp_ep, stcb,
1583224641Stuexen					    netp);
1584163953Srrs					sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
1585163953Srrs					    stcb->sctp_ep, stcb,
1586224641Stuexen					    netp);
1587163953Srrs				}
1588163953Srrs			}
1589163953Srrs		} else {
1590163953Srrs			/*
1591163953Srrs			 * No auto close at this time, reset t-o to check
1592163953Srrs			 * later
1593163953Srrs			 */
1594163953Srrs			int tmp;
1595163953Srrs
1596163953Srrs			/* fool the timer startup to use the time left */
1597163953Srrs			tmp = asoc->sctp_autoclose_ticks;
1598163953Srrs			asoc->sctp_autoclose_ticks -= ticks_gone_by;
1599163953Srrs			sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb,
1600163953Srrs			    net);
1601163953Srrs			/* restore the real tick value */
1602163953Srrs			asoc->sctp_autoclose_ticks = tmp;
1603163953Srrs		}
1604163953Srrs	}
1605163953Srrs}
1606